Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: require mystery

by ikegami (Pope)
on Sep 06, 2007 at 00:44 UTC ( #637301=note: print w/replies, xml ) Need Help??


in reply to require mystery

Going on a hunch since you provided neither the code causing the error nor the resulting error message,
use File::Path; # ok require File::Path; # ok require "File/Path.pm"; # ok require "File::Path"; # Error!

Replies are listed 'Best First'.
Re^2: require mystery
by Pstack (Scribe) on Sep 06, 2007 at 07:29 UTC

    I followed your hunch to look deeper, as it seemed line 2 should have failed for me, even if there had been a prior use.

    my $pathtool = "File::Path";
    eval {require $pathtool}; # FAILS
    if ($@) {print $@;} # "No File::Path in @INC"

    eval "require $pathtool"; # OK
    eval {require File::Path}; # OK
    eval {$pathtool->import("mkpath")}; # OK

    Clearly $pathtool wont work in the FIRST block eval where the bareword will, but will work in the SECOND block eval. A subtle trap for young players! But I am relieved. Thanks for your help.

    Pstack

      No:

      require $pathtool;

      is exactly the same as

      require "File::Path";

      which fails. Your other attempt,

      eval "require $pathtool";

      evaluates the string, which then looks like

      require File::Path;

      which succeeds. Consider using the following code if you want a dynamic module loader:

      sub require_package { for (@_) { (my $file = $_) =~ s-::|'-/-g; require "$file.pm"; }; };

      Other alternatives which I recommend against are UNIVERSAL::require and Module::Load because they both hide fatal errors from you, for example compilation errors in the required modules.

      Maybe you might want Module::Pluggable for a plugin system.

        Yes, I see, and like your custom function. The code had been working previously with an expression eval before I altered it to the FIRST block eval.

        Did you notice how I was thrown off by the success nearby of the SECOND block eval which survived all versions:

        eval {$pathtool->import("mkpath")}; #OK

        which would not then seem right either if similarly translated as ..(though it may be right?)..:

        "File::Path"->import("mkpath");#??

        Mmm!

        cheers

        Pstack

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2020-05-29 10:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (169 votes). Check out past polls.

    Notices?