Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Perl's lib pragma work?

Tags:

perl

I use use lib "./DIR" to grab a library from a folder elsewhere. However, it doesn't seem to work on my server, but it works fine on my local desktop. Any particular reasons?

And one more question, does use lib get propagated within several modules?

Two situations: Say I make a base class that requires a few libraries, but I know that it needs to be extended and the extended class will need to use another library. Can I put the use lib command in the base class? or will I need to put it in every extending class?

Finally, can I just have a use package where package contains a bunch of use lib, will it propagate the use lib statements over to my current module? <-- I don't think so, but asking anyways

like image 653
Tyug Avatar asked Apr 07 '10 14:04

Tyug


2 Answers

The . in your use lib statement means "current working directory" and will only work when your script is run from the right directory. The server's idea of cwd is probably something different (or undefined). Assuming that the library directory is co-located with with script and private to it you want to do something like this instead:

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

A use lib statement affects @INC -- the list of locations perl searches when you use or require a module. It globally affects the current instance of the interpreter. You should really only put use lib statements in scripts, not in modules.

In principle, you could have a package MyLibs that consisted of a bunch of use lib statements and then use MyLibs before using any of the packages in those locations, but I wouldn't recommend it.

like image 56
Michael Carman Avatar answered Oct 19 '22 09:10

Michael Carman


There's no way to know why it isn't working on your server without more information. In particular, check your server's error logs, and dump @INC somewhere if necessary, and compare that to your actual library paths.

use lib modifies @INC, which is global, so as long as you execute your use lib before other packages try to include stuff, it will work and all other packages will see the new include paths.

For more on @INC, see its entry in perlvar.

like image 28
friedo Avatar answered Oct 19 '22 07:10

friedo