How To Pass PERL Library Paths From The Environment

You can have PERL look in different places for libraries and modules using a number of methods. If you find yourself using a custom PERL module repository, you can make sure your PERL programs always reference it without touching the code.

What we’re doing in a nutshell is telling PERL to push values on to the @INC array before loading any modules. You can do this on the command line, in your PERL code or with the environment variable PERL5LIB.

PERL5LIB can contain more than one value. Just set it in you .bashrc file or wherever you see fit. This method works in bash:

export PERL5LIB=/first/path/to/libs"${PERL5LIB:+:$PERL5LIB}"
export PERL5LIB=/second/path/to/libs"${PERL5LIB:+:$PERL5LIB}"
export PERL5LIB=/third/path/to/libs"${PERL5LIB:+:$PERL5LIB}"

You can check what PERL is going to use by printing out the contents of @INC. You can print an array without a loop using join() as I blogged about before:

perl -e 'print join "\n", @INC;'

Let’s put it together:
~$ perl -e 'print join "\n", @INC;'
/etc/perl
/usr/local/lib/perl/5.10.0
/usr/local/share/perl/5.10.0
/usr/lib/perl5
/usr/share/perl5
/usr/lib/perl/5.10
/usr/share/perl/5.10
/usr/local/lib/site_perl
.

~$ env|grep -i perl
~$

~$ export PERL5LIB=/first/path/to/libs"${PERL5LIB:+:$PERL5LIB}"
~$ export PERL5LIB=/second/path/to/libs"${PERL5LIB:+:$PERL5LIB}"
~$ export PERL5LIB=/third/path/to/libs"${PERL5LIB:+:$PERL5LIB}"

~$ env|grep -i perl
PERL5LIB=/third/path/to/libs:/second/path/to/libs:/first/path/to/libs

~$ perl -e 'print join "\n", @INC;'
/third/path/to/libs
/second/path/to/libs
/first/path/to/libs
/etc/perl
/usr/local/lib/perl/5.10.0
/usr/local/share/perl/5.10.0
/usr/lib/perl5
/usr/share/perl5
/usr/lib/perl/5.10
/usr/share/perl/5.10
/usr/local/lib/site_perl
.

And there it is. Your PERL apps will look in those locations starting from the top.

Did you find this post useful or have questions or comments? Please let me know!

This entry was posted in How Tos, perl, Programming. Bookmark the permalink.

3 Responses to How To Pass PERL Library Paths From The Environment

  1. Dan says:

    Fantastic – simple and it works. Thanks!

  2. Luuwen says:

    I created a new Perl Module (pm file) using CPP2XS shared object (*.so). How can the Perl code find the shared object path? Any idea?

    t/test.t .. Can’t load ‘/home/lday/my_work/perlxs/CPP2XS/test/My-Mod-0.01/blib/arch/auto/My/Mod/Mod.so’ for module My::Mod: /home/lday/my_work/perlxs/CPP2XS/test/My-Mod-0.01/blib/arch/auto/My/Mod/Mod.so: undefined symbol: _Z11fdbx_to_csviPPcPm at /usr/lib64/perl5/DynaLoader.pm line 200.
    at /home/lday/my_work/perlxs/CPP2XS/test/My-Mod-0.01/blib/lib/My/Mod.pm line 10.

  3. admin says:

    @Luuwen,

    Your problem looks like a library problem, not a problem with PERL finding the module. You can see that PERL did find the Mod.pm module. I recommend running strace against it to see where it is looking.

Leave a Reply

Your email address will not be published. Required fields are marked *