Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

while(<>) in perl 5.22

by xorl (Deacon)
on Nov 14, 2016 at 19:46 UTC ( [id://1175906]=perlquestion: print w/replies, xml ) Need Help??

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

So a script I wrote long ago has stopped working somewhere between perl 5.x and 5.22.
myscript.pl
#!/usr/bin/perl use strict; while (<>) { # do stuff with each argument given and spit out a sting. # this example I'll just do print; }
Now when I run it
$> ./myscript.pl foo bar baz Can't open foo: No such file or directory at ./sp2comma.pl line 13. Can't open bar: No such file or directory at ./sp2comma.pl line 13. Can't open baz: No such file or directory at ./sp2comma.pl line 13.
The expected result is of course
$> ./myscript.pl foo bar baz foobarbaz
Has something happened? Why is while (<>) not doing what it use to? How do I get the old behavior? Thanks in advance.

Replies are listed 'Best First'.
Re: while(<>) in perl 5.22
by toolic (Bishop) on Nov 14, 2016 at 19:54 UTC
    I reproduce your undesired behavior on 5.16. To get your desired output in this instance, replace while (<>) with for (@ARGV)

    See also perlop

Re: while(<>) in perl 5.22
by dsheroh (Monsignor) on Nov 15, 2016 at 08:33 UTC
    Has something happened?
    Nope.
    Why is while (<>) not doing what it use to?
    It's doing the same thing as it has since at least 1999, when I started using Perl, and probably always has.
    How do I get the old behavior
    <> reads either from files named as command-line arguments or from STDIN. It does not, and never has, returned the literal command-line arguments themselves.

    If you want to access the literal command-line arguments, you can find them in the array @ARGV. e.g.:

    $ perl -E 'for (@ARGV) { say $_ }' foo bar baz foo bar baz

      Hi dsheroh,

      It's doing the same thing as it has since at least 1999, when I started using Perl, and probably always has.

      I just tested it and can confirm what you wrote for all Perl releases from 5.002 (1996) to v5.24.

      Regards,
      -- Hauke D

Re: while(<>) in perl 5.22
by choroba (Cardinal) on Nov 14, 2016 at 21:46 UTC
    > somewhere between perl 5.x and 5.22

    Interesting. The diamond operator <> is documented to behave this way for x=0.4 (I wasn't able to find older references easily).

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Neither result is consistent with my understanding of the 5.22 documentation.
      while (<>) { ... # code for each line } is equivalent to the following Perl-like pseudo code: unshift(@ARGV, '-') unless @ARGV; while ($ARGV = shift) { open(ARGV, $ARGV); while (<ARGV>) { ... # code for each line } }

      I would expect the open to fail silently.(There is no explicit test for errors.) The read on the unopened filehandle should return a false value. The loop would terminate without ever executing the print statement.

      Bill
        The documentation also says there's a difference, and you can easily see there is:
        ~ $ perl -e '1 while <>' non-existent-file Can't open non-existent-file: No such file or directory at -e line 1.

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2024-04-16 16:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found