From: Rafael Garcia-Suarez Date: Thu, 6 Jul 2006 15:49:30 +0000 (+0000) Subject: Add a new warning, "State variable %s will be reinitialized" X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=53fdf963570d8de6e113571512f8002a3597d780;p=p5sagit%2Fp5-mst-13.2.git Add a new warning, "State variable %s will be reinitialized" p4raw-id: //depot/perl@28496 --- diff --git a/op.c b/op.c index 370a01f..4716788 100644 --- a/op.c +++ b/op.c @@ -3800,10 +3800,16 @@ Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right) curop->op_type == OP_PADHV || curop->op_type == OP_PADANY) { - if ((left->op_private & OPpLVAL_INTRO) && (curop->op_private & OPpPAD_STATE)) { - o->op_private |= OPpASSIGN_STATE; - /* hijacking PADSTALE for uninitialized state variables */ - SvPADSTALE_on(PAD_SVl(curop->op_targ)); + if (curop->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(curop->op_targ)); + } + else if (ckWARN(WARN_MISC)) { + Perl_warner(aTHX_ packWARN(WARN_MISC), "State variable %s will be reinitialized", + PAD_COMPNAME_PV(curop->op_targ)); + } } if (PAD_COMPNAME_GEN(curop->op_targ) == (STRLEN)PL_generation) diff --git a/pod/perldiag.pod b/pod/perldiag.pod index 8228cf9..d9aa877 100644 --- a/pod/perldiag.pod +++ b/pod/perldiag.pod @@ -3745,6 +3745,16 @@ 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 twice 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/t/lib/warnings/op b/t/lib/warnings/op index 891677f..3c10751 100644 --- a/t/lib/warnings/op +++ b/t/lib/warnings/op @@ -1081,3 +1081,20 @@ 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); +no warnings 'misc'; +state($x) = 1; +(state $y) = 2; +(state $z, my $t) = (3, 4); +(state $foo, state $bar) = (5, 6); +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.