Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re: Callback Designby repson (Chaplain) |
on Jan 16, 2001 at 08:43 UTC ( [id://52155]=note: print w/replies, xml ) | Need Help?? |
The problem is that all coderefs work like closures. When
a piece of code is complied the the variable $foo in it, it
will not store the name $foo, but rather the current
position in the parse tree that $foo would refer to in
normal usage. When this piece of compiled code is passed
into a subroutine in any package it isn't changed, so the
place that said $foo still refers to the variable which was
complied into the code.
The map, sort and grep callbacks use the variables $_, $a and $b which are special cases, where their usage always refers to the same position in the parse tree (though that can be displaced with local, but we won't go into that). On the other hand callbacks for modules such as HTML::Parser and Tk are passed a full list in @_ (using $code_ref->('foo',3,'blah');) which means those callbacks generally begin with my($foo,$blah)=@_; to extract that data. In your case I'd say your best bet may be to stick to using $_, something like this.
In Section
Seekers of Perl Wisdom
|
|