http://qs1969.pair.com?node_id=52116


in reply to Callback Design

1. This dirty bit of nastiness appears to do what you want:
package Foo; my $coderef = sub { no strict 'refs'; ${ (caller)[0] . "::global" } }; $global = 'in Foo'; package Bar; $global = 'in Bar'; print &$coderef, "\n";
..but why on earth do you want to do this? :)

This kind of magic side-effect can cause incredibly hard-to-diagnose bugs: someone might move the subroutine call into another package, never suspecting that it might vary it's behaviour based on something as oblique as compiler directives in effect in the most-recent calling code.

2. Using $_ for callbacks is fine AFAIK, as long as you are careful and the callbacks are short. I've used things like hostname_cleanup => sub { s/test\.userfriendly\.org$//i } to pass regexps in from configuration files before I learned about qr//. I too will be interested to see what the real experts think!