Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re^2: eval "require $module" vs. Module::Load::load($module)

by djerius (Beadle)
on Aug 17, 2007 at 14:37 UTC ( #633313=note: print w/replies, xml ) Need Help??

in reply to Re: eval "require $module" vs. Module::Load::load($module)
in thread eval "require $module" vs. Module::Load::load($module)

Your code is very similar to what is in Module::Load. Why is this better than a simple eval "require $module" which uses Perl's built-in module searching capability and doesn't require any path name manipulation?

Replies are listed 'Best First'.
Re^3: eval "require $module" vs. Module::Load::load($module)
by Corion (Pope) on Aug 17, 2007 at 14:41 UTC

    You shouldn't forget that every eval "..." must be followed by or die $@ or you will never find out why requireing your module failed. In that way, I consider my way to be more foolproof. My code is quite different from the code in Module::Load because my code does not try to guess whether it is a filename or a bareword module name and it also does not do any fancy games to massage the error message. I don't like modules that try to be fancy in their error messages because far too often I found that the one helpful error message got suppressed when I wanted to see it.

      I'm aware of Perl exceptions and how to handle them. What I do with $@ is really not germane to the question, which is to determine if there is any functional difference between Module::Load and eval "require $module".

      Your code uses the same general approach as Module::Load (albeit expressed more succinctly) namely to create a filename out of a module name and pass that to require.

      So, other than style, is there any technical reason to choose this method over

      eval "require $module"; die $@ if $@;

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2018-01-22 18:26 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (235 votes). Check out past polls.