Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

module

perl

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?

like image 367
Frew Schmidt Avatar asked Apr 25 '09 00:04

Frew Schmidt


People also ask

What is use lib in Perl?

It is typically used to add extra directories to Perl's search path so that later do, require, and use statements will find library files that aren't located in Perl's default search path.

How do I import a Perl file into another Perl file?

if you use “use” your file should be named with a . pm extension and placed in the same folder as the main perl script: use Filename; or you can use the “use lib” pragma to add modules to the @INC array and put them in any folder you want as long as you tell “lib” where you put your modules.


1 Answers

The standard FindBin module does what you want.

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

perldoc FindBin for more.

like image 69
Michael Cramer Avatar answered Oct 13 '22 02:10

Michael Cramer