Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

loading modules at runtime

by zer (Deacon)
on Oct 14, 2006 at 19:51 UTC ( #578316=perlquestion: print w/ replies, xml ) Need Help??
zer has asked for the wisdom of the Perl Monks concerning the following question:

i was wondering if there is a way to load up a module during runtime. I am making a game that allows the user to load bots into the system. The bots take the form of modules so that you can program your own AI.

I tried fiddling with eval and it hasnt been working out. And the use only workes during the begining stage of the execution.

Comment on loading modules at runtime
Re: loading modules at runtime
by Hue-Bond (Priest) on Oct 14, 2006 at 19:55 UTC

    The standard way is:

    require Module; import Module PARAMETERS;

    See use, require.

    --
    David Serrano

      Module->import( PARAMETERS );

      Indirect method invocation has severe and difficult-to-debug ambiguities.

        Module might not have an import.

        Module->import( PARAMETERS ) if Module->can('import');

        This is rather moot, since once usually wants to avoid importing from a dynamically loaded module.

Re: loading modules at runtime
by ikegami (Pope) on Oct 14, 2006 at 21:16 UTC

    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->();
      $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

      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
      yes it is typed in by the user during runtime
Re: loading modules at runtime
by ioannis (Priest) on Oct 16, 2006 at 03:46 UTC
    Once you are thinking about pluggins, you could simplify your code a little with Module::Pluggable .

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (18)
As of 2014-07-23 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (142 votes), past polls