How do I load a module at runtime in Perl?

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;

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

Tags:

Perl

Module