Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by theatrus (Initiate)
on Jun 04, 2000 at 09:45 UTC ( [id://16270]=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://16270]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-19 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found