Scope of variables in a function

Dave Mitchell davem at
Mon Jun 3 00:25:28 BST 2013

On Sun, Jun 02, 2013 at 10:43:02PM +0100, Gordon Banner wrote:
> Also that would break
> my $var = "outer";
> {
>     my $stashed_value = $var;
>     my $var = "inner";
>     ...
> }

(except that it wouldn't). My example wasn't meant to be literal: it was
just a hand-wavy explanation of moving the *runtime* effects of my to the
start of the block. The compile-time effect (i.e. scope) was unaffected.
Sorry if this wasn't; clear.

NB: the run-time effect of 'my' is to push a note onto the save stack
saying "at scope exit, please decrement the reference count of this
lexical var, and do other misc bits of housekeeping"). This is why with

    my $x if 0;


    goto foo;
    my $x;

the lexical doesn't get cleared at the end of scope, meaning its old value
is sill there on next entry to the block.

I've often wanted to drown my troubles, but I can't get my wife to go

More information about the mailing list