Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

by tye (Sage)
on Aug 22, 2007 at 19:10 UTC ( #634482=note: print w/replies, xml ) Need Help??


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

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

- tye        

  • Comment on Re^3: eval "require $class" seems wrong (::)

Replies are listed 'Best First'.
Re^4: eval "require $class" seems wrong (::)
by rvosa (Curate) on Aug 22, 2007 at 22:51 UTC
    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        

        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!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://634482]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (12)
As of 2018-07-20 13:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (431 votes). Check out past polls.

    Notices?