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

Re: How to dynamically load modules

by ikegami (Patriarch)
on May 03, 2005 at 15:51 UTC ( [id://453656]=note: print w/replies, xml ) Need Help??


in reply to How to dynamically load modules

You can do

$mod = 'My::Module1'; eval "use $mod";

which is the same as

$mod = 'My::Module1'; ($mod_file = $mod) =~ s{::}{/}g; $mod_file .= '.pm'; require $mod_file; $mod->import if $mod->can('import');

The first two snippet imports the symbols if the module uses Exporter or similar. However, instead of importing, it be safer to do

$mod = 'My::Module1'; eval "require $mod"; $mod->function();

The last snippet requires that function accept an extra initial argument.

Log In?
Username:
Password:

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

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

    No recent polls found