Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Utility of "use lib qw{ . }" ?

by deprecated (Priest)
on Jun 23, 2003 at 13:50 UTC ( #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% ./ bar bletch [scorch:~/monks] alex% find . . ./Demo ./Demo/ ./ ./ [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).


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 .


      (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.


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.

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 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.

    "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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://268155]
Approved by broquaint
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2017-06-26 04:35 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (572 votes). Check out past polls.