diff options
author | Rafael Garcia-Suarez <rgarciasuarez@gmail.com> | 2007-04-24 10:31:28 +0000 |
---|---|---|
committer | Rafael Garcia-Suarez <rgarciasuarez@gmail.com> | 2007-04-24 10:31:28 +0000 |
commit | b708784ed29bddb36a466db5f726fe7b7635e19b (patch) | |
tree | 92a122ed8f06a2782dcf5b8db39a49b27343fddb /pod | |
parent | 0d746ee5c1be80219aec2680f655ad5be66ccdf0 (diff) | |
download | perl-b708784ed29bddb36a466db5f726fe7b7635e19b.tar.gz |
Remove the code that handles assignment to state variables
p4raw-id: //depot/perl@31049
Diffstat (limited to 'pod')
-rw-r--r-- | pod/perldiag.pod | 10 | ||||
-rw-r--r-- | pod/perlfunc.pod | 2 | ||||
-rw-r--r-- | pod/perlsub.pod | 23 |
3 files changed, 5 insertions, 30 deletions
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 |