On Sat, Mar 22, 2003 at 09:45:43PM +0200, arcadi shehter wrote:
in this example

sub a { state $x ; my $y ; my sub b { ... } ;
...
}


how "my sub b" is different from "state $x" from the point of view of
scope ?

Actually, all three have the same scope, but they have different lifetime.
Each invocation of sub a has fresh $y and &b variables, but keeps the same $x every time.


A nice example is:

sub a {
   state $x;
   my $y;
   my sub b { return $x++ + $y++; }
   return &b;   # is a \ before &b needed?
}

Every call to sub a will return a different closure. The $x in each closure all refer to the same variable. Each closure's $y however is different and independent.

--
Matthijs van Duin  --  May the Forth be with you!

Reply via email to