Search code examples
perlrequire

How can I wrap require and use?


Let's say I want to wrap require such that,

package MyModule;
use Data::Dumper;

Would output either,

MyModule -> Data::Dumper
MyModule -> Data/Dumper.pm

For all packages and all requires/use statements. How could I do it?


Solution

  • BEGIN {
       unshift @INC, sub {
          printf "%s -> %s\n", ( caller() )[0], $_[1];
          return;
       };
    }
    

    See the paragraph starting with "You can also insert hooks into the import facility" in require's documentation.