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

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 } }?

Replies are listed 'Best First'.
Re^5: Lexical scope vs. postfix loops (perl bug?)
by moritz (Cardinal) on Aug 26, 2008 at 16:49 UTC
    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'" }