tags:

views:

65

answers:

2

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

+2  A: 

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.

friedo
+3  A: 

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.

Michael Carman
I used getcwd to figure out where "." is. Just curious to know if it's possible to figure out where the perl script is located? It didn't work when I tried to use FindBin because the lib and bin are not co-located. So here's an example of what I'm trying to do. I run the perl script from say folder1. It has use lib "/abs/path/topack/"; use pack. In pack I do use lib './FOLDER2'; assuming the cwd would be the same. Since "." is pointing to Folder1, and not the path. It is wrong. How do I not use absolute path in pack yet also be able to use .? Is it possible?
Tyug
@Tyug: You can determine where a module was loaded from by inspecting `%INC`. See http://stackoverflow.com/questions/1463414 for a similar question that should get you pointed in the right direction.
Michael Carman
Thank you very much!
Tyug