Solution:
my $old_require =
defined(&CORE::GLOBAL::require)
? \&CORE::GLOBAL::require
: sub { CORE::require($_[0]) };
my $new_require = sub {
my $path = shift;
if ($INC{$path}) {
return $old_require->($path);
}
my $rv = $old_require->($path);
warn("Loaded $path after fork\n");
return $rv;
};
no warnings 'redefine';
*CORE::GLOBAL::require = $new_require;
The solution has the following features:
- Logs modules loaded by
require EXPR
. - Logs modules loaded by
require BAREWORD
. - Logs modules loaded by
use BAREWORD
. - Ignores code loaded by
do EXPR
. - Ignores modules that fail to load.
- Ignores modules that have already been loaded.
- Works even if
@INC
is manipulated.
The last four were problems experienced by the solution in the OP. I can't find any downsides to this solution.