Re: new "state" variables... nest properly??
- From: Ben Morrow <ben@xxxxxxxxxxxx>
- Date: Thu, 27 Dec 2007 23:43:32 +0000
Quoth Abble <grg2@xxxxxxxxxxx>:
Do the new "state" variable declaration stuff mean that finally you
can nest subs and variables will behave properly? Great news, if
true.
My little test program seems to indicate this is correct:
use strict; use warnings; use English; use feature 'state';
my( $GVar );
sub Outer{ my( $Arg ) = @_; state ( $Var );
sub Inner{
Named subs don't close over lexicals. That hasn't changed. This still
gives a 'Variable '$Var' will not stay shared' warnings, although since
the whole point of state vars is you only get one copy, this is not
terribly useful :).
What you are looking for is not state vars, but 'my sub'. There has been
a slot for this in the Perl grammar for a long time, but noone has ever
implemented it.
if( $GVar eq $Var ) { print "midvar is okay: $Var\n" }
else { print "midvar should be '$GVar' but is '$Var'\n" }
}
#begin Outer
if( $Var ) { print "*** Var preset!!! to '$Var' \n" }
else { print "Var unset as supposed to be!\n" }
This section 'fails': the second time through, $Var is already set,
since it preserves its value from last time Outer was called.
Ben
.
- Follow-Ups:
- Re: new "state" variables... nest properly??
- From: Abble
- Re: new "state" variables... nest properly??
- References:
- new "state" variables... nest properly??
- From: Abble
- new "state" variables... nest properly??
- Prev by Date: Re: sprintf doesnt work in s///e syntax.
- Next by Date: Re: new "state" variables... nest properly??
- Previous by thread: new "state" variables... nest properly??
- Next by thread: Re: new "state" variables... nest properly??
- Index(es):