Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Instance of 'use lib' won't accept relative path

by Lady_Aleena (Priest)
on Aug 21, 2020 at 17:06 UTC ( [id://11120969]=perlquestion: print w/replies, xml ) Need Help??

Lady_Aleena has asked for the wisdom of the Perl Monks concerning the following question:

I've been using relative paths in my use lib for years without any problems. However, today I used lib with a relative path in a script, and it is not finding the lib. I used the absolute path, and the lib was found. Could the problem be that the lib in question is not in PERL5LIB or PATH? Could the problem be something else entirely?

Update: I added the dir to PERL5LIB, and it worked. I just did not want to add it to @INC.

My OS is Debian 10 (Buster); my perl versions are 5.28.1 local and 5.16.3 or 5.30.0 on web host depending on the shebang.

No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
Lady Aleena

Replies are listed 'Best First'.
Re: Instance of 'use lib' won't accept relative path
by haj (Vicar) on Aug 21, 2020 at 18:43 UTC

    The libs in use lib don't need to be in PERL5LIB nor in PATH. Relative paths in use lib are not relative to the script itself, but to the current working directory. So either the script itself changed its current working directory, or you're calling it from an unexpected location (e.g. you've symlinked the script but not the library directory).

    For the frequent situation where you want to have the libraries relative to the script, FindBin is a convenient solution. Directly from the synopsis:

    use FindBin qw($Bin); use lib "$Bin/../lib";

    Edit:Now pointing to perldoc instead of CPAN, where a search can give different results.

      That won't work if you call the script via symlink. You need to use $RealBin.

      use FindBin qw( $RealBin ); use lib "$RealBin/../lib";

      Is that the actual usage, or does something go in place of the ..? In my case, the script is in $HOME/bin and the modules are in $HOME/bin/flies/lib. How would that change the usage, if there are any changes?

      My OS is Debian 10 (Buster); my perl versions are 5.28.1 local and 5.16.3 or 5.30.0 on web host depending on the shebang.

      No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
      Lady Aleena

        The usage from the SYNOPSIS is when you have a bin and lib sitting next to each other, e.g. in a typical CPAN distribution. In your case, that would be:

        use FindBin qw($Bin); use lib "$Bin/flies/lib";

        $Bin is the directory where your Perl script started. Have a look at the edge cases in FindBin whether you need FindBin::again or $FindBin::RealBin, but I guess it will already work as given. I apologize for having used a misleading CPAN link in my first post: FindBin is in Perl core.

        BTW, probably you should rethink your layout. A lib shouldn’t be under $HOME/bin/. A better place might be $HOME/lib/. Like /usr/local/bin and /usr/local/lib/. Alternatively you could put the whole stuff under /opt. See also. Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: Instance of 'use lib' won't accept relative path
by Anonymous Monk on Aug 21, 2020 at 18:25 UTC
      Since the problem at hand is most likely in the environment and not in the script, I can't see how an SSCCE would help here.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11120969]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-03-28 13:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found