Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: loading modules at runtime

by ikegami (Patriarch)
on Oct 14, 2006 at 21:16 UTC ( [id://578322]=note: print w/replies, xml ) Need Help??


in reply to loading modules at runtime

I presume the module name is in a variable.
I also presume you have no need to import symbols.

my $plugin = ...; my $pm = $plugin; $pm =~ s{::}{/}g; $pm .= '.pm'; eval { require $pm } or die("Unable to load plugin $plugin: $@\n"); # Example static method call. $plugin->method(); # Example function call. my $func = do { no strict 'refs'; *{"${plugin}::func"}{CODE} } or die("Undefined function func in plugin $plugin\n"); $func->();

Replies are listed 'Best First'.
Re^2: loading modules at runtime
by blazar (Canon) on Oct 14, 2006 at 21:52 UTC
    $pm =~ s{::}{/}g; $pm .= '.pm'; eval { require $pm } or die("Unable to load plugin $plugin: $@\n");

    Just one question, I'm all against string eval most of the time, but in this case would it be much different to avoid the self-made $plugin "conversion" and do

    eval qq{ require $plugin } or die("Unable to load plugin $plugin: $@\n");

    instead?

      Your call. My method is documented in require (see below) and used by core module if.

      If EXPR is a bareword, the require assumes a ".pm" extension and replaces "::" with "/" in the filename for you, to make it easy to load standard modules

Re^2: loading modules at runtime
by phaylon (Curate) on Oct 16, 2006 at 13:05 UTC
    Nowadays I like to use Class-Inspector:
    use Class::Inspector;
    
    my $module = 'Foo::Bar';
    require Class::Inspector->filename($module);

    Ordinary morality is for ordinary people. -- Aleister Crowley
Re^2: loading modules at runtime
by zer (Deacon) on Oct 17, 2006 at 00:20 UTC
    yes it is typed in by the user during runtime

Log In?
Username:
Password:

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

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

    No recent polls found