You probably want to put coderef at the top of @INC, as described in perldoc -f require . From there, you can create exceptions to prevent the loading of certain modules, or do nothing to demand that you continue your normal work of finding the module in other @INC entries.
$ perl -E'BEGIN { unshift @INC, sub { die q{no NEXT} if pop eq q{NEXT.pm}; () }; }; use Carp; say q{success}' success $ perl -E'BEGIN { unshift @INC, sub { die q{no NEXT} if pop eq q{NEXT.pm}; () }; }; use NEXT; say q{success}' no NEXT at -e line 1. BEGIN failed--compilation aborted at -e line 1.
If you want this behavior to be lexical, you should use Perl %^H hash lists. Dealing with this is a bit difficult, so I would recommend using Devel::Pragma , which can take care of all the gory details for you.
As you pointed out, @INC hooks @INC not be executed for an already loaded module. If you also need to connect to use or require loaded module, then overriding CORE::GLOBAL::require will work, because it is called for every attempt to load the module.
$ perl -E'BEGIN { *CORE::GLOBAL::require = sub { warn @_ } } use NEXT; use NEXT;' NEXT.pm at -e line 1 NEXT.pm at -e line 1.
Also, as a supporter of NEXT, I fully approve of preventing people from using it at all. :-)
source share