in reply to Modules 101 :: Using a Local Module from CPAN

Instead of requiring the file with full path, see lib:
use lib '/path/to/my/modules'; use Script::Singleton;

If the module is located in a place relative to the script, you can use FindBin:

use FindBin; use lib "$FindBin::Bin/lib"; use Script::Singleton;

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Modules 101 :: Using a Local Module from CPAN
by redapplesonly (Sexton) on Dec 06, 2022 at 18:13 UTC

    Thanks choroba,

    I'm afraid either solution doesn't work. When I change my script to:

    use lib '/path/to/my/local/directory/'; use Script::Singleton; # Line 6

    ...the output is still:

    Can't locate Script/Singleton.pm in @INC (you may need to install the +Script::Singleton module) (@INC contains: /home/demo/pullWorkload/scp +LRSIControllerDir/toys/ /etc/perl /usr/local/lib/x86_64-linux-gnu/per +l/5.30.0 /usr/local/share/perl/5.30.0 /usr/lib/x86_64-linux-gnu/perl5 +/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.30 /usr/share +/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-ba +se) at ./testMe.perl line 6.

    I note that the use lib must specify the directory, not the .PM file, BTW.

    If the use lib doesn't add /path/to/my/local/directory/ to @INC, I'm at a loss on what might be the problem. Any thoughts...? Thank you for writing.

      Perl always expects each :: in a module name to translate to a path separator in the directory hierarchy. So for the module Script::Singleton it will look for the file Singleton.pm in a subdirectory named Script underneath one of the directories specified in @INC.

      So if you want to reference it from /home/demo/pullWorkload/scpLRSIControllerDir/toys, you need to create /home/demo/pullWorkload/scpLRSIControllerDir/toys/Script (with mkdir), and put the file Singleton.pm in that directory.

      I would recommend this as a temporary solution only. For CPAN modules it is almost always preferable to keep them in a separate place - see other people's suggestions for how to do that.

      The handling of :: in module names is not trivial to find in the documentation. From perldoc -f use you will see that use Script::Singleton; is exactly equivalent to BEGIN { require Script::Singleton; Script::Singleton->import(); }. You then need to understand that since Script::Singleton is not in quotes, it is what perl calls a "bareword", and then find the part of perldoc -f require that describes this:

      If EXPR is a bareword, "require" assumes a .pm extension a +nd replaces "::" with "/" in the filename for you, to make it + easy to load standard modules.
        Thanks hv! Yes! Yes, this is the piece of information I was hoping for. Thanks so much for writing, you've explained it well enough for me to understand it, which is a pretty big accomplishment, actually. Thank you, may the universe shower you in positive kudos today :)