Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.

Re^3: require mystery

by Corion (Patriarch)
on Sep 06, 2007 at 07:38 UTC ( [id://637340]=note: print w/replies, xml ) Need Help??

in reply to Re^2: require mystery
in thread require mystery


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 "$"; }; };

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.

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

    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?)..:





      Perl allows barewords or strings (which are basically the same, except that a bareword comes without quotes, hence, "bare") as invocants:


      is the same as:



      my $pathtool = "File::Path"; $pathtool->import(...)

      and all three translate to:


Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-13 18:35 GMT
Find Nodes?
    Voting Booth?

    No recent polls found