dgaramond2 has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible to write something in MyWrapper module so that in a code like this:

package Foo;
use MyWrapper;

sub bar { ... }
sub baz { ... }
...
1;
or perhaps this:
package Foo;
use base qw(MyWrapper);

sub bar { ... }
sub baz { ... }
...
1;

I can list all subs in Foo and then wrap them all and then replace them all with Sub::Override? I already find out how to list subs and wrap them, the problem is where to put the code in MyWrapper :-). Of course I can just do this:

package Foo;
use MyWrapper qw(wrap_all);

sub bar { ... }
sub baz { ... }
...

wrap_all();
1;

but it's less magical, less automatic, and "less friendly" to my module users (which, most often, is myself).

  • Comment on Executing use'd/require'd/base module code *after* use'r/require'r/child is compiled?

Replies are listed 'Best First'.
Re: Executing use'd/require'd/base module code *after* use'r/require'r/child is compiled?
by jettero (Monsignor) on Feb 28, 2009 at 12:26 UTC
    I may not understand the question, but it seems like you want to write an import() sub. If I missed it, read the source for Memoize to see some pure magic you might want to copy, emulate, or borrow.

    -Paul

Re: Executing use'd/require'd/base module code *after* use'r/require'r/child is compiled?
by phaylon (Curate) on Mar 01, 2009 at 19:57 UTC

    You could use B::Hooks::EndOfScope in an import method. That's what namespace::clean uses to clean up the package's symbol table after it has been compiled.

    I hope this helps, but I'm not sure this exactly fits your use case. Is there a reason you don't want to be more explicit? Like, for example:

    wrap foo => sub { ... };

    Ordinary morality is for ordinary people. -- Aleister Crowley
Re: Executing use'd/require'd/base module code *after* use'r/require'r/child is compiled?
by dgaramond2 (Monk) on Mar 04, 2009 at 12:33 UTC

    Thanks to all who replied.

    I should've reminded myself (again!): "CPAN has everything, CPAN has *everything*" :-)

    After just a couple of minutes browsing CPAN, I ended up using Filter::EOF.

    And btw, apparently sub wrapping (including pre- and post-hooks) seem to be in a lot of people's scratch lists, evident via the existence of several CPAN modules to do this. In my specific case, I want to do some custom logging, parameter checking, as well as remoting with my subs.