http://qs321.pair.com?node_id=603253

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

Is there a way to hook into the module loading process, so that a source filter gets applied to all modules loaded afterwards?

Some thing like:

BEGIN { # ... some magic } use Foo::Bar; # Source filter gets applied # when it's loaded.
Or is this crazy talk?

Replies are listed 'Best First'.
Re: Apply a source filter to all modules to be loaded?
by Corion (Patriarch) on Mar 05, 2007 at 18:44 UTC

    This is crazy talk, but that's not stopping Perl from having it, see require:

    sub my_own_loader { my ($self,$file) = @_; open my $fh, "<", "my/private/dir/$file" or return; $fh }; BEGIN { unshift @INC, \&my_own_loader; }; use Foo::Bar; use The::Net;

    You might also want to look at Filter::Simple, but please realize that source filtering isn't a sane practice :)

Re: Apply a source filter to all modules to be loaded? (yes)
by tye (Sage) on Mar 05, 2007 at 18:39 UTC
    Is there a way to hook into the module loading process

    Yes. unshift a code ref onto @INC.

    so that a source filter gets applied to all modules loaded afterwards [....] Or is this crazy talk?

    Yes, pretty much. (Some others might care what the original problem you are trying to solve is.)

    - tye        

Re: Apply a source filter to all modules to be loaded?
by chromatic (Archbishop) on Mar 05, 2007 at 19:22 UTC