As the title says, I’m not clear on when such a subroutine will be called. From the require page at perldoc one can write:
push @INC, \&my_sub;
sub my_sub {
my ($coderef, $filename) = @_; # $coderef is \&my_sub
...
}
but where does this go exactly? The required package or the requiring script (or package)? I’ve tried both with some sentinel print statements but neither worked so clearly there is something I’m not getting.
Perl calls a subroutine reference in
@INCwhen it is traversing@INCto look for a module. That is, you’ll trigger it when you try to load a module withuseorrequireand Perl does not find that module in the preceding@INClocations.You need to ensure that your subroutine reference is in
@INCbefore you try to load the modules. Remember thatuseis a compile time feature and thatrequireis a run time feature. As with adding other "regular"@INCentries, you probably want to do it in aBEGINblock as early as possible in your program.