Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Use with variable

by Anonymous Monk
on Sep 08, 2004 at 13:05 UTC ( #389334=perlquestion: print w/replies, xml ) Need Help??

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

is it possible to use command with variable name in a program
for eg.,
 $module = CGI; use $module;
If i use the above said method i am getting error as syntax error at ... line ..., near "use $module"
how can i do this

Replies are listed 'Best First'.
Re: Use with variable
by borisz (Canon) on Sep 08, 2004 at 13:11 UTC
    Yes, maybe you need to call import yourself.
    $mod = "Digest::MD5"; eval "require $mod";

      If you're already going to eval, might as well just do:

      my $mod = "Digest::MD5"; eval "use $mod";

      and avoid calling import yourself...

      And don't forget to check $@...


        Actually, you don't need to interpolate the variable, and it's much safer not to:

        my $module = "Digest::MD5"; eval 'use $module'; # note single quotes

        That's because use $var actually works. You just have to somehow populate $var before the compile-time effect of use takes place. A BEGIN block lets you do that. Silly example:

        my $module; BEGIN { $module = "Digest::MD5"; } use $module;

        Note that the snippets of course aren't equivalent — the first loads the module at runtime, the second loads it at compile time.

        Makeshifts last the longest.

Re: Use with variable
by Corion (Pope) on Sep 08, 2004 at 13:08 UTC

    perldoc -f use tells you the following:

    Imports some semantics into the current package from the named module, generally by aliasing certain subroutine or variable names into your package. It is exactly equivalent to
    BEGIN { require Module; import Module LIST; }
    except that Module *must* be a bareword.

    So, you can easily do it like the following:

    my $module = "CGI"; $module->require; $module->import;

    Update: Fixed the ugly bug I introduced. Thanks to tomhukins for spotting it.

    Update 2: I should test before I promise broken things as fixed. See my reply to edan further down

      Module *must* be a bareword
      my $module = "CGI"; require $module;

      That's an interesting bareword you use. ;-)

      my $module = "CGI"; $module->require; $module->import;

      Can't locate object method "require" via package "CGI" (perhaps you forgot to load "CGI"?) at line 2.

        That's what I get for not testing my stuff :-((

        So it seems the only actually working way is the ugly way of converting the module name to the file name and then requiring that file :

        my $module = "CGI"; my $filename = $module; $filename =~ s!::!/!g; $filename .= ".pm"; require $filename or die "$module did not return a true value"; $module->import;

        (this time tested it ...)

Re: Use with variable
by radiantmatrix (Parson) on Sep 08, 2004 at 14:36 UTC
    Others have answered (the eval route is the way I'd go); but I have a question. Was your query merely curiosity, or have you actually found a use for this technique?

    I've never encountered a problem where this was the solution, and I'm just curious from a "hey, that's really unusual" point-of-view. Would you mind sharing more about what you're doing?

    $me = rand($hacker{perl});

      It's easy to think of applications for this technique. Any sort of "plug-in" or "load-on-request" situation. Or "drivers", for instance. Ever use DBI? Notice how you only have to "use DBI;" and then the appropriate "DBD::*" module is loaded according to the datasource in your DBI->connect() call? That magic is done by dynamically loading a module that is contained in, yes, you guessed it, a variable! Actually, the eval method is used there:

      # --- load the code my $driver_class = "DBD::$driver"; eval qq{package # hide from PAUSE DBI::_firesafe; # just in case require $driver_class; # load the driver }; if ($@) { #....

        Hey, neat. I've never written anything that could benefit from plugins, so I never encountered the need for that before.

        I definitely need to file that one away for later. Thanks!

        $me = rand($hacker{perl});

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2021-11-28 05:36 GMT
Find Nodes?
    Voting Booth?

    No recent polls found