Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Do you know where your variables are?
 
PerlMonks  

Re^2: eval "require $class" seems wrong

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


in reply to Re: eval "require $class" seems wrong
in thread eval "require $class" seems wrong

Ah, the update works. I'd ++ that if I had votes left for today :)


Comment on Re^2: eval "require $class" seems wrong
Re^3: eval "require $class" seems wrong (::)
by tye (Cardinal) on Aug 22, 2007 at 19:10 UTC

    Then you are violating a different "best practice": Modules should always have a "::" in their name.

    - tye        

      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;

        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        

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (13)
As of 2014-04-23 21:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (554 votes), past polls