in reply to Rules of Lexical Scoping

2. Each time a block with a 'my' declaration is entered, a new instance of that lexical is created, and each time the block is exited, that instance is discarded (of course, if something else holds a reference to it, then the actual thinbg itself continues to exist, it is just not accessible via the lexical name).

I think that's quite inaccurate.While that may be what seems to happen and it may be what should happen, that's not what really happens. Lexical variables are cleared on block exit and re-used unless there are still references to them.

That means

In more details,

These details are somewhat visible in the following example:

sub test { my ($reset) = @_; my $x if $reset; print ++$x, "\n"; } test(0); # 1 test(0); # 2 test(1); # 3 test(0); # 1 test(0); # 2

Perl 5.8.8

Replies are listed 'Best First'.
Re^2: Rules of Lexical Scoping
by ysth (Canon) on Nov 11, 2007 at 06:33 UTC
    I think that's quite inaccurate. Lexical variables are cleared on block exit and re-used unless there are still references to them.
    That is an optimization, and the documentation specifically warns against relying on it as your example does.

    The "discarded" doesn't mean literally freeing the pad SV. Here:

    $ perl5.10.0 -we'{ my $x; sub foo { eval q!print $x! } foo }' Use of uninitialized value $x in print at (eval 1) line 1. $ perl5.10.0 -we'{ my $x; sub foo { eval q!print $x! } } foo' Variable "$x" is not available at (eval 1) line 2. Use of uninitialized value $x in print at (eval 1) line 1.
    $x is logically discarded and hence "not available" after the scope is exited, even though the SV may still stay in the pad.
Re^2: Rules of Lexical Scoping
by jdporter (Paladin) on Nov 11, 2007 at 13:43 UTC
    Lexical variables aren't discarded and recreated. They are simply cleared.

    I think that ysth has adequately answered that objection, but I would answer it this way: All of that happens under the hood; it has no user-visible effects. In any way that a perl programmer can tell, perl behaves just as ysth has described.

    A word spoken in Mind will reach its own level, in the objective world, by its own weight
      I personally believe that ysth has adequately answered that objection, but I would answer it this way: All of that happens under the hood; it has no user-visible effects. In any way that a perl programmer can tell, perl behaves just as ysth has described.
      s/ysth has described/dave_the_m has described/.

      Also "has no user-visible effects" is an overstatement; should have no user-visible effects is more accurate. The rules describe how things should work; there may be bugs, and (as in the case of my $foo if 0;) there are things you can (but shouldn't) do to expose rule violations that for various reasons won't be fixed.

        s/ysth has described/dave_the_m has described/

        I presume you're alluding to this; I was merely referring to your root in this thread.

        there may be bugs

        Granted.

        A word spoken in Mind will reach its own level, in the objective world, by its own weight

      "3. At the creation time of a sub that references an outer lexical" as opposed to "3. At the creation time of a sub in scope of an outer lexical" is also an optimization that also has little user-visible effect.

      ysth posted (in part) to explain the low-level details behind the sub { eval '$i' } problem.
      I posted to explain the low-level details behind the my $i if $expr; problem.

      I don't see my post as being out of place, although I'll grant you that my opening ("I think that's quite inaccurate.") could have been worded better.

        When I read "Each time a block with a 'my' declaration is entered, a new instance of that lexical is created", my first reaction was very much "I think that's quite inaccurate".

        I also quite disagree with the assertion that "it has no user-visible effects" and not just because you demonstrated some in your reply. The only reason I knew that the original description was inaccurate was because I had answered nodes from users who ran into things (which means they were user-visible) that, in order to explain, I had to make those specific effects more obviously visible.

        - tye