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

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

I'm attempting to create a module which loads a Perl module by name. So a database has a list of modules by a more friendly name, and a script calls the loader function to load the actual module and return an object. Example of what I'm trying to do: sub load($module) { if ($nett_load_hash$module == 0) { use vars qw($module); $nett_load_hash$module = 1; } return $module->new(); } Obviously, it doesn't work at the 'return' part. How would I do this? PHP works nicely by loading "modules" ($module = new $name). Anything like this in Perl?

Originally posted as a Categorized Question.

  • Comment on Loading modules by external data (i.e., database)

Replies are listed 'Best First'.
Re: Loading modules by external data (i.e., database)
by Corion (Patriarch) on Jun 04, 2000 at 14:22 UTC

    In my (frozen) file information display tool (a souped up magic program), I used dynamic loading for modules more or less like this :

    $handler = eval( "use $modulename; return $modulename->new;" );
    (I'm in a hurry, if this one dosen't work, tell me and I'll look up the actual code :))

      That works just fine: $class = "my::Module"; $obj = eval("use $class; return $class->new;"); is the same as use my::Module; $obj = my::Module->new; But why does this work: use my::Module; $obj = my::Module->new($args_ref); When this doesn't: $class = "my::Module"; $obj = eval("use $class; $class->new($args_ref);"); $obj comes back undefined. Any ideas?
Re: Loading modules by external data (i.e., database)
by chromatic (Archbishop) on Jun 06, 2000 at 03:31 UTC