in reply to Callback Design
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.
$_ = 'hi'; my $code = sub { print $_->{blah} . "\t" . $_->{moo} . "\n" }; Bar::hi($code); print $_ . "\n"; # prints 'hi' package Bar; sub hi { my $coderef = shift; local $_ = { 'foo' => 2, 'blah' => 5, 'moo' => 'blah' }; &$coderef; # prints '5 blah' }
|
|---|