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

eval "require $class" seems wrong

by rvosa (Curate)
on Aug 22, 2007 at 17:34 UTC ( #634438=perlquestion: print w/replies, xml ) Need Help??

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

Dear monks,

there are some situations where I need to use/require a class from a variable (say $class). The way I do that is by:
eval "require $class"; if ( $@ ) { die "Can't load $class: $@"; } my $obj = $class->new;
...or something to that effect, i.e. by using a string eval. This feels wrong (perlcritic complains about it also). What's the right way?


Replies are listed 'Best First'.
Re: eval "require $class" seems wrong
by jZed (Prior) on Aug 22, 2007 at 17:37 UTC
    eval { require $class }; :-)


    eval { require "$" }; :-)
      That's not the same thing! Compare:
      perl -Mstrict -e 'my $c="CGI";eval "require $c";die $@ if $@' perl -Mstrict -e 'my $c="CGI";eval {require $c};die $@ if $@'
      Ah, the update works. I'd ++ that if I had votes left for today :)

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

        - tye        

Re: eval "require $class" seems wrong
by ikegami (Patriarch) on Aug 22, 2007 at 17:48 UTC
      That also does a string eval, actually. See the _require sub in Module/Pluggable/ Maybe I'm just barking up the wrong tree - I figured that something that's not uncommon (requiring a class at runtime, from a variable) could be done in a "best practices" sort of way. Apparently not. This seems strangely un-TIMTOWDI.

        I was kinda suggesting the best practice was to avoid requireing dynamic class names completely, and address the problem at a higher level.

        To answer your question directly, one can require either a class supplied as a bareword or a path. There isn't a way to require a class supplied in a scalar. In order to load a dynamically constructed class name, some use eval EXPR to do the former (e.g. Module::Pluggable), and some transform the class name into a path and use the latter (e.g. if). I have no idea why one cannot do require $class;.

Re: eval "require $class" seems wrong
by lima1 (Curate) on Aug 22, 2007 at 17:38 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        

        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
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://634438]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2022-06-30 16:54 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (98 votes). Check out past polls.