http://qs1969.pair.com?node_id=1102428

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.