Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^4: eval "require $class" seems wrong (::)

by rvosa (Curate)
on Aug 22, 2007 at 22:51 UTC ( #634535=note: print w/ replies, xml ) Need Help??


in reply to Re^3: eval "require $class" seems wrong (::)
in thread eval "require $class" seems wrong

Okay, well, so in the general case (including '::' in module names), the non-string-eval way to do it seems to be:

# turn $class name into $path my $class = 'Some::Class'; my $path = $class; $path =~ s/::/\//g; $path .= '.pm'; # check if $path already loaded if ( not exists $INC{$path} ) { # do block eval on $path eval { require $path }; die "Can't load $class: $@" if $@; } # if we make it here, we can use $class my $obj = $class->new;


Comment on Re^4: eval "require $class" seems wrong (::)
Download Code
Re^5: eval "require $class" seems wrong (code)
by tye (Cardinal) on Aug 22, 2007 at 23:01 UTC

    The choices of code I posted over 2 hours ago are all shorter, don't make your one minor mistake, and don't waste code making duplicate checks of %INC nor repeating parts of the error message that will be in $@. But, yeah, that's the general idea. :)

    - tye        

      Sorry, what's the minor mistake? I'm trying to figure out how to do this correctly. I know the bit I wrote is more verbose (I did that for clarity), but of course I want to avoid duplicate %INC checks, so could you please explain? Thanks!

        Did you compare the code? It isn't much code at all, so I'd think that should be your first approach.

        The minor mistake was lack of support for modules like D'oh and Acme::Don't (hence the emphasis).

        but of course I want to avoid duplicate %INC checks, so could you please explain?

        I'm at a loss. What don't you understand about duplicate %INC checks when comparing my code examples to yours?

        - tye        

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-08-20 16:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (119 votes), past polls