Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

disambiguating require (or: require loves join more than catfile)

by rovf (Priest)
on Jul 23, 2013 at 12:56 UTC ( [id://1045850]=perlquestion: print w/replies, xml ) Need Help??

rovf has asked for the wisdom of the Perl Monks concerning the following question:

This gives the expected error message :

$ perl -lwe 'use File::Spec::Functions; my $x="x"; require(catfile($x, +$x))' Can't locate x/x in @INC ...

As does this :

$ perl -lwe 'use File::Spec::Functions; my $x="x"; require join("/",$x +,$x)' Can't locate x/x in @INC ...

But this gives a syntax error :

$ perl -lwe 'use File::Spec::Functions; my $x="x"; require catfile($x, +$x)' syntax error at -e line 1, near "require catfile(" Execution of -e aborted due to compilation errors.

Why?
-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re: disambiguating require (or: require loves barewords )
by Anonymous Monk on Jul 23, 2013 at 13:02 UTC
    $ perl -e " use File::Spec::Functions; my $x=q{x}; require +catfile($x +,$x) " Warning: Use of "require" without parentheses is ambiguous at -e line +1.
      $ perl -f -we " use File::Spec::Functions; my $x=q{x}; require catfile +($x,catfile($x,$x)) " syntax error at -e line 1, near "require catfile(" Execution of -e aborted due to compilation errors. $ perl -f -we " use File::Spec::Functions; my $x=q{x}; require scalar( +catfile($x,catfile($x,$x))) " Can't locate x\x\x in @INC

      catfile isn't a builtin, scalar is a builtin, so perl knows scalar isn't a bareword, but it thinks catfile is a bareword :)

        bareword as in modulename
Re: disambiguating require (or: require loves join more than catfile)
by Monk::Thomas (Friar) on Jul 23, 2013 at 15:09 UTC
    Are you ~sure~ this does what you want it to? Require behaves differently if you feed it a bareword or a variable. (Maybe this was the reason for catfile?)

    http://perldoc.perl.org/functions/require.html

    use strict; use warnings; my $x="x"; my $module = $x . '::' . $x; print "$module\n"; eval { require $module; };
    The eval block needs some finetuning to actually do what you want it to. (Do something if require succeeds, do something else if require fails. Recommendation: Use Try::Tiny and stop worrying about all the ways eval / $! / $_ can go wrong.
    use strict; use warnings; use Try::Tiny; my $x="x"; my $module = $x . '::' . $x; try { require $module; print "Found module $module.\n"; } catch { print "Failed to find module $module!\n"; };
      Are you ~sure~ this does what you want it to?
      I think so. I have the absolute path to a file containing a set of functions which I want to drag into my code, and this should happen at runtime. If there is a problem with the 'required' file, it's fine for me to get an exception. I *could* catch it, but in my particular case I just want to abort the program, so the default behaviour is fine.

      -- 
      Ronald Fischer <ynnor@mm.st>
        Ah. Then another question: Do you expect this code to run on multiple platforms? (I'm asking because you used catfile.) In that case I'd recommend using Path::Class in favor of File::Spec because

        The well-known module File::Spec also provides this service, but it's sort of awkward to use well, so people sometimes avoid it, or use it in a way that won't actually work properly on platforms significantly different than the ones they've tested their code on.

        In fact, Path::Class uses File::Spec internally, wrapping all the unsightly details so you can concentrate on your application code. Whereas File::Spec provides functions for some common path manipulations, Path::Class provides an object-oriented model of the world of path specifications and their underlying semantics. File::Spec doesn't create any objects, and its classes represent the different ways in which paths must be manipulated on various platforms (not a very intuitive concept). Path::Class creates objects representing files and directories, and provides methods that relate them to each other.

        -- from http://search.cpan.org/~kwilliams/Path-Class-0.32/README.pod

        Recommended reading: http://www.perladvent.org/2012/2012-12-01.html

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1045850]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 15:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found