Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: Modules 101 :: Using a Local Module from CPAN

by redapplesonly (Sexton)
on Dec 06, 2022 at 18:13 UTC ( [id://11148620]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^3: Modules 101 :: Using a Local Module from CPAN
by hv (Prior) on Dec 06, 2022 at 18:46 UTC

    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 :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-26 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found