Gossamer Forum
Home : General : Perl Programming :

use lib $var NOT WORKING on win

Quote Reply
use lib $var NOT WORKING on win
Hi

I have

$var = '/path/to/dir'; #top of the file before anything else

then in a subroutine I call a local module ...

sub (

use lib $var;

use MODULENAME;

)

On windows use lib $var; does not work. But it works if I put : use lib '/path/to/dir';

Why the $var is not working in use lib on windows?
Quote Reply
Re: [robyone] use lib $var NOT WORKING on win In reply to
Why are you defining a variable?...can you not insert the path manually?...eg

use lib '/path/to/dir';

You can add use lib to the top of your script also.

Last edited by:

Paul: Jan 16, 2003, 7:28 AM
Quote Reply
Re: [Paul] use lib $var NOT WORKING on win In reply to
I use that module only for that routine.
Quote Reply
Re: [robyone] use lib $var NOT WORKING on win In reply to
That's ok, you can still add the use lib line at the top manually.

If you really do want to load it dynamically (which I honestly don't think you need to do) you will either have to use a BEGIN block to define your variable or call the import routine in the lib module.

Last edited by:

Paul: Jan 16, 2003, 7:34 AM
Quote Reply
Re: [Paul] use lib $var NOT WORKING on win In reply to
if I put use lib '/my/own/module' at the beginning of the file will this cause problems to other modules used like

CGI, LWP, etc. ?

Last edited by:

robyone: Jan 16, 2003, 7:39 AM
Quote Reply
Re: [robyone] use lib $var NOT WORKING on win In reply to
Nope you can load custom modules or default modules.
Quote Reply
Re: [Paul] use lib $var NOT WORKING on win In reply to
do you mean there are no problems?

i can't get how it works with modules. sorry
Quote Reply
Re: [robyone] use lib $var NOT WORKING on win In reply to
Yeah it should work fine. It is common practice.

What happens, or what errors do you see?...or did you just mean you didn't understand how it works rather than you can't get it to work?

Last edited by:

Paul: Jan 16, 2003, 7:57 AM
Quote Reply
Re: [Paul] use lib $var NOT WORKING on win In reply to
Ok I noticed that it will be addded to @INC

Not override @INC (AND OTHER LIBS)

Thanks,
Quote Reply
Re: [robyone] use lib $var NOT WORKING on win In reply to
Yep @INC keeps track of perl's libraries, the only reason you need use lib is to tell perl where your own custom libraries are. Perl scans its lib tree for standard modules but it would be impractical for it to do that for custom modules as it wouldn't know where to look so performace would be really bad, so that's why you "use lib"
Quote Reply
Re: [robyone] use lib $var NOT WORKING on win In reply to
Quote:
sub (

use lib $var;

use MODULENAME;

)

Perl doesn't do what you think it does here. All use statements happen at compile time. Have a look at:

http://www.perldoc.com/....0/pod/func/use.html

for more info.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] use lib $var NOT WORKING on win In reply to
I simply added use lib at the top of the script and then the lib has been added to @INC together with other libs ;) Perfect.

Thanks.
Quote Reply
Re: [robyone] use lib $var NOT WORKING on win In reply to
Yes, but:

sub {
use Module;
}

does not load Module only when the sub is called, the Module will automatically be loaded every time. You need to read about the difference between use and require.

Cheers,

Alex
--
Gossamer Threads Inc.