Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: eval "require $class" seems wrong

by lima1 (Curate)
on Aug 22, 2007 at 17:38 UTC ( #634442=note: print w/replies, xml ) Need Help??

in reply to eval "require $class" seems wrong


Replies are listed 'Best First'.
Re^2: eval "require $class" seems wrong
by rvosa (Curate) on Aug 22, 2007 at 17:48 UTC
    It looks like what UNIVERSAL::require does is: turn $class name (e.g. Some::Class) into a path (Some/, then do eval qq{require $path}, which amounts to the same thing - a string eval - but hidden in a dependency. Okay, it checks %INC first to see if it's already been loaded, but that's about it.

      If you change "::" to "/" and append ".pm", then you don't need a string eval, you can just do require $class. I wouldn't even do eval { require $class } (non-string eval) unless I was wanting to work around the module not being found. But if you do want to eval, then the best choice is:

      ( my $file= $class ) =~ s-::|'-/-g; if( ! eval { require "$"; 1 } ) { # work-around the failure here }

      But I don't find the argument against string eval compelling enough to have much of a strong preference between that and the below string eval unless there is risk that $class might contain mischevious data:

      if( ! eval "require $class; 1" ) { # work-around the failure here }

      If you don't want to work-around a failure (and trust $class), then the choices are:

      eval "require $class; 1" or die $@;


      ( my $file= $class ) =~ s-::|'-/-g; require "$";

      And I'd never use UNIVERSAL::require, since I consider poluting such a very global namespace to be way too much sin for the sake of saving one line of code.

      - tye        

        The 1 is useless since require always returns true. (If it encounters no error, it returns what the module returns. If the module returns false, it encountered an error. If it encounters an error, it throws an exception.)

      It uses eval EXPR for #line to work. It has nothing to do with require. (eval BLOCK would suffice if you wanted to catch exceptions.)

      The following will work (even if your platform doesn't use / as the path seperator):

      $class = 'HTTP/'; require $class;

      However, transforming the class name into a path is really no better.

      Sure, you need an eval here to check whether the module was found and successfully loaded. You need the eval in the first case to interpolate the module name and turn it into a bareword, or not?

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2020-10-31 07:15 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (287 votes). Check out past polls.