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

I want to use external libraries made by myself. Those modules are located with the main script. However, we know it is unsave to just say:

use MyModule;
, because the "./" in @INC is with the location where the script is runned.

Moreover, I don't want to copy those modules to an fixed location. So, how can I get the position?

Replies are listed 'Best First'.
Re: Get module's location savely?
by cdarke (Prior) on Nov 12, 2009 at 08:20 UTC
    If the library directory is not fixed then you have to tell perl somehow. One way is to set the environment variable PERL5LIB to the directory, another is to use your own environment variable, for example:
    use lib $ENV{'MY_ENV_VAR'}; use MyModule;
Re: Get module's location savely?
by happy.barney (Friar) on Nov 12, 2009 at 11:28 UTC
Re: Get module's location savely?
by Bloodnok (Vicar) on Nov 12, 2009 at 11:53 UTC
    The recommended/standard way is to use, as cdarke says, the PERL5LIB ( or PERLLIB if your perl is old enough) environment variable - well that's how alternative location(s) are specified to perl in a code independent manner.

    How ever, if, as your question implies, you need to know from whence a library was loaded, use the global hash %INC - see Predefined Names for further details.

    A user level that continues to overstate my experience :-))
Re: Get module's location savely?
by dsheroh (Monsignor) on Nov 12, 2009 at 11:51 UTC
    use FindBin; use lib "$FindBin::Bin/lib";
    in the main script will add the lib/ subdirectory of the its installation directory to @INC. (Usually... There are some corner cases where FindBin will get things wrong, but I've never run into any of them in the wild.)