develooper Front page | perl.perl5.porters | Postings from August 2013

[perl #119487] local $/ = <STDIN> broken

Thread Next
From:
Father Chrysostomos via RT
Date:
August 27, 2013 21:39
Subject:
[perl #119487] local $/ = <STDIN> broken
Message ID:
rt-3.6.HEAD-1873-1377639579-1751.119487-15-0@perl.org
On Tue Aug 27 13:12:01 2013, mauke- wrote:
> The following programs should all produce the same output:
> 
> % perl -e 'print "a\nb\nc\n"' | perl -we 'print "[$_]\n" for do {
>    local $/ = <STDIN> }'
> [a
> b
> c
> ]
> 
> % perl -e 'print "a\nb\nc\n"' | perl -we 'print "[$_]\n" for do {
>    local $/ = do { <STDIN> } }'
> [a
> ]
> 
> % perl -e 'print "a\nb\nc\n"' | perl -we 'print "[$_]\n" for do {
>    local $/ = (0, <STDIN>) }'
> [a
> ]
> 
> I think the first one is broken.

A readline optimisation is causing the order of evaluation to change,
making the ‘local $/’ get executed too soon:

Notice how the LVINTRO line is number 3 while readline is number 5:

$ ./perl -Ilib -MO=Concise -e ' local $/ = <foo> '
6  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter ->2
2     <;> nextstate(main 1 -e:1) v:{ ->3
-     <1> null vKS/2 ->6
-        <1> ex-rv2sv sKRM*/129 ->4
3           <#> gvsv[*/] s/LVINTRO ->4
5        <1> readline[t3] sKS/1 ->6
4           <#> gv[*foo] s ->5
-e syntax OK

Here the numbers are 5 and 4, so the execution order is different:

$ ./perl -Ilib -MO=Concise -e ' local $/ = do { <foo> } '
7  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter ->2
2     <;> nextstate(main 2 -e:1) v:{ ->3
6     <2> sassign vKS/2 ->7
-        <1> null sK*/1 ->5
-           <@> scope sK ->-
-              <0> ex-nextstate v ->3
4              <1> readline[t3] sK/1 ->5
3                 <#> gv[*foo] s ->4
-        <1> ex-rv2sv sKRM*/129 ->6
5           <#> gvsv[*/] s/LVINTRO ->6
-e syntax OK

-- 

Father Chrysostomos


---
via perlbug:  queue: perl5 status: new
https://rt.perl.org:443/rt3/Ticket/Display.html?id=119487

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About