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

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

by djerius (Beadle)
on Aug 17, 2007 at 14:09 UTC ( #633296=perlquestion: print w/ replies, xml ) Need Help??
djerius has asked for the wisdom of the Perl Monks concerning the following question:

Is there any benefit to Module::Load (other than style and not invoking the compiler) over a simple eval "require $module"? I think that the following should be equivalent:
use Module::Load qw( load ); my $module = 'MyModule'; eval { load $module }; die "can't load $module this way\n" if $@; eval "require $module"; die "can't load $module this way either\n" if $@;

Comment on eval "require $module" vs. Module::Load::load($module)
Select or Download Code
Replies are listed 'Best First'.
Re: eval "require $module" vs. Module::Load::load($module)
by Corion (Pope) on Aug 17, 2007 at 14:16 UTC

    Both of your methods hide the actual value of $@, which I consider a horrible design idea. $@ often contains valuable information and hiding that from the users/programmers makes for ugly debugging. Personally, I prefer the following idiom:

    my $filename = $module; $filename =~ s-(::|')-/-g; $filename .= '.pm'; require $filename; # dies with an informative message

    Looking at Module::Load, it again contains many weirdo checks that I have come to distrust when it comes to load failures - far too often I've had syntax errors in my modules hidden by UNIVERSAL::require to want such ...

      That method ignores that fact that perl search for .pmc files first, only then for .pm files.

      Most of the time there are no .pmc files, but iirc v6 uses them.

        Did you test your claim?

        corion@aliens:~/parent$ perl -we 'require "t/lib/FileThatOnlyExistsAsP"' corion@aliens:~/parent$ perl -we 'require "t/lib/FileThatOnlyExistsAsP" or die "Uhoh"' corion@aliens:~/parent$ ls -l t/lib/FileThatOnlyExistsAsPMC.pmc -rwxr-xr-x 2 corion corion 82 2007-08-02 22:57 t/lib/FileThatOnlyExist +sAsPMC.pmc corion@aliens:~/parent$ cat t/lib/FileThatOnlyExistsAsPMC.pmc package FileThatOnlyExistsAsPMC; sub exclaim { "I CAN FROM " . __PACKAGE__ } 1;
        Modern versions of perl will search for .pmc files if you require ''. The change ocurred sometime between 5.00501 (which doesn't), and 5.6.0 (which does).
      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?

        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.

Re: eval "require $module" vs. Module::Load::load($module)
by moritz (Cardinal) on Aug 17, 2007 at 14:15 UTC
    From the Module::Load Documentation:

    If you consult perldoc -f require you will see that require will behav +e differently when given a bareword or a string. In the case of a string, require assumes you are wanting to load a fil +e. But in the case of a bareword, it assumes you mean a module. This gives nasty overhead when you are trying to dynamically require m +odules at runtime, since you will need to change the module notation +(Acme::Comment) to a file notation fitting the particular platform yo +u are on.

    Actually I have no clue if that's right, but it sounds like it answers your question.

      I think this part is even wrong:

      This gives nasty overhead when you are trying to dynamically require m +odules at runtime, since you will need to change the module notation (Acme::Comment) to a file notation fitting the particular platform you are on.

      ... because require FILENAME always wants unix notation (forward slashes and .pm at the end), no matter what the platform is.

      Thanks, but eval "require $module" avoids this problem as it ensures that require is passed a bareword.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://633296]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (12)
As of 2015-09-04 14:01 GMT
Find Nodes?
    Voting Booth?

    My preferred temperature scale is:

    Results (138 votes), past polls