Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Let a module call a sub of the caller

by moritz (Cardinal)
on Sep 06, 2012 at 19:24 UTC ( #992172=note: print w/ replies, xml ) Need Help??


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

Should call "foo" after compilation (not during BEGIN). Could the module somehow define an INIT block that calls the sub?

The problem with that is that modules INIT and runtime are still compile time from the perspective of the calling script.

You could run it in an END block, which is also after compilation, but I could imagine that that's too late for your purpose. So I don't know any solution, except via source filters (or maybe evil hijacking of DESTROY subs, depending on when exactly the routine should run).

Speaking of which, what is your purpose? Maybe there's a better solution to it if we know the bigger picture.


Comment on Re: Let a module call a sub of the caller
Download Code
Re^2: Let a module call a sub of the caller
by voj (Acolyte) on Sep 07, 2012 at 07:22 UTC

    I just wanted to facilitate the use of a Module and avoid exorting into the caller's namespace. In particular, the Module does not just call the method but does some processing to pass it the right parameters. The same could be done with an export:

    use My::Module qw(call); call(\&foo); sub foo { ... };

    I am not that familiar with the processing phases BEGIN,INIT,END, that's why I asked. Is there a way to call the method at the END of execution of the caller?:

    use My::Module call => 'foo'; sub foo { ... }; # foo is called at the end
      I am not that familiar with the processing phases BEGIN,INIT,END, that's why I asked. Is there a way to call the method at the END of execution of the caller?

      END time is the same for all, so yes, that works:

      # file CallMe.pm package CallMe; use 5.010; use strict; use warnings; my @to_call; sub import { my ($self, @routines) = @_; my $call_package = (caller(1))[0]; push @to_call, $call_package. "::$_" for @routines; } END { no strict 'refs'; $_->() for @to_call; } 1;

      And then in your script:

      use 5.010; use lib '.'; use CallMe 'foo'; sub foo { say 42 } say 'in mainline';

      prints

      in mainline 42

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992172]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-12-27 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls