Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Let a module call a sub of the caller

by ikegami (Pope)
on Sep 06, 2012 at 23:57 UTC ( #992211=note: print w/replies, xml ) Need Help??


in reply to Let a module call a sub of the caller

The same mechanisms pragmas use to clean themselves up can be used here. Here's an example:

call_when_scope_is_compiled.pm:

package call_when_scope_is_compiled; use Sub::ScopeFinalizer qw( scope_finalizer ); sub import { my ($class, $sub) = @_; $^H{__PACKAGE__ . '::'} = scope_finalizer { $sub->() }; } 1;
$ perl -E' say "post"; use call_when_scope_is_compiled \&foo; sub foo { say "foo!" } BEGIN { say "pre"; } ' pre foo! post

It also works for narrower scopes.

$ perl -E' { use call_when_scope_is_compiled \&foo; sub foo { say "foo!" } BEGIN { say "pre"; } } BEGIN { say "post"; } ' pre foo! post

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992211]
help
Chatterbox?
[Eily]: yes we're pretty prawn to making puns around here
Eily went to the wiktionary to check that the pronunciations of prawn and prone are actually similar. Turns out prawn can be an alternative form of porn...
[Discipulus]: well i'm still at the end of the food chain..
[Discipulus]: so a new life as krill in prague will be good

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2017-06-29 12:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (662 votes). Check out past polls.