Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

use lib statement with path variable

by sans-clue (Beadle)
on Dec 04, 2013 at 19:23 UTC ( #1065637=perlquestion: print w/ replies, xml ) Need Help??
sans-clue has asked for the wisdom of the Perl Monks concerning the following question:

I need to set a use lib statement with a path variable. use lib qw($some_path);

but as you can guess it says

Can't locate Foo.pm in @INC (@INC contains: $some_path /usr/local.....etc

This path happens to be the current path of the script plus /lib, not that it matters.

What kind of escaping do I need to do here to have it 'see' the actual value of $some_path ?

Thanks

Comment on use lib statement with path variable
Re: use lib statement with path variable
by Eily (Deacon) on Dec 04, 2013 at 19:30 UTC

    use lib ($some_path,);

    Make sure the $some_path variable is set early enough. For example, in a BEGIN block, because the value might only get affected at run time while "use" is called at compile time (straightaway)

      Well this is what I am doing, finding the current path of the script first, so I am not sure if I can set $some_path any earlier.
      #!/usr/bin/perl use File::Spec; use File::Basename; $some_path = dirname(File::Spec->rel2abs(__FILE__)); use lib qw($some_path/lib);

        There are two problems there. qw takes each blank characters separated elements in its operand (most often, words separated by spaces) and reads them in a simple quotish context. Put a simpler way, qw(A $b C %h); could also be written ('A', '$b', 'C', '%h'). In Ruby there is a qw-like construct that allows double-quotish interpretation, but this is Perl. So you instead have to write use lib ("$some_path/lib",); (the comma is optional, I just always put an extra comma in a single element list).

        The second issue is that use is called straightaway, so you could say that perl reads your program :

        # Compiling using File::Spec using File::Basename there is a global variable called $some_path add to path $some_path/lib _Compilation complete_ # Running $some_path = dirname(File::Spec->rel2abs(__FILE__));
        This is of course really simplified.

        The BEGIN keyword means that a block has to be executed during compile time, and not to wait after compilation completion. You should write:

        my $some_path; BEGIN { $some_path = "/my/path"; } use lib "$some_path/lib"; # Yeah, you don't even need the parenthesis, + Perl is clever enough for you # Edit : thanks dave_the_m for the correction

Re: use lib statement with path variable
by AnomalousMonk (Abbot) on Dec 04, 2013 at 19:48 UTC
    ... path happens to be the current path of the script plus /lib ...

    If the directory you want to add is a fixed and existing sub-directory of the script directory, I don't understand why lib won't work:

    c:\@Work\Perl\monks\dougbot>perl -wMstrict -le "use lib './junque'; local $\" = qq{'\n'}; print qq{'@INC'}; " './junque' 'C:/strawberry/5.14/perl/site/lib' 'C:/strawberry/5.14/perl/vendor/lib' 'C:/strawberry/5.14/perl/lib' '.' c:\@Work\Perl\monks\dougbot>dir ... 12/04/2013 02:35 PM <DIR> . 12/04/2013 02:35 PM <DIR> .. 12/04/2013 02:06 PM 9,730 array_disjunct_1.pl 12/04/2013 02:35 PM <DIR> junque
Re: use lib statement with path variable
by scorpio17 (Monsignor) on Dec 04, 2013 at 20:04 UTC
    I think a better way to deal with this type of problem is to use the PERL5LIB env variable, rather than a $path variable inside your script.
Re: use lib statement with path variable
by Laurent_R (Parson) on Dec 04, 2013 at 22:25 UTC
    If you are using use lib somepath, I fail to see the advantage of having somepath stored in a $variable, I think that in almost all cases that I can think of, somepath could just be a hard coded directory. The only exception that I can think of is when yo want to deploy a module on different environments having different paths, but in such a case, you probably want to go further and have a real installation procedure that will define or export the right paths.
Re: use lib statement with path variable ( File::FindLib )
by Anonymous Monk on Dec 05, 2013 at 01:39 UTC
    use File::FindLib - Find and use a file/dir from a directory above your script file

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1065637]
Approved by Eily
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-12-20 02:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls