Yes, this is possible, but also, that way lies madness. If you go down that road, you will make things much harder for you, and you will have to debug lots of weird errors. You are much better served by re-installing a fresh copy of all modules with your current Perl, and keeping your modules (say) in a cpanfile so you can reinstall them quickly whenever you move to a new version.
Perl will not reload a module if it has already been loaded. A first, horrible approach is to manually pre-load all the modules you want from the appropriate directories:
use 5.020;
use feature 'experimental::signatures';
no warnings 'experimental::signatures';
sub preload_module( $base_dir, $modulename ) {
my $final_name = $modulename;
$final_name =~ s!::!/!g;
require "$base_dir/$final_name.pm";
}
BEGIN {
preload_module('/some/perl5/dir', 'List::Util' );
preload_module('/some/perl5/dir', 'Scalar::Util' );
preload_module('/some/other/perl5/dir', 'Another::Module' );
}
use List::Util 'reduce';
An even more horrible way is to set up a hook in @INC which looks at the file to be loaded and chooses the correct one:
use 5.020;
use feature 'experimental::signatures';
no warnings 'experimental::signatures';
my %module_map = (
'List/Util.pm' => '/some/path/to/perl/modules',
'Scalar/Util.pm' => '/some/other/path/to/perl/modules',
);
sub find_correct_module( $self, $module_file ) {
my $real_module = $module_map{ $module_file } // $module_file;
open my $fh, '<', $real_module or croak $!;
return $fh
}
BEGIN {
unshift @INC, \&find_correct_module;
}
This will be even harder to debug but allows you to have deep dependencies automatically resolved without preloading modules.
Again, my advice is to do a fresh start and keep a list of all modules you need, and use local::lib and to install all dependencies below a directory separate from the system Perl directory. |