Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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.

Replies are listed 'Best First'.
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");


        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 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 ioannis (Monsignor) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://578316]
Approved by Arunbear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2018-02-21 13:08 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (279 votes). Check out past polls.