Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

pipe list open trouble?

by dk (Chaplain)
on Sep 30, 2014 at 13:01 UTC ( #1102428=perlquestion: print w/replies, xml ) Need Help??

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

Hello all,

Can anybody explain why I'm seeing the difference in those pieces of code? I thought these should be identical:

$ perl -e 'open F, @ARGV or die $!' '|-' echo 42 No such file or directory at -e line 1. $ perl -e 'open F, $ARGV[0],$ARGV[1],$ARGV[2] or die $!' '|-' echo 42 42
Thank you!

Replies are listed 'Best First'.
Re: pipe list open trouble?
by salva (Canon) on Sep 30, 2014 at 13:13 UTC
    That's a quirk of the open built-in: It forces scalar context on its second argument.
    open F, @ARGV;
    ... is equivalent to ...
    open F, scalar(@ARGV);
    As a workaround you can use...
    my ($mode, @cmd) = @ARGV; open F, $mode, @cmd or die ...;

    update: prototype shows it:

    $ perl -E 'say prototype(\*CORE::open)' *;$@
      Good example why the use of $ prototypes is usually discouraged. :)

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

        Technically, prototypes are not involved here.

        The way open arguments are parsed is hard-coded inside the Perl grammar and the prototype(\*CORE::...) construction is just a facade.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1102428]
Approved by jellisii2
Front-paged by toolic
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2022-08-10 02:49 GMT
Find Nodes?
    Voting Booth?

    No recent polls found