Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Using objects where you don't know the module's name until run time.

by Anonymous Monk
on Nov 07, 2000 at 03:17 UTC ( [id://40278]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (object-oriented programming)

I'm trying to build a wrapper class to hold one of an arbitrary number of "plug-in" modules. My motive here is to load the module, and then pass calls from the outside world to that module, failing gracefully if it can't perform the function. I get the name of the module out of a database query. I can do require $string no problem, but the issue then becomes how do I say $string->new?

Originally posted as a Categorized Question.

  • Comment on Using objects where you don't know the module's name until run time.

Replies are listed 'Best First'.
Re: Using objects where you don't know the module's name until run time.
by tye (Sage) on Nov 07, 2000 at 03:20 UTC
    $string->new()
    Yes, it really is that simple.
Re: Using objects where you don't know the module's name until run time.
by Narveson (Chaplain) on Jan 23, 2008 at 17:03 UTC
    require $class_name;
    only ensures that an exception will be thrown if $class_name hasn't been loaded. Presumably you think all the modules you may ever need will have already been loaded at compile time. If you want require to go out and load the class from a file at runtime, say
    eval "require $class_name";
    This allows you to leave unwanted modules unloaded, with possible performance gains.

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-25 12:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found