Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

use lib problem.

by tanger (Scribe)
on Jun 01, 2007 at 06:39 UTC ( [id://618649]=perlquestion: print w/replies, xml ) Need Help??

tanger has asked for the wisdom of the Perl Monks concerning the following question:

Hey, not sure why I'm having this problem. I can only speculate that its because I'm not referencing the scalar as a LIST.

This works and loads the module properly:
use lib '/home/tanger/www/mods'; use Email::Valid;
This doesn't work:
my $mod_path = '/home/tanger/www/mods'; use lib $mod_path; use Email::Valid;
I've been trying to figure out how to do this correctly for the last 2 hrs and had no luck or progress. If anyone knows if this is even possible , please let me know :)

Update: I just read NEVER a list in a scalar context -- PLEASE!, so I tried this:
my @mod_path = '/home/ripenapp/perl_mods'; use lib @mod_path; use Email::Valid;
But this does not work as well... Thanks

Replies are listed 'Best First'.
Re: use lib problem.
by vrk (Chaplain) on Jun 01, 2007 at 07:10 UTC

    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";

      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;
Re: use lib problem.
by GrandFather (Saint) on Jun 01, 2007 at 07:07 UTC

    It's because the use statements are "compiled" before the rest of the code. Why do you want to do it that way?


    DWIM is Perl's answer to Gödel
Re: use lib problem.
by syphilis (Archbishop) on Jun 01, 2007 at 07:14 UTC
    Hi,
    Seems an odd thing to be doing - but it should work if you do it in a BEGIN{} block:
    BEGIN { my $mod_path = '/home/tanger/www/mods'; use lib $mod_path; }; use Email::Valid;
    Cheers,
    Rob

      This was covered recently. Your solution doesn't work because the assignment is still executed after the use. The use statement finishes compiling before the BEGIN statement, so it's executed first. A fix:

      my $mod_path; BEGIN { $mod_path = '/home/tanger/www/mods'; } use lib $mod_path; use Email::Valid;

      Follow the above link for alternative solutions.

Re: use lib problem.
by Tomte (Priest) on Jun 01, 2007 at 07:21 UTC

    This is because use lib $mod_path is evaluated during compile time (it's equivalent to BEGIN { require lib; lib->import($mod_path) ; } - but the assignement to $mod_path is done during runtime - in effect you are calling lib->import with an empty list as argument

    I modified my lib.pm to produce a little message every time import is called:

    my @mod_path = '/home/ripenapp/perl_mods'; use lib @mod_path; use Email::Valid; __END__ tomte@librics-tomte ~/scratch/test $ perl test.pl Importing:

    Your first version:

    use lib '/home/tanger/www/mods'; use Email::Valid; __END__ tomte@librics-tomte ~/scratch/test $ perl test.pl Importing: /home/tanger/www/mods

    regards,
    tomte


    An intellectual is someone whose mind watches itself.
    -- Albert Camus

Re: use lib problem.
by tanger (Scribe) on Jun 01, 2007 at 07:33 UTC
    Hey,

    thank you all for your replies

    It does seem a little weird. The part where I'm trying to execute my code is inside a sub routine that validates form fields. I only want to load Email::Valid if and only it needs to validate a e-mail field.

    Anyhow, the code above worked out...however I do not wish to load it at runtime if I don't have to? Not sure if this is a good idea or not, if not , I'll just load it at runtime.

    tanger
      If you want to conditionally load modules at runtime, you want require:
      sub validate_fields { ... if($q->param(mail)) { my $mod = '/home/tanger/www/mods'; unshift @INC, $mod unless $INC[0] eq $mod; require Email::Valid; # to include the code import Email::Valid; # to import symbols into your names +pace ... } ... }

      Read perlmod - I've done that again yesterday. It is good to re-read manual pages, since there are always things in them which escape your attention in the first place, but get important when your knowledge of perl deepens. Read the docs, and often! :-)

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Well, your use statements will get executed at compile-time whether the condition is true or not. To conditionally load a module at runtime, you can do this:
      if ( $want_to_check_email ) { unshift @INC, $mod_path; require Email::Valid; Email::Valid->import; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://618649]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-19 15:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found