New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
for loop doesn't update correct variable #15635
Comments
From @mjdominusCreated by @mjdominusThe following program: my $i; for $i (1..3) { It emits Count 0 But it should be Count 1 Also, when correcting that bug, please make sure this is also corrected: for my $i (1..3) { It shows the same wrong behavior. Perl Info
|
From @jkeenanOn Wed Sep 28 17:50:35 2016, mjd@plover.com wrote:
Why, in a similar circumstance, does 'print' behave differently from 'printf'? ##### for $j (1..3) { $ perl 129757-other.pl Thank you very much. |
The RT System itself - Status changed from 'new' to 'open' |
From Eirik-Berg.Hanssen@allverden.noOn Thu, Sep 29, 2016 at 3:05 AM, James E Keenan via RT <
It's not different, is it? Well, okay, %d in the pattern to printf evaluates it in numeric context Eirik |
From @rjbs* James E Keenan via RT <perlbug-followup@perl.org> [2016-09-28T21:05:02]
~$ perl -e 'my $x = undef; printf "%d\n", $x' The %d format numifies undef. ~$ perl -Mwarnings -e 'my $x = undef; printf "%d\n", $x' -- |
From @petdance# A self-contained test that shows the correct behavior and doesn't get into print/printf. require "t/test.pl"; my $i = 2112; ok( $i == 2112, '$i starts at 2112' ); my $n; ok( $i == 2112, '$i should go back to 2112 outside of the loop' ); exit 0; sub somesub { |
From @ap* Mark-Jason Dominus <perlbug-followup@perl.org> [2016-09-29 03:00]:
Maybe. But that might affect this: $ perl -E 'my @i; for my $i (5..8) { push @i, sub { say $i } }; $_->() for @i' That behaviour must not break under any circumstances. To make that work, in each iteration, foreach rebinds the name $i to the perl -E 'my @i = (5..8); for my $i (@i) { say \$i eq \$i[$a++] }' It seems to me that this binding is necessarily lexical to the loop Meanwhile the `announce` sub has already closed over $i at compile time. I don’t know that it’s possible to change either of these facts. Not to The straightforward way to get your “correct” output would be to change You can always use while() when you need it.
That sub is compiled just once despite its placement inside the loop and Arguably perl ought to warn about this, the same way that it warns when sub do_announce { That throws the infamous “will not stay shared” warning. But you would { I don’t know how hard it would be to distinguish sure-fire warn-worthy Regards, |
From @cpansproutOn Wed Sep 28 21:01:34 2016, aristotle wrote:
This is similar to the problem that plagues lexical aliasing. The notes I added in 514e62e may also be relevant to package subs, but I do not have time to think it through now, or in the foreseeable future. -- Father Chrysostomos |
Migrated from rt.perl.org#129757 (status was 'open')
Searchable as RT129757$
The text was updated successfully, but these errors were encountered: