http://www.perlmonks.org?node_id=453642

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks Is there a way to dynamically loading a module. It is like I have a couple of modules having the same functions names in it but doing a totally different job. Now the requirement is that I need to dynamically load the module I need without using a lot of if then else loop. Can any one suggest a way how to do this. Well I have to parse this based on a command line option. Regards Sid

Replies are listed 'Best First'.
Re: How to dynamically load modules
by lachoy (Parson) on May 03, 2005 at 15:46 UTC

    The CPAN module Class::Factory encapsulates all this for you -- just tell your factory about the different implementations and type associated with each, then pass that type to the factory when you want a new object.

    Chris
    M-x auto-bs-mode

Re: How to dynamically load modules
by ikegami (Patriarch) on May 03, 2005 at 15:51 UTC

    You can do

    $mod = 'My::Module1'; eval "use $mod";

    which is the same as

    $mod = 'My::Module1'; ($mod_file = $mod) =~ s{::}{/}g; $mod_file .= '.pm'; require $mod_file; $mod->import if $mod->can('import');

    The first two snippet imports the symbols if the module uses Exporter or similar. However, instead of importing, it be safer to do

    $mod = 'My::Module1'; eval "require $mod"; $mod->function();

    The last snippet requires that function accept an extra initial argument.

Re: How to dynamically load modules
by borisz (Canon) on May 03, 2005 at 15:47 UTC
    I did not understand your description. My best bet is:
    eval "require $your_class_name";
    Boris
Re: How to dynamically load modules
by Joost (Canon) on May 03, 2005 at 15:47 UTC
Re: How to dynamically load modules
by tlm (Prior) on May 03, 2005 at 16:06 UTC

    Just require the modules and don't import the functions that have the same name; invoke them using their fully qualified names. E.g. if both Module1 and Module2 have foo:

    require Module1; require Module2; Module1::foo(); Module2::foo();
    With eval, the above translates to:
    my $mod = some_condition() ? 'Module1' : 'Module2'; eval "require $mod; ${mod}::foo()";

    TIMTOWTDI: I find the problem description admits too many different interpretations, leading to entirely different solutions; anyway, here's another possible form the solution could take:

    use strict; use warnings; my $mod = shift; require "$mod.pm"; eval "$mod->import(); 1" or die; # modules all export 'foo' by defaul +t # eval "$mod->import( 'foo' ); 1" or die; foo();
    Use the commented-out eval instead if you want to limit the functions that are imported.

    the lowliest monk

Re: How to dynamically load modules
by jeteve (Pilgrim) on May 03, 2005 at 15:48 UTC
    Hi . As soon as I understood, you should need something like this: You got your module name into $modName . At runtime, you can do
    eval "require $modName"; if( $@ ){ die("Cannot load $modName : $@"); }