in reply to Re^2: Lexical scope vs. postfix loops (perl bug?)
in thread Lexical scope vs. postfix loops (perl bug?)

Do you know why a specific behavior is not defined?
I think that there's no one consistent way to define one. Usually variables are block scoped, so making my $x for @list behaving the same as do { my $x } for @list is just weird.

One could argue that $stuff for @list should always be the same as for @list { $stuff }, but then you could write

use strict; print $x*$x for my $x (0..10);

Which seems equally weird, because a variable is used (textually)before it is declared. (And I don't know if that's technically possible in the perl compiler).

So regardless from which angle you look at it, it smells badly. So the behaviour is not defined. Maybe someday a hero of programming languages will find something that's consistent in every way, and then it can be still implemented.

Replies are listed 'Best First'.
Re^4: Lexical scope vs. postfix loops (perl bug?)
by jethro (Monsignor) on Aug 25, 2008 at 20:47 UTC
    I could live with a hero of perl who makes statements like this emit a warning if 'use warnings' is on.
      The problem is that there are usually strong objections against introducing new warnings. That's because p5p is partly dominated by perl gurus who have used every weird feature in their code at least once, and don't want it to break, or emit a warning.

      The second reason to reject warnings usually is "that belongs in a lint utility like Perl::Critic.

      A third possible reason can be that some stuff is surprisingly hard to implement in perl5 because it doesn't keep a parse tree of the whole program, but discards the current part of the parse tree as soon as it can be turned into an op tree. Which makes static analysis non-trivial, in some cases (I don't know how it is in this case, though).

      Currently I think that a warning is not a bad idea.

        I agree... it's hard to agree on what the behavior is (probably why it has remained undefined) but if the behavior is "undefined" then a warning should be emitted either under "use warnings" or (my preference) "use strict".

        Could you not add a new category for this under "strict" or "warnings" so that you could say:

        use strict; no strict 'postfix';

        That might satisfy the gurus who have code that depends on this "weird" behavior.

Re^4: Lexical scope vs. postfix loops (perl bug?)
by JadeNB (Chaplain) on Aug 26, 2008 at 16:31 UTC
    I think that there's no one consistent way to define one. Usually variables are block scoped, so making my $x for @list behaving the same as do { my $x } for @list is just weird.
    Do you really think that it's weirder than having for my $x ( @list ) { BLOCK } behave the same as { my $x; for $x ( @list ) { BLOCK } }?
      Yes. I one case the special behaviour is restricted to only declarations, in the other case it would be expanded to arbitrary code. (Or at least one statement of arbitrary code). That's quite a difference, IMHO.

      But my opinion doesn't count that much ;-)

      Luckily Perl 6 fixes that behaviour by not special casing the scoping at all, and instead introducing blocks with signatures ("pointy blocks", known as lambdas in other programming languages).

      # Perl 6 code below: while my $x < 3 { # code here } # $x still visible here for @list -> $y { # $y visible here } # $y not visible here # other uses for lambdas: my $quote = -> Str $s { "'$s'" }