Question

How do I use a Perl module from a relative location?

I have a dir called foo, and in that I have lib and bin. The scripts in bin need stuff in lib. I do something like this:

#!perl
use strict;
use warnings;
use lib '../lib';
use Foo; # <-- comes from lib

But that means I have to be in the bin dir to run the script. Surely there is a better way. What's the Right Way to do this?

 45  41026  45
1 Jan 1970

Solution

 56

The standard FindBin module does what you want.

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

perldoc FindBin for more.

2009-04-25

Solution

 5

Parse out the complete path to your .pl via __FILE__ and and tack the ../lib on the end or pop off the last element of split(/\//,__FILE__) and add /lib to that.

2009-04-25