This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Dave remarks that I have been too terse here.
[perl5.git] / pod / perlsub.pod
index f11f1ae..a04dfc9 100644 (file)
@@ -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
 
@@ -1372,7 +1357,8 @@ And, as you'll have noticed from the previous example, if you override
 C<glob>, the C<< <*> >> glob operator is overridden as well.
 
 In a similar fashion, overriding the C<readline> function also overrides
-the equivalent I/O operator C<< <FILEHANDLE> >>.
+the equivalent I/O operator C<< <FILEHANDLE> >>. Also, overriding
+C<readpipe> also overrides the operators C<``> and C<qx//>.
 
 Finally, some built-ins (e.g. C<exists> or C<grep>) can't be overridden.