Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: use lib problem.

by vrk (Chaplain)
on Jun 01, 2007 at 07:10 UTC ( #618656=note: print w/replies, xml ) Need Help??

in reply to use lib problem.

The problem is that use calls are evaluated at BEGIN time, while your lexical variable declarations are called after that. Although use lib LIST does not try to import a module, it's still a compile-time pragma, and to my knowledge, those are still evaluated at BEGIN time.

So, I recommend trying this:

my $mod_path; # or @mod_path, if you're so inclined. BEGIN { $mod_path = '/home/ripenapp/perl_mods'; } use lib $mod_path; use Email::Valid;

What this does is ensure that a value is assigned in $mod_path before use lib $mod_path is called -- as use calls are implicitly wrapped in BEGIN blocks (see use and lib) and BEGIN blocks are executed sequentially in the order of definition (see perlmod).

By the way, you do use strict, right? use lib $mod_path ought to complain in your example code, as the variable hasn't been assigned a value before use lib is called...

print "Just Another Perl Adept\n";

Replies are listed 'Best First'.
Re^2: use lib problem.
by sanPerl (Friar) on Jun 01, 2007 at 09:52 UTC
    This is just FYI. If you want to use directory as library on unix, where your script is being called. for e.g.
    >perl /home/abcd/
    then you can also use following code
    BEGIN { if ($0 =~ m/^(.*?)[\/\\]([^\/\\]+)$/) { $runtimedir = $1; $program = $2; } } use strict; use lib $main::runtimedir;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://618656]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2018-04-26 04:18 GMT
Find Nodes?
    Voting Booth?