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;
A module can be loaded by calling the use function. #!/usr/bin/perl use Foo; bar( "a" ); blat( "b" ); Notice that we didn't have to fully qualify the package's function names. The use function will export a list of symbols from a module given a few added statements inside a module.
To install Perl modules using CPAN, you need to use the cpan command-line utility. You can either run cpan with arguments from the command-line interface, for example, to install a module (e.g Geo::IP) use the -i flag as shown.
Foo.pm
package Foo;
use strict;
use warnings;
use Exporter qw(import);
our @EXPORT = qw(bar);
sub bar { print "bar(@_)\n" }
1;
script.pl
use strict;
use warnings;
require Foo;
Foo->import('bar');
bar(1, 22, 333);
The easiest way is probably to use a module like Module::Load:
use Module::Load;
load Data::Dumper;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With