develooper Front page | perl.perl5.porters | Postings from September 2016

[perl #129757] for loop doesn't update correct variable

Thread Next
From:
James E Keenan via RT
Date:
September 29, 2016 01:05
Subject:
[perl #129757] for loop doesn't update correct variable
Message ID:
rt-4.0.24-10757-1475111102-923.129757-15-0@perl.org
On Wed Sep 28 17:50:35 2016, mjd@plover.com wrote:
> 
> This is a bug report for perl from mjd@plover.com,
> generated with the help of perlbug 1.39 running under perl 5.18.2.
> 
> 
> -----------------------------------------------------------------
> [Please describe your issue here]
> 
> The following program:
> 
> my $i;
> sub announce { printf "Count %d\n", $i }
> 
> for $i (1..3) {
>   announce();
> }
> 
> It emits
> 
> Count 0
> Count 0
> Count 0
> 
> But it should be
> 
> Count 1
> Count 2
> Count 3
> 
> Also, when correcting that bug, please make sure this is also
> corrected:
> 
> for my $i (1..3) {
>   sub announce { printf "Count %d\n", $i }
>   announce();
> }
> 
> It shows the same wrong behavior.
> 

Why, in a similar circumstance, does 'print' behave differently from 'printf'?

#####
$ cat 129757-other.pl 
my $j;
sub denounce { print "Count $j\n"; }

for $j (1..3) {
  denounce();
}

$ perl 129757-other.pl 
Count 
Count 
Count 
#####

Thank you very much.
-- 
James E Keenan (jkeenan@cpan.org)

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

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