Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Arbritrarily requireing classes.

by graq (Curate)
on Aug 03, 2004 at 15:52 UTC ( [id://379714]=perlquestion: print w/replies, xml ) Need Help??

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

How do I avoid 'hard coded' require at the top of my module and place require A::N::Other::typeOne; inside the eval{};?
package Some::Class; my $typeToTableLookup = { 'typeOne' => 'classOne', 'typeTwo' => 'classTwo', }; require A::N::Other::typeOne; # Remove these require A::N::Other::typeTwo; # two lines... sub new { my ($proto, $type) = @_; my $class = ref($proto) || $proto; die( "Note type must be specified.\n" ) unless $type; my $self = bless { type => $type }, $class; return $self; } sub doSomeFoo { my ($self, $id) = @_; my $typeTableName = $$typeToTableLookup{$self->{type}} or die( "Type does not have a class\n" ); my $classFile = 'A::N::Other::'.ucfirst($typeTableName); my $fooOb; eval { # require $classFile; # ... and replace them here. $fooOb = $classFile->doSomeBar($id); }; die $@ if $@; return \$fooOb; }
Or am I missing the point (as usual :P)?

Replies are listed 'Best First'.
Re: Arbritrarily requireing classes.
by japhy (Canon) on Aug 03, 2004 at 16:03 UTC
    You could say eval "require $classFile";
    _____________________________________________________
    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
•Re: Arbritrarily requireing classes.
by merlyn (Sage) on Aug 03, 2004 at 16:13 UTC
Re: Arbritrarily requireing classes.
by Gilimanjaro (Hermit) on Aug 03, 2004 at 16:14 UTC
    The
    eval "require $classFile;";
    method is the one I use often. I'd name the variable $package though, because you're not actually naming the file.

    I sometimes even eval a 'use', so the import get's run. Be sure to keep checking $@ afterwards though... Not doing so is asking for trouble. You may want to expand the die a bit, something like:
    die "Error during dynamic require of $classFile:\n$@"
    This'll just make the error messages a bit clearer for ya...
Re: Arbritrarily requireing classes.
by danielcid (Scribe) on Aug 03, 2004 at 16:12 UTC
    Just try:
    eval{ require Test::Foo::Bar; }; your_error_handled_here();

    -DBC
Re: Arbritrarily requireing classes.
by ysth (Canon) on Aug 03, 2004 at 17:08 UTC
    You make it:
    my $class = "A::N::Other::".ucfirst($typeTableName); (my $classFile = "$class.pm") =~ s!::!/!g; require $classFile; $fooOb = $class->doSomeBar($id);
    No eval required, unless you are guarding against the requested class or method not existing or the method throwing an exception.
Re: Arbritrarily requireing classes.
by gellyfish (Monsignor) on Aug 03, 2004 at 16:43 UTC

    At first glance this looks like you may be going in the same direction as Module::Pluggable - of course I may be going completely wrong there.

    /J\

Re: Arbritrarily requireing classes.
by broquaint (Abbot) on Aug 04, 2004 at 03:34 UTC
    use Module::Locate 'locate'; ## later ... my $className = 'A::N::Other::'.ucfirst($typeTableName); my $fooOb = eval { require( locate $className ); $className->doSomeBar($id); };
    See. Module::Locate for more info.
    HTH

    _________
    broquaint

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://379714]
Approved by sschneid
Front-paged by csuhockey3
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2024-03-28 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found