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
third part of C-style for has messed up scoping #15161
Comments
From @maukeCreated by @mauke$ perl -we 'for (my $i = 1; $i < 10; $i += my $k) { $k = $i; print "$i\n"; }' I'm not sure what's going on here, but 'my $k' should probably not do that (it for (my $i = 1; $i < 10; do { $i += my $k }) But for some reason $k leaks through to the loop body, where it can be set Perl Info
|
From @TuxOn Thu, 4 Feb 2016 17:08:28 -0800, "l.mai@web.de (via RT)"
Normally, the part inside the parens scopes to the block connected, so $ cat t.pl if (my @foo = (1..4)) { $ perl t.pl Which is completely in line with $ cat t.pl for (my $i = 1; $i < 5; $i += my $k) { $ perl t.pl -- |
The RT System itself - Status changed from 'new' to 'open' |
From @maukeAm Do 04. Feb 2016, 23:29:24, hmbrand schrieb:
The difference is that for (A; B; C) { D } should be equivalent to A; while (B) { D } continue { C } C is executed after A, B, and D. That's why it's surprising that the loop body D can see and access variables declared in C, even though their definitions haven't even been executed yet. The situation is analogous to % perl -wE 'my $r; { say(my $k), last if $r; $k = "what"; $r = 1; redo }' Here we also manage to make 'my $k' not evaluate to undef by arranging for the control flow not to match the lexical structure of the program, executing 'my $k' after setting $k. This version is more obviously broken because the behavior of 'my ... if ...' is documented as undefined ("Here be dragons"). But with C-style 'for' you get this effect automatically and implicitly, just by declaring variables in the third part. |
From @ap* l.mai@web.de via RT <perlbug-followup@perl.org> [2016-02-05 12:00]:
Variables declared in the A part of for(A;B;C) do not remain declared Regards, |
From @maukeAm Fr 27. Mai 2016, 04:19:32, aristotle schrieb:
I'm not serious about strict equivalence, let alone textual. What bothers me is the time traveling / backwards scoping of C, which comes last in execution order. Anyway, the equivalence wrt A can be easily restored by wrapping the whole thing in another scope: do { A; while (B) { D } continue { C } }; It's a bit harder to make 'my $x' not return undef in normal Perl code. |
From @iabynOn Mon, May 30, 2016 at 11:55:09AM -0700, l.mai@web.de via RT wrote:
Just as a data point, the grammar for C<for(;;)> is currently written for (A; B; C) { D } is equivalent to: { -- |
Migrated from rt.perl.org#127463 (status was 'open')
Searchable as RT127463$
The text was updated successfully, but these errors were encountered: