tags:

views:

82

answers:

3

Is it possible to load a module at runtime in Perl? I tried the following, but it didn't work. I wrote the following somewhere in the program:

require some_module;
import some_module ("some_func");
some_func;
+1  A: 

Look at this "How to dynamically load modules" and you can also look at [DynaLoader - Automatic Dynamic Loading of Perl Modules] in Programming Perl.

Space
any reason for downvote ?
Space
+7  A: 
# In Foo.pm.
package Foo;

use strict;
use warnings;

use base qw(Exporter);
our @EXPORT = qw(bar);

sub bar { print "bar(@_)\n" }

1;

# In your script.
use strict;
use warnings;

require Foo;
Foo->import('bar');
bar(1, 22, 333);
FM
`package` declaration goes to the top.
daxim
+1  A: 

The easiest way is probably to use a module like Module::Load:

use Module::Load;
load Data::Dumper;
jkramer