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

Good Day PerlMonks

I have written some perl modules to perform some functions and put them all in a directory. At the moment, every module has something like the following in it...

use lib 'C:\My\Module\Directory'; use myModule1 qw(:All); use myModule2 qw(:All); ...

Unfortunately, if I move the folder then the modules can't use each other because they suddenly don't know where they are. While I am happy to update the folder locations by modifying the module files every time I move them, it is not elegant and prone to breakage, can't be sent to other people, etc...

I was wondering if there was a more elegant solution to this problem than specifying a directory in every file. Is there a better way of organising these module files?

Replies are listed 'Best First'.
Re: Accesing Module directory (use lib goes in scripts)
by Anonymous Monk on Apr 30, 2015 at 06:31 UTC

    At the moment, every module has something like the following in it... use lib 'C:\My\Module\Directory';

    This line  use lib 'C:\My\Module\Directory'; goes in your program/script, not in your modules

    you can also set these env vars in your shell or perlrun#PERL5OPT, PERLLIB, PERL5LIB...

    or programmatically using sitecustomize if you have usesitecustomize

Re: Accesing Module directory
by vinoth.ree (Monsignor) on Apr 30, 2015 at 08:29 UTC

    Set the environment variable PERL5LIB

    Perl will look for modules in the directories specified in PERL5LIB environment variable before looking in the standard library and current directory, so you can set this variable to locate your modules.

    The syntax is the same you use for the PATH environment variables, so you separate the directories with colons on unix and with a semicolon on Windows.

    Example:
    export PERL5LIB=/home/foobar/code

    You can add this to the ~/.bashrc to make it always available when you log-in.

    On Windows you can set the same in the cmd command window by typing

    set PERL5LIB = c:\path\to\dir

    All is well. I learn by answering your questions...
Re: Accesing Module directory
by ns550 (Novice) on May 11, 2015 at 06:43 UTC

    Good Day Perl Monks

    use lib 'C:\My\Module\Directory';

    Investigation of the state of the @INC variable in the module environment has revealed that perl automatically puts the use lib search path into module's @INC array. Therefore I don't need to repeat these use lib commands in my module files.

    Thanks for your help