Making Linux Server Directories More Readable, Add to Perl's @INC Array

by Juliet Kemp

Tip of the Trade: Anyone who has encountered the Perl @INC array in the context of a 'cannot find module' error knows the value of being able to see a list of directories on you Linux server. Here's how to access @INC and find obscurely located modules before they are lost.

You're most likely to have encountered the Perl @INC array in the context of a "cannot find module" error. This is the list of directories Perl searches when looking for a module. To see a list of what's in @INC, try this one-liner:

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

Note that it includes ., the current working directory. In some cases, it also includes /etc/perl, which can be a good location for user-written modules.

If you're using CPAN as root, or installing modules as packages from your distribution, everything will wind up in the Right Place automatically. If you have root access, you can also add your own modules by hand. However, it's not actually possible to change @INC permanently without recompiling, and you will always have root access.

Here are a handful of other ways to make Perl find a module that's installed in a non-standard location.

  • Probably the most common way of doing this is use lib. Put this line somewhere near the top of your script, along with the references to any other modules you're using:
    use lib '/home/juliet/perl/libs';
    then use whichever modules you want from that directory.
  • On the command line, you can use -I, which is good for a one-liner:
    perl -I/home/juliet/perl/libs -e 'code goes here'
    This also works with a script:
    perl -I/home/juliet/perl/libs myscript
    but in that case you're probably better off with use lib.
  • You can also set the PERL5LIB environment variable:
    export	PERL5LIB=/home/juliet/perl/libs:/home/juliet/perl/morelibs
    (bash syntax). Add this to ~/.bashrc to avoid having to reference your library directory in every Perl script you write.

Of course, because this is Perl, there are other ways to do it. Feel free to experiment!

Juliet Kemp has been messing around with Linux systems, for financial reward and otherwise, for about a decade. She is also the author of "Linux System Administration Recipes: A Problem-Solution Approach" (Apress, 2009).

Follow ServerWatch on Twitter

This article was originally published on Monday Feb 22nd 2010
Mobile Site | Full Site