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

Re: Use with variable

by Corion (Patriarch)
on Sep 08, 2004 at 13:08 UTC ( [id://389336]=note: print w/replies, xml ) Need Help??


in reply to Use with variable

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

Replies are listed 'Best First'.
Re^2: Use with variable
by tomhukins (Curate) on Sep 08, 2004 at 13:18 UTC
    Module *must* be a bareword
    my $module = "CGI"; require $module;

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

Re^2: Use with variable
by edan (Curate) on Sep 08, 2004 at 13:34 UTC

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

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

      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 ...)

        Do you have something against eval? I think that's the most straightforward solution...

        --
        edan

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-23 20:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found