o->op_private |= OPpASSIGN_COMMON;
}
- if ( ((left->op_private & OPpLVAL_INTRO) || ckWARN(WARN_MISC))
- && (left->op_type == OP_LIST
- || (left->op_type == OP_NULL && left->op_targ == OP_LIST)))
- {
- OP* lop = ((LISTOP*)left)->op_first;
- while (lop) {
- if (lop->op_type == OP_PADSV ||
- lop->op_type == OP_PADAV ||
- lop->op_type == OP_PADHV ||
- lop->op_type == OP_PADANY)
- {
- if (lop->op_private & OPpPAD_STATE) {
- if (left->op_private & OPpLVAL_INTRO) {
- o->op_private |= OPpASSIGN_STATE;
- /* hijacking PADSTALE for uninitialized state variables */
- SvPADSTALE_on(PAD_SVl(lop->op_targ));
- }
- else { /* we already checked for WARN_MISC before */
- Perl_warner(aTHX_ packWARN(WARN_MISC), "State variable %s will be reinitialized",
- PAD_COMPNAME_PV(lop->op_targ));
- }
- }
- }
- lop = lop->op_sibling;
- }
- }
- else if (((left->op_private & (OPpLVAL_INTRO | OPpPAD_STATE))
- == (OPpLVAL_INTRO | OPpPAD_STATE))
- && ( left->op_type == OP_PADSV
- || left->op_type == OP_PADAV
- || left->op_type == OP_PADHV
- || left->op_type == OP_PADANY))
- {
- o->op_private |= OPpASSIGN_STATE;
- /* hijacking PADSTALE for uninitialized state variables */
- SvPADSTALE_on(PAD_SVl(left->op_targ));
- }
-
if (right && right->op_type == OP_SPLIT) {
OP* tmpop = ((LISTOP*)right)->op_first;
if (tmpop && (tmpop->op_type == OP_PUSHRE)) {
return kid;
}
}
- if (kid->op_sibling) {
- OP *kkid = kid->op_sibling;
- if (kkid->op_type == OP_PADSV
- && (kkid->op_private & OPpLVAL_INTRO)
- && SvPAD_STATE(*av_fetch(PL_comppad_name, kkid->op_targ, FALSE))) {
- o->op_private |= OPpASSIGN_STATE;
- /* hijacking PADSTALE for uninitialized state variables */
- SvPADSTALE_on(PAD_SVl(kkid->op_targ));
- }
- }
return o;
}
#define OPpASSIGN_BACKWARDS 64 /* Left & right switched. */
#define OPpASSIGN_CV_TO_GV 128 /* Possible optimisation for constants. */
-/* Private for OP_[AS]ASSIGN */
-#define OPpASSIGN_STATE 32 /* Assign to a "state" variable */
-
/* Private for OP_MATCH and OP_SUBST{,CONST} */
#define OPpRUNTIME 64 /* Pattern coming in on the stack */
iterate more times than there are characters of input, which is what
happened.) See L<perlfunc/split>.
-=item State variable %s will be reinitialized
-
-(W misc) You're declaring a C<state> variable inside a list. The list
-assignment will be treated by perl as a regular assignment, which means
-that the C<state> variable will be reinitialized each time the statement
-is run. The solution to have it initialized only once is to write the
-assignment on its own line, as in:
-
- state $var = 42;
-
=item Statement unlikely to be reached
(W exec) You did an exec() with some statement after it other than a
=item state TYPE EXPR : ATTRS
C<state> declares a lexically scoped variable, just like C<my> does.
-However, those variables will be initialized only once, contrary to
+However, those variables will never be reinitialized, contrary to
lexical variables that are reinitialized each time their enclosing block
is entered.
Also, since C<$x> is lexical, it can't be reached or modified by any Perl
code outside.
-You can initialize state variables, and the assigment will be executed
-only once:
+Be aware that assignment to C<state> variables (as in C<state $x = 42>)
+are executed every time; to initialize (or re-initialize) an undefined
+state scalar, you can use, for example, the defined-or assignment :
- sub starts_from_42 { state $x = 42; return ++$x }
-
-You can also, as a syntactic shortcut, initialize more than one if they're
-all declared within the same state() clause:
-
- state ($a, $b, $c) = ( 'one', 'two', 'three' );
-
-However, be warned that state variables declared as part of a list will
-get assigned each time the statement will be executed, since it will be
-considered as a regular list assigment, not one to be executed only once:
-
- (state $x, my $y) = (1, 2); # $x gets reinitialized every time !
-
-B<Caveat>: the code at the right side of the assignment to a state
-variable will be executed every time; only the assignment is disabled. So,
-avoid code that has side-effects, or that is slow to execute. This might
-be optimized out in a future version of Perl.
+ state $x //= initial_value();
=head3 Persistent variables with closures
SV * const temp = left;
left = right; right = temp;
}
- else if (PL_op->op_private & OPpASSIGN_STATE) {
- if (SvPADSTALE(right))
- SvPADSTALE_off(right);
- else {
- (void)POPs;
- PUSHs(right);
- RETURN; /* ignore assignment */
- }
- }
if (PL_tainting && PL_tainted && !SvTAINTED(left))
TAINT_NOT;
if (PL_op->op_private & OPpASSIGN_CV_TO_GV) {
int duplicates = 0;
SV **firsthashrelem = NULL; /* "= 0" keeps gcc 2.95 quiet */
- if (PL_op->op_private & OPpASSIGN_STATE) {
- if (SvPADSTALE(*firstlelem))
- SvPADSTALE_off(*firstlelem);
- else
- RETURN; /* ignore assignment */
- }
-
PL_delaymagic = DM_DELAY; /* catch simultaneous items */
gimme = GIMME_V;
Deprecated use of my() in false conditional at - line 7.
Deprecated use of my() in false conditional at - line 8.
Deprecated use of my() in false conditional at - line 9.
-########
-# op.c
-use feature 'state';
-use warnings 'misc';
-state($x) = 1;
-(state $y) = 2;
-(state $z, my $t) = (3, 4);
-(state $foo, state $bar) = (5, 6);
-(undef, my $v, state $w) = (7 .. 9);
-no warnings 'misc';
-state($x) = 1;
-(state $y) = 2;
-(state $z, my $t) = (3, 4);
-(state $foo, state $bar) = (5, 6);
-(undef, my $v, state $w) = (7 .. 9);
-EXPECT
-State variable $z will be reinitialized at - line 6.
-State variable $foo will be reinitialized at - line 7.
-State variable $bar will be reinitialized at - line 7.
-State variable $w will be reinitialized at - line 8.
use strict;
use feature "state";
-plan tests => 46;
+plan tests => 37;
ok( ! defined state $uninit, q(state vars are undef by default) );
sub stateful {
state $x;
- state $y = 1;
+ state $y //= 1;
my $z = 2;
- state ($t) = 3;
+ state ($t) //= 3;
return ($x++, $y++, $z++, $t++);
}
# in a nested block
sub nesting {
- state $foo = 10;
+ state $foo //= 10;
my $t;
- { state $bar = 12; $t = ++$bar }
+ { state $bar //= 12; $t = ++$bar }
++$foo;
return ($foo, $t);
}
sub TIESCALAR {bless {}};
sub FETCH { ++$fetchcount; 18 };
tie my $y, "countfetches";
- sub foo { state $x = $y; $x++ }
+ sub foo { state $x //= $y; $x++ }
::is( foo(), 18, "initialisation with tied variable" );
::is( foo(), 19, "increments correctly" );
::is( foo(), 20, "increments correctly, twice" );
sub gen_cashier {
my $amount = shift;
- state $cash_in_store = 0;
+ state $cash_in_store;
return {
add => sub { $cash_in_store += $amount },
del => sub { $cash_in_store -= $amount },
# stateless assignment to a state variable
sub stateless {
- no warnings 'misc';
- (state $reinitme, my $foo) = (42, 'bar');
+ state $reinitme = 42;
++$reinitme;
}
is( stateless(), 43, 'stateless function, first time' );
$xsize = stateful_array();
is( $xsize, 1, 'uninitialized state array after one iteration' );
-sub stateful_array_init {
- state @x = (1, 2);
- push @x, 'x';
- return $#x;
-}
-
-$xsize = stateful_array_init();
-is( $xsize, 2, 'initialized state array' );
-
-$xsize = stateful_array_init();
-is( $xsize, 3, 'initialized state array after one iteration' );
-
# hash state vars
sub stateful_hash {
$xhval = stateful_hash();
is( $xhval, 1, 'uninitialized state hash after one iteration' );
-sub stateful_hash_init {
- state %hx = (foo => 10);
- return $hx{foo}++;
-}
-
-$xhval = stateful_hash_init();
-is( $xhval, 10, 'initialized state hash' );
-
-$xhval = stateful_hash_init();
-is( $xhval, 11, 'initialized state hash after one iteration' );
-
-# state declaration with a list
-
-sub statelist {
- # note that this should be a state assignment, while (state $lager, state $stout) shouldn't
- state($lager, $stout) = (11, 22);
- $lager++;
- $stout++;
- "$lager/$stout";
-}
-
-my $ls = statelist();
-is($ls, "12/23", 'list assignment to state scalars');
-$ls = statelist();
-is($ls, "13/24", 'list assignment to state scalars');
-
-sub statelist2 {
- state($sherry, $bourbon) = (1 .. 2);
- $sherry++;
- $bourbon++;
- "$sherry/$bourbon";
-}
-
-$ls = statelist2();
-is($ls, "2/3", 'list assignment to state scalars');
-$ls = statelist2();
-is($ls, "3/4", 'list assignment to state scalars');
-
# Recursion
sub noseworth {
sub pugnax { my $x = state $y = 42; $y++; $x; }
is( pugnax(), 42, 'scalar state assignment return value' );
-is( pugnax(), 43, 'scalar state assignment return value' );