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

I see!

Do you know why a specific behavior is not defined? Just curious.

I also note that the warning appears in the perlsyn page 5.10, but not in my perlsyn page (5.8). Is this something that bit people until they added it to the manual? I didn't see it in the camel either...

Anyway, thanks for the help.

  • Comment on Re^2: Lexical scope vs. postfix loops (perl bug?)

Replies are listed 'Best First'.
Re^3: Lexical scope vs. postfix loops (perl bug?)
by ikegami (Patriarch) on Aug 25, 2008 at 22:13 UTC

    Do you know why a specific behavior is not defined? Just curious.

    The behaviour is actually quite predictable, but it's probably not one the Perl developers want to be held to. If that's the case, the warning in the documentation serves to avoid a dependancy on the current behaviour.

    In case you're curious,

    The current compile-time behaviour of my is to define the variable for the rest of the scope, starting with the next statement.

    The current run-time behaviour of my is to place an instruction on the stack to clear (if refcount = 0) or replace (if refcount > 0) the variable on scope exit. It also returns the variable as an lvalue.

Re^3: Lexical scope vs. postfix loops (perl bug?)
by moritz (Cardinal) on Aug 25, 2008 at 20:36 UTC
    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.

      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 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'" }