urbanspr1nter
urbanspr1nter

Reputation: 1357

FindBin for perl modules that reside in my script's directory

I have a script that uses modules that are external to the standard Perl library and would like for some way to use them. I don't have permissions to install them into the Perl lib directory and was wondering if I could just have these external modules reside in my scripts directory.

I have read about using FindBin but it seems to not work. Am I using it correctly?

Right now I want to use 3 modules I want to use (2 being directories). So lets say my script is in Dir1, then my modules will be in a subdirectory of Dir1 called Dir2.

So assuming FindBin finds Dir1, then all I have to do is this?

use FindBin '$Bin';
use Dir2 "$Bin/Dir2";
use Dir2::SubDir_ofDir2_1::Module1;
use Dir2::Module2;
use Dir2::Module3;

My program seems to run but it doesn't do anything. So I am pretty sure it is not importing the modules correctly.

Thanks

Upvotes: 4

Views: 6819

Answers (1)

JB.
JB.

Reputation: 42114

The proper way to do it would more likely be either:

use lib "$FindBin::Bin/Dir2";
use SubDir::Module1;

or:

use lib $FindBin::Bin;
use Dir2::Subdir::Module;

Both would find the files, behavior would then depend on whether the modules declare themselves as inside package Dir2 or not.

Check out FindBin and lib's documentation.

Upvotes: 7

Related Questions