Krishna P Krishna P - 5 months ago 12
Perl Question

Include perl module from a different location

my problem is as follows

  1. I have the lib in location ./abc/def/lib

  2. I am using it in path ./abc/xyz/lib/tuv/

I am running the code from a different location altogether
which is something like ./abc/xyz/blah1/blah2/../../ . I'm running the code here.

I have created env params

setenv def ./abc/def
setenv xyz ./abc/xyz

I tried the below solutions

  1. Begin { push(@INC, $ENV{def}."/lib" ) }

    Along with the providing the path for other libraries i used
    use lib $ENV{xyz}."/lib"

    This solution didn't work. I get the error saying the library in ./abc/def/lib
    is not included.

  2. I tried the FindBin

    # for the libs present in ./abc/def/lib
    use FindBin;
    use lib "$FindBin::Bin/../../";
    use mylib::abc;
    use mylib::def;

(I tried giving the path also of root from where to start FindBin)

I tried using different combinations but not sure why it doesn't work.
Can someone point out what i am doing wrong or is my understanding of FindBin wrong.


The FindBin module is the best fit for your needs. The important thing to remember is that $FindBin::Bin refers to the directory that contains the script. So when you refer to it ...

use lib $FindBin::Bin . '/../../etc';

the '/../..' path must describe the path from the script directory to the lib directory.

The other important thing to take into consideration is the error message. It will start with Can't locate Your/Module/ but will then go on to list all the directories in @INC - the library search path. Check this list of directories to make sure it includes the lib path you intended to add.