summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--op.c48
-rw-r--r--op.h3
-rw-r--r--pod/perldiag.pod10
-rw-r--r--pod/perlfunc.pod2
-rw-r--r--pod/perlsub.pod23
-rw-r--r--pp_hot.c16
-rw-r--r--t/lib/warnings/op20
-rw-r--r--t/op/state.t68
8 files changed, 13 insertions, 177 deletions
diff --git a/op.c b/op.c
index befacc38c0..8cf8cd9c5c 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 c01bf4d03d..f53c7a503e 100644
--- a/op.h
+++ b/op.h
@@ -166,9 +166,6 @@ Deprecated. Use C<GIMME_V> 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 6285c71fc4..1ba0c46fd0 100644
--- a/pod/perldiag.pod
+++ b/pod/perldiag.pod
@@ -3902,16 +3902,6 @@ L<perlfunc/splice>.
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
diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod
index 599017b3f5..d7d9044e7b 100644
--- a/pod/perlfunc.pod
+++ b/pod/perlfunc.pod
@@ -6011,7 +6011,7 @@ X<state>
=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.
diff --git a/pod/perlsub.pod b/pod/perlsub.pod
index 5ecd346a14..a04dfc95c4 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<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
diff --git a/pp_hot.c b/pp_hot.c
index 71bbb5c03f..dfa19fd11a 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 8dadc7aa85..891677fabe 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 57b46ab32a..fb2880d852 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' );