Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: module location

by ferrency (Deacon)
on Nov 07, 2003 at 15:24 UTC ( #305319=note: print w/replies, xml ) Need Help??


in reply to module location

"use" happens at compile time, but the $perl_module variable isn't initialized until run time, after the "use" statement has already executed. Actually you can't use a variable package name, but you can require it. Try this:

#!/usr/bin/perl -w use strict; my $perl_module = shift; eval {require $perl_module} or die "Can't find $perl_module!";
Alan

Replies are listed 'Best First'.
Re: Re: module location
by hmerrill (Friar) on Nov 07, 2003 at 15:34 UTC
    I thought you had it, but not I get this error:
    Can't find DBI! at ./test.pl line 4.
    Here is the absolute path to the DBI.pm file:
    /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi/DBI.pm
    and the @INC path shows this in 'perl -V':
    @INC: /usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0 /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0
    What am I missing? Thanks for your help!
      Well, there are a few solutions to this.

      My bug was: require DBI looks for "DBI.pm" because DBI is a bareword; but this does not work if you put the bareword in a variable and require that instead.

      One simple solution is to just:

      ./test.pl DBI.pm
      If you don't like that, you can change the eval to a string eval, which will interpolate the package name into a bareword before require sees it:

      #!/usr/bin/perl -w use strict; my $perl_module = shift; eval "require $perl_module" or die "Can't find $perl_module!";
      This would be the more flexible solution, since it would handle things like ./test.pl DBD::PgSQL (assuming that makes sense... basically, any module with ::'s in it).

      Alan

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2019-12-14 05:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?