http://qs321.pair.com?node_id=634479


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

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 "$file.pm"; 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 $@;

and

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

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