X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlsub.pod;h=72b28f1add448ff31cdca3981780e774517aaade;hb=0c42fe95656e99f238a0bcf90ab2476c175615b7;hp=f11f1ae7130de588e18b97e24f6c48b5abe737df;hpb=3c10abe350e3df50f8ef0ac37c9d14175bc899f1;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlsub.pod b/pod/perlsub.pod index f11f1ae..72b28f1 100644 --- a/pod/perlsub.pod +++ b/pod/perlsub.pod @@ -453,26 +453,10 @@ 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: - - 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. +When combined with variable declaration, simple scalar assignment to C +variables (as in C) is executed only the first time. When such +statements are evaluated subsequent times, the assignment is ignored. The +behavior of this sort of assignment to non-scalar variables is undefined. =head3 Persistent variables with closures @@ -1317,10 +1301,9 @@ that understands regular expressions. sub glob { my $pat = shift; my @got; - local *D; - if (opendir D, '.') { - @got = grep /$pat/, readdir D; - closedir D; + if (opendir my $d, '.') { + @got = grep /$pat/, readdir $d; + closedir $d; } return @got; } @@ -1372,7 +1355,8 @@ And, as you'll have noticed from the previous example, if you override C, the C<< <*> >> glob operator is overridden as well. In a similar fashion, overriding the C function also overrides -the equivalent I/O operator C<< >>. +the equivalent I/O operator C<< >>. Also, overriding +C also overrides the operators C<``> and C. Finally, some built-ins (e.g. C or C) can't be overridden.