Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: use lib problem.

by w-ber (Hermit)
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";


Comment on Re: use lib problem.
Select or Download Code
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/script.pl
    then you can also use following code
    BEGIN { if ($0 =~ m/^(.*?)[\/\\]([^\/\\]+)$/) { $runtimedir = $1; $program = $2; } } use strict; use lib $main::runtimedir;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://618656]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-10-02 04:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (49 votes), past polls