Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Using arbitrary packages

by Sprad (Hermit)
on Jul 12, 2004 at 21:40 UTC ( [id://373742]=perlquestion: print w/replies, xml ) Need Help??

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

I'd like to be able to do this:
use $foo;
where $foo contains some module name. But when I try, I get a syntax error. Is such a thing even possible?

---
A fair fight is a sign of poor planning.

Replies are listed 'Best First'.
Re: Using arbitrary packages
by fergal (Chaplain) on Jul 12, 2004 at 21:49 UTC
    eval "use $foo"; die $@ if $@ # die with error message if it failed
    will do it but it will do it at runtime, whereas a genuine use will do it at compile time. If you do
    BEGIN { my $foo = get_a_module_name(); eval "use $foo"; die $@ if $@ }
    it will happen at compile time, you just need to be careful that $foo has a value at compile time.
Re: Using arbitrary packages
by ysth (Canon) on Jul 12, 2004 at 21:52 UTC
    If $foo is trusted data, you can say eval "use $foo";, but that won't take effect at compile time. Starting with 5.6.2, you can say
    use if 1, $foo;
    but $foo has to already be set at compile time, e.g.
    my $foo; BEGIN { $foo = $ENV{SERIALIZER} || "Storable" } use if 1, $foo;
    but even that IMO is more clearly written as
    BEGIN { eval "use ".($ENV{SERIALIZER}||"Storable"); }
    What exactly are you trying to accomplish? There's probably a better way.
      In a nutshell, I'd like to have a config file that contains module names and function names. I want to read the config file, 'use' all the modules, and then make the function calls as given in the config file.

      It's all trusted data, since it's all being run locally.

      ---
      A fair fight is a sign of poor planning.

        Perhaps you should use
        do $filename;
        and have actual real Perl in the file that looks like
        use Module1; use Module2; function1(); function2();
        it will run faster. No point in inventing your own mini perl parser. Especially if in the future you decide you need to start adding arguments to your functions etc.
      Starting with 5.6.2, you can say
      Its more like starting with perl 5 (if its on CPAN, you can get it from CPAN) :D

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Using arbitrary packages
by duff (Parson) on Jul 13, 2004 at 03:49 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-03-29 05:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found