Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^5: help with "symbol lookup error" message

by Special_K (Monk)
on Feb 03, 2023 at 20:28 UTC ( [id://11150145]=note: print w/replies, xml ) Need Help??


in reply to Re^4: help with "symbol lookup error" message
in thread help with "symbol lookup error" message

So in my example, which uses 5.20.0, File::Copy is calling List::Util, but is finding the version installed at /home/user_foo/perl_modules/lib/perl5, rather than the version installed at a location corresponding to (and compatible with) 5.20.0. Is that correct? Is it possible to tell perl to use one path for a specific module but use another path (or set of paths) for other modules?

Replies are listed 'Best First'.
Re^6: help with "symbol lookup error" message
by Corion (Patriarch) on Feb 03, 2023 at 20:42 UTC

    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.

      > and keeping your modules (say) in a cpanfile so you can reinstall them quickly whenever you move to a new version.


      This is the first time I've heard the term cpanfile, so I read this page:
      https://metacpan.org/dist/Module-CPANfile/view/lib/cpanfile.pod

      and it seems I could just maintain this file by having a requires $module line for each module I want to maintain. Then when a new version of perl is installed, I can just create a new directory, copy the cpanfile, and then run cpanm to reinstall all modules in that local directory using the new version of perl. Is that correct?

      If I do that, is a record created anywhere in that directory indicating what version of perl was used to do the installation, or do I need to maintain that myself via directory naming conventions and/or README files?

      Also does a new directory need to be created for each version of perl no matter how minor the update? For example, if I am currently using 5.36 and a 5.36.1 is released, do I need to reinstall all modules using 5.36.1 or can I wait for a larger update, e.g. 5.37?

        Yes, that's the idea.

        I recommend installing all modules with cpanm and its -L option to install everything specific for an application in a separate directory.

        Usually, Perl guarantees (resp. takes great care) that you can upgrade between minor versions without recompiling provided that the new version of Perl was compiled exactly as the previous version. I'm not sure whether you can actually do that. I would simply reinstall, as that is usually easier.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11150145]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-03-29 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found