From: Rafael Garcia-Suarez Date: Tue, 24 Apr 2007 10:31:28 +0000 (+0000) Subject: Remove the code that handles assignment to state variables X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b708784ed29bddb36a466db5f726fe7b7635e19b;p=p5sagit%2Fp5-mst-13.2.git Remove the code that handles assignment to state variables p4raw-id: //depot/perl@31049 --- diff --git a/op.c b/op.c index befacc3..8cf8cd9 100644 --- a/op.c +++ b/op.c @@ -4064,44 +4064,6 @@ Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right) 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)) { @@ -7025,16 +6987,6 @@ Perl_ck_sassign(pTHX_ OP *o) 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; } diff --git a/op.h b/op.h index c01bf4d..f53c7a5 100644 --- a/op.h +++ b/op.h @@ -166,9 +166,6 @@ Deprecated. Use C instead. #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 */ diff --git a/pod/perldiag.pod b/pod/perldiag.pod index 6285c71..1ba0c46 100644 --- a/pod/perldiag.pod +++ b/pod/perldiag.pod @@ -3902,16 +3902,6 @@ L. iterate more times than there are characters of input, which is what happened.) See L. -=item State variable %s will be reinitialized - -(W misc) You're declaring a C variable inside a list. The list -assignment will be treated by perl as a regular assignment, which means -that the C 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 diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod index 599017b..d7d9044 100644 --- a/pod/perlfunc.pod +++ b/pod/perlfunc.pod @@ -6011,7 +6011,7 @@ X =item state TYPE EXPR : ATTRS C declares a lexically scoped variable, just like C 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. diff --git a/pod/perlsub.pod b/pod/perlsub.pod index 5ecd346..a04dfc9 100644 --- a/pod/perlsub.pod +++ b/pod/perlsub.pod @@ -453,26 +453,11 @@ each time the gimme_another() function is called: 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 variables (as in C) +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: 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 diff --git a/pp_hot.c b/pp_hot.c index 71bbb5c..dfa19fd 100644 --- a/pp_hot.c +++ b/pp_hot.c @@ -127,15 +127,6 @@ PP(pp_sassign) 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) { @@ -972,13 +963,6 @@ PP(pp_aassign) 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; diff --git a/t/lib/warnings/op b/t/lib/warnings/op index 8dadc7a..891677f 100644 --- a/t/lib/warnings/op +++ b/t/lib/warnings/op @@ -1081,23 +1081,3 @@ Deprecated use of my() in false conditional at - line 6. 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. diff --git a/t/op/state.t b/t/op/state.t index 57b46ab..fb2880d 100644 --- a/t/op/state.t +++ b/t/op/state.t @@ -10,7 +10,7 @@ BEGIN { use strict; use feature "state"; -plan tests => 46; +plan tests => 37; ok( ! defined state $uninit, q(state vars are undef by default) ); @@ -18,9 +18,9 @@ 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++); } @@ -45,9 +45,9 @@ is( $t, 5, 'incremented state var, list syntax' ); # 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); } @@ -83,7 +83,7 @@ is( $f2->(), 2, 'generator 2 once more' ); 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" ); @@ -94,7 +94,7 @@ is( $f2->(), 2, 'generator 2 once more' ); 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 }, @@ -109,8 +109,7 @@ is( gen_cashier()->{bal}->(), 42, '$42 in my drawer' ); # 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' ); @@ -130,18 +129,6 @@ is( $xsize, 0, 'uninitialized state array' ); $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 { @@ -155,44 +142,6 @@ is( $xhval, 0, 'uninitialized state 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 { @@ -208,4 +157,3 @@ noseworth(2); 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' );