Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Utility of "use lib qw{ . }" ?

by deprecated (Priest)
on Jun 23, 2003 at 13:50 UTC ( [id://268155]=perlquestion: print w/replies, xml ) Need Help??

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

I saw in a recent node of Juerd's (it isn't really relevant where) the following code:
use lib '.';
Now, at one point I used to do this myself. However, on accident one day I noticed that it wasn't necessary. Observe:
[scorch:~/monks] alex% ./testlib.pl bar bletch [scorch:~/monks] alex% find . . ./Demo ./Demo/SubDemo.pm ./Demo.pm ./testlib.pl [scorch:~/monks] alex% find . -name '*p[ml]' -exec cat {} \; package Demo::SubDemo; $baz = "bletch"; 1; package Demo; $foo = "bar"; 1; #!/usr/bin/perl -wl use Demo; use Demo::SubDemo; print $Demo::foo; print $Demo::SubDemo::baz; [scorch:~/monks] alex% echo $PERL5LIB PERL5LIB: Undefined variable.
Note that nowhere am I asking perl to use "." in @INC. Yet, perl still sees it. I think this is pretty sensible behaviour. Additionally, were I to have POD in any of the Demo modules, I could use perldoc, and it would DTRT as regards '.'.

Am I missing something that coders are using lib this way? I guess with the exception of CGI applications (although I suspect it isn't necessary there, either).

Cheers
dep.

--
Laziness, Impatience, Hubris, and Generosity.

minor reword edit on first line.

Replies are listed 'Best First'.
Re: Utility of "use lib qw{ . }" ?
by Lachesis (Friar) on Jun 23, 2003 at 14:00 UTC
    . will be the last element of @INC which means that all though it will always be checked, it will be the last path checked for any modules.
    if you call
    use lib '.';
    . will be unshifted onto the front of @INC which means it will be checked first so you know that any modules in the current path will take precedence.
      Exactly. I just wanted to follow this up with a demonstration that everyone can try at home:
      perl -le"print for @INC" perl -Mlib=. -le"print for @INC"
      Notice the placement of .

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
Re: Utility of "use lib qw{ . }" ?
by broquaint (Abbot) on Jun 23, 2003 at 13:58 UTC
    Note that nowhere am I asking perl to use "." in @INC. Yet, perl still sees it.
    This is because the default build of perl will include '.' in @INC, although it can configured to leave out '.' and it is also dropped when running in taint mode. See. the @INC section of perlvar for more info.
    HTH

    _________
    broquaint

Re: Utility of "use lib qw{ . }" ?
by gellyfish (Monsignor) on Jun 23, 2003 at 14:18 UTC

    The only time that you might want to use use lib '.' (and with extreme caution) is when running your program with taint mode (the -T switch) on, when the current directory is removed from @INC - this should be done with caution because it is not alway possible for a program to control what it's current working directory is before requiring or using any modules and in a less secure environment (such as a web server) there is a risk of malicious code in the current directory being substituted for that of a real module. At the very least one should assure oneself that the directories that are likely to be the working directory for your program are not world writeable - of course if it is necessary to add the (assumed) current working directory (probably the directory in which the program resides) to @INC when using '-T' then it is probably best to supply the absolute path rather than '.', which whilst having some postential impact on portability is actually more likely to work under some conditions as well as being more secure.

    /J\
    
Re: Utility of "use lib qw{ . }" ?
by crouchingpenguin (Priest) on Jun 23, 2003 at 14:19 UTC

    Just a small comment on the utility of modifying @INC under apache/mod_perl. I often include directories in the startup file (often named startup.pl) via use libs like:

    use lib qw( /var/www/perl_lib ); ### for example

    This is especially handy when you have a dev apache instance that includes dev copies of your local libs.


    cp
    ----
    "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."
Re: Utility of "use lib qw{ . }" ?
by PodMaster (Abbot) on Jun 23, 2003 at 14:45 UTC
    There is a similar discussion about the implications of relative paths and $ENV{PATH} as it pertains to the shebang at #!$var/bin/perl.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found