Prev: FAQ 7.21 How do I redefine a builtin function, operator, or method?
Next: FAQ 8.14 How do I modify the shadow password file on a Unix system?
From: J�rgen Exner on 6 Jul 2010 21:59 Nick Wedd <nick(a)maproom.co.uk> wrote: [...] >Now I would like to generalise it, to work for subroutines other than >'arc'. I can promise that their first two arguments will be the 'from' >point and the 'to' point, I can't promise anything about the other >arguments. So I want to do something like > > sub polyanything { > my ( $displist, $from, $to, $functionname, @rest ) = @_; > foreach my $d ( @$displist ) { > CALL $functionname( add($from,$d), add($to,$d), @rest ); > } > } > >but, how do I do CALL? I have found googling for "Perl function call" >unhelpful, as you might expect. While from a technical point of view this is possible, in general it is A Very Bad Idea(TM). A much, much better approach would be using references, dispatch tables, and if applicable even closures. jue
From: Nick Wedd on 7 Jul 2010 11:39 In message <5ln736t8j5kh76tsa3jbg5smm8ckki4fm4(a)4ax.com>, J�rgen Exner <jurgenex(a)hotmail.com> writes >Nick Wedd <nick(a)maproom.co.uk> wrote: >[...] >>Now I would like to generalise it, to work for subroutines other than >>'arc'. I can promise that their first two arguments will be the 'from' >>point and the 'to' point, I can't promise anything about the other >>arguments. So I want to do something like >> >> sub polyanything { >> my ( $displist, $from, $to, $functionname, @rest ) = @_; >> foreach my $d ( @$displist ) { >> CALL $functionname( add($from,$d), add($to,$d), @rest ); >> } >> } >> >>but, how do I do CALL? I have found googling for "Perl function call" >>unhelpful, as you might expect. > >While from a technical point of view this is possible, in general it is >A Very Bad Idea(TM). >A much, much better approach would be using references, dispatch tables, >and if applicable even closures. My thanks to everyone who answered. Passing a reference to a function with \& , as recommended by Peter Makholm and Sherm Pendley, is what I was looking for. I have implemented it and it works. I had not intended to use symbolic references. They seem to have something in common with 'eval', so I realise that I would do best to avoid them. I have ordered myself a copy of Dominus' book 'Higher-Order Perl'. I will learn from it what a dispatch table is. Maybe I will even get my brain round closures at last. Nick -- Nick Wedd nick(a)maproom.co.uk
From: Uri Guttman on 7 Jul 2010 11:54 >>>>> "NW" == Nick Wedd <nick(a)maproom.co.uk> writes: NW> I had not intended to use symbolic references. They seem to have NW> something in common with 'eval', so I realise that I would do best to NW> avoid them. symrefs aren't directly related to eval but they share the same evil nature as they can be nasty and dangerous. the rule is use them only when they are the only or very best solution. they should never be a first resort if possible (which is what newbies tend to do) NW> I have ordered myself a copy of Dominus' book 'Higher-Order Perl'. NW> I will learn from it what a dispatch table is. Maybe I will even NW> get my brain round closures at last. dispatch tables have been covered in this newsgroup many many times. google for that term and you will learn them quickly. they are a very simple construct - basically a hash with names of ops for keys and code refs as values. you get the code ref out when you have an op name, and if it is there, call it. or handle an unknown name with a default call or error. uri -- Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: Ted Zlatanov on 7 Jul 2010 12:43
On Wed, 7 Jul 2010 16:39:55 +0100 Nick Wedd <nick(a)maproom.co.uk> wrote: NW> I have ordered myself a copy of Dominus' book 'Higher-Order Perl'. I NW> will learn from it what a dispatch table is. Maybe I will even get my NW> brain round closures at last. HOP is a complex book by any standard so I wouldn't recommend it to learn about dispatch tables or closures (both are basic programming concepts). You'll get lost quickly. Ted |