Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

require mystery

by Pstack (Beadle)
on Sep 06, 2007 at 00:33 UTC ( #637299=perlquestion: print w/ replies, xml ) Need Help??
Pstack has asked for the wisdom of the Perl Monks concerning the following question:

I cannot seem to successfully require "File::Path" despite that use "File::Path" works fine.

When I require "File::Path" the runtime error is that it is not in @INC, despite that the correct path does seem to be located there "/..../5.8.7/File/Path.pm".

I am wondering if there is some particular reason that File::Path may not be able to be required? Or do I need to do a bit more digging on the problem?

cheers

Pstack

Comment on require mystery
Re: require mystery
by ikegami (Pope) on Sep 06, 2007 at 00:44 UTC
    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!

      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.

Re: require mystery
by clinton (Priest) on Sep 06, 2007 at 08:20 UTC
    require mystery;
    Ahhh, the cure for a mundane life...
      Tsk tsk, you have to import before you're cured :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://637299]
Approved by ikegami
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-12-28 21:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (183 votes), past polls