Perl Language Loading a module at runtime


require Exporter;

This will ensure that the Exporter module is loaded at runtime if it hasn't already been imported. (See also: perldoc -f require.)

N.B.: Most users should use modules rather than require them. Unlike use, require does not call the module's import method and is executed at runtime, not during the compile.

This way of loading modules is useful if you can't decide what modules you need before runtime, such as with a plugin system:

package My::Module;
my @plugins = qw( One Two );
foreach my $plugin (@plugins) {
    my $module = __PACKAGE__ . "::Plugins::$plugin";
    $module =~ s!::!/!g;
    require "$";

This would try to load My::Package::Plugins::One and My::Package::Plugins::Two. @plugins should of course come from some user input or a config file for this to make sense. Note the substitution operator s!::!/!g that replaces each pair of colons with a slash. This is because you can load modules using the familiar module name syntax from use only if the module name is a bareword. If you pass a string or a variable, it must contain a file name.