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

PERL5LIB different than 'use lib'

by saintmike (Vicar)
on May 07, 2009 at 01:52 UTC ( #762455=perlquestion: print w/replies, xml ) Need Help??
saintmike has asked for the wisdom of the Perl Monks concerning the following question:

If I want to install a CPAN module in a location that doesn't interfere with my package manager, I typically use
perl Makefile.PL INSTALLBASE=/home/me/perl-lib
and then use

PERL5LIB=/home/me/perl-lib/lib/perl5 perl-script
to run scripts using that new hierarchy of perl libs.

However, instead of using an ENV variable, I'd like to use code like

use lib '/home/me/perl-lib/lib/perl5';
which, unfortunately, and contrary to the explanation in perlrun, isn't the same. For example, if you have architecture-dependent code that ends up in something like /home/me/perl-lib/lib/perl5/i686-linux-thread-multi, then PERL5LIB set to /home/me/perl-lib/lib/perl5 will find it, while 'use lib' won't.


  1. What are you doing to keep Perl modules installed by your package manager and CPAN modules separate?
  2. Is INSTALLBASE still the recommended way of installing modules in a different than the standard location with MakeMaker?
  3. Is there a directive in Perl that does what PERL5LIB does (without using PERL5LIB)?
Thanks for any insight.

Replies are listed 'Best First'.
Re: PERL5LIB different than 'use lib'
by toolic (Bishop) on May 07, 2009 at 02:34 UTC
    This is a quote from lib:
    For each directory in LIST (called $dir here) the lib module also checks to see if a directory called $dir/$archname/auto exists. If so the $dir/$archname directory is assumed to be a corresponding architecture specific directory and is added to @INC in front of $dir.
    If there is a subdirectory named "auto", then use lib seems to behave more like PERL5LIB in that it will also search in the architecture-dependent subdirectory for your module.
      also checks to see if a directory called $dir/$archname/auto exists...

      Both PERL5LIB and use lib should behave the same in this respect. For example

      $ mkdir -p testlib/x86_64-linux-gnu-thread-multi/auto $ PERL5LIB=testlib perl -e'print join"\n",@INC' testlib/x86_64-linux-gnu-thread-multi testlib /etc/perl /usr/local/lib/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl $ perl -Mlib=testlib -e'print join"\n",@INC' testlib/x86_64-linux-gnu-thread-multi testlib /etc/perl /usr/local/lib/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl

      (the same applies to -Itestlib, btw)

        You'd think they should, but they don't. Try the following:
        $ PERL5LIB=/no/such/dir perl -le 'print for @INC' > foo $ PERL5LIB= perl -le 'use lib "/no/such/dir"; print for @INC' > bar $ diff -u foo bar--- foo 2009-05-07 16:24:12.000000000 -0700 +++ bar 2009-05-07 16:24:32.000000000 -0700 @@ -1,12 +1,3 @@ -/no/such/dir/5.8.6/x86_64-linux-thread-multi -/no/such/dir/5.8.6 -/no/such/dir/x86_64-linux-thread-multi -/no/such/dir/5.8.5 -/no/such/dir/5.8.4 -/no/such/dir/5.8.3 -/no/such/dir/5.8.2 -/no/such/dir/5.8.1 -/no/such/dir/5.8.0 /no/such/dir /usr/lib64/perl5/5.8.6/x86_64-linux-thread-multi /usr/lib/perl5/5.8.6
        And now we see the difference that saintmike was talking about. If you put stuff in $ENV{PERL5LIB}, it adds a list of internal hardcoded architecture specific directories to look in whether or not those directories exist, and regardless of the contents! By contrast use lib adds subdirectories based on a heuristic about their contents.
Re: PERL5LIB different than 'use lib'
by almut (Canon) on May 07, 2009 at 02:22 UTC

    Just out of curiosity... you could compare the strace output you're getting with PERL5LIB against the corresponding attempt with "use lib ...". If the one works and the other doesn't, there presumably is some difference in which places are being searched...

    $ strace -efile perl -MSome::Module -e1 2>&1 | grep Module

    (where Some::Module of course is a module that exhibits the difference)

Re: PERL5LIB different than 'use lib'
by Anonymous Monk on May 07, 2009 at 03:23 UTC
      See my response above to almut. While your Perl doesn't show it, mine shows a hardcoded difference in the behavior of PERL5LIB and use lib. And therefore there is every reason to believe saintmike's description.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://762455]
Approved by ww
Front-paged by tye
and a soft breeze sighs...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2017-06-27 17:56 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (611 votes). Check out past polls.