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

[perl #127463] third part of C-style for has messed up scoping

Thread Next
From:
l.mai@web.de via RT
Date:
May 30, 2016 18:55
Subject:
[perl #127463] third part of C-style for has messed up scoping
Message ID:
rt-4.0.18-32237-1464634508-1884.127463-15-0@perl.org
Am Fr 27. Mai 2016, 04:19:32, aristotle schrieb:
> * l.mai@web.de via RT <perlbug-followup@perl.org> [2016-02-05 12:00]:
> > Am Do 04. Feb 2016, 23:29:24, hmbrand schrieb:
> > > Normally, the part inside the parens scopes to the block connected,
> > > so this does not surprise me.
> >
> > 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.
> 
> Variables declared in the A part of for(A;B;C) do not remain declared
> past the loop. If you are serious about strict textual equivalence of
> these constructs then that must also bother you. Does it?

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.

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

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