patrykf
patrykf

Reputation: 449

How to include perl modules while started from other directory?

I am writing some perl script and I want to include module. Everything is okay when I am in the same directory that my script.pl is. But when I try to start my script from other directory it says it cant locate my module. My includes looks like this:

use Functions qw(translateWord sendHelp);

and the file with module is called Functions.. I tried something like this:

use lib '..';

but it failed too.. I also tried:

use Cwd 'abs_path';
BEGIN {
    my $dir = abs_path($0);
    use lib "$dir";
}

but again it failed.. I also tried this:

use Cwd 'abs_path';
my $dir = abs_path($0);
use lib $dir;

and still fail.. I am new to Perl.

Thanks in advance!

Upvotes: 1

Views: 82

Answers (2)

Gilles Quénot
Gilles Quénot

Reputation: 185680

Try this :

BEGIN{
    unshift @INC, '/FULL/PATH/TO/DIR/OF/YOUR/MODULE';
}

Upvotes: 0

Sobrique
Sobrique

Reputation: 53498

The canonical way of accomplishing this is with 'use lib'. Using a lib of .. is not ideal though, because it's relative to the current working directory when you invoke the script.

The way to accomplish this is with FindBin.

E.g.

use FindBin;

use lib $FindBin::Bin."/../";

To traverse up a directory level from the 'base location' of your script.

Upvotes: 3

Related Questions