Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

store module names in an array

by Farhad (Initiate)
on Oct 27, 2009 at 02:44 UTC ( [id://803365]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all. Here's the code that is throwing a syntax error. I believe that the package name must be a bareword and not stored in a variable. "require $dbf" works, but how can I do this using use?

#!/usr/bin/perl use strict; use warnings; my @dbf=qw( rates ); foreach my $dbf (@dbf){ use $dbf; }

Changing the above to require $dbf.pm works. But how do I make it work with use? thanks.

Replies are listed 'Best First'.
Re: store module names in an array
by kyle (Abbot) on Oct 27, 2009 at 03:34 UTC

    You could wrap it in a string eval like so:

    for my $dbh ( @dbf ) { die $@ if ! eval "use $dbf; 1"; }

    I might be inclined to put the whole thing in a BEGIN block.

    BEGIN { my @dbf = ...; for my $dbf ( @dbf ) { require $dbf; $dbf->import(); } }

    Either way, there's a big security risk if @dbf comes from an untrusted source, but I think the risk is bigger with eval. It might be useful to read the documentation for use and require to see how close they are to what you want to do.

Re: store module names in an array
by FloydATC (Deacon) on Oct 27, 2009 at 05:55 UTC
    The trick is that 'use' loads the module at compile time, 'require' does so at runtime. Simply put, wrapping your loop in a BEGIN block would execute it at compile time, and 'use' should work as expected.

    -- Time flies when you don't know what you're doing
      On and off, I have been trying to understand the differences between use and require for two years now. I'm not kidding!

      As you said, the run time and compile time execution of each is the only difference, and the only difference i've understood.

      Since I cannot load modules in the BEGIN block due to programming logic here, i'll try the require statement to load my modules as needed and see how it goes.

      Each Module is the name of a table in a database i need to load and execute. I don't want to load 100 modules everytime. Just those that I need for a specific day. That's why i'm trying to manage the module names in an array (or hash)... Thanks all for your replies.

Re: store module names in an array
by bichonfrise74 (Vicar) on Oct 27, 2009 at 03:02 UTC
    It looks like you have a list of modules that you want loaded all the time and you are tired of constantly typing it. If this is the case, then you might want to look at Toolkit.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-24 01:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found