X-Git-Url: https://perl5.git.perl.org/perl5.git/blobdiff_plain/36fb85f3330d45eeaf9d312c267fad71d5354c6c..2b32d74cef00e0bba7871a1c7dc3f66b07049e2c:/pod/perlsub.pod diff --git a/pod/perlsub.pod b/pod/perlsub.pod index 082d520..325c823 100644 --- a/pod/perlsub.pod +++ b/pod/perlsub.pod @@ -227,10 +227,10 @@ indirectly by the run-time system itself, usually due to a triggered event. Subroutines that do special, pre-defined things include C, C, C plus all functions mentioned in L and L. -The C, C, C and C subroutines are not so much -subroutines as named special code blocks, of which you can have more -than one in a package, and which you can B call explicitly. See -L +The C, C, C, C and C subroutines +are not so much subroutines as named special code blocks, of which you +can have more than one in a package, and which you can B call +explicitly. See L =head2 Private Variables via my() X X X X X @@ -431,7 +431,34 @@ L for something of a work-around to this. =head2 Persistent Private Variables -X X X X +X X X X X X + +There are two ways to build persistent private variables in Perl 5.10. +First, you can simply use the C feature. Or, you can use closures, +if you want to stay compatible with releases older than 5.10. + +=head3 Persistent variables via state() + +Beginning with perl 5.9.4, you can declare variables with the C +keyword in place of C. For that to work, though, you must have +enabled that feature beforehand, either by using the C pragma, or +by using C<-E> on one-liners. (see L) + +For example, the following code maintains a private counter, incremented +each time the gimme_another() function is called: + + use feature 'state'; + sub gimme_another { state $x; return ++$x } + +Also, since C<$x> is lexical, it can't be reached or modified by any Perl +code outside. + +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 Just because a lexical variable is lexically (also called statically) scoped to its enclosing block, C, or C FILE, this doesn't mean that @@ -478,8 +505,9 @@ starts to run: } } -See L about the -special triggered code blocks, C, C, C and C. +See L about the +special triggered code blocks, C, C, C, +C and C. If declared at the outermost scope (the file scope), then lexicals work somewhat like C's file statics. They are available to all @@ -507,6 +535,7 @@ Synopsis: local @oof = @bar; # make @oof dynamic, and init it local $hash{key} = "val"; # sets a local value for this hash entry + delete local $hash{key}; # delete this entry for the current block local ($cond ? $v1 : $v2); # several types of lvalues support # localization @@ -664,6 +693,55 @@ Perl will print The behavior of local() on non-existent members of composite types is subject to change in future. +=head3 Localized deletion of elements of composite types +X X X X + +You can use the C and C +constructs to delete a composite type entry for the current block and restore +it when it ends. They return the array/hash value before the localization, +which means that they are respectively equivalent to + + do { + my $val = $array[$idx]; + local $array[$idx]; + delete $array[$idx]; + $val + } + +and + + do { + my $val = $hash{key}; + local $hash{key}; + delete $hash{key}; + $val + } + +except that for those the C is scoped to the C block. Slices are +also accepted. + + my %hash = ( + a => [ 7, 8, 9 ], + b => 1, + ) + + { + my $a = delete local $hash{a}; + # $a is [ 7, 8, 9 ] + # %hash is (b => 1) + + { + my @nums = delete local @$a[0, 2] + # @nums is (7, 9) + # $a is [ undef, 8 ] + + $a[0] = 999; # will be erased when the scope ends + } + # $a is back to [ 7, 8, 9 ] + + } + # %hash is back to its original state + =head2 Lvalue subroutines X X @@ -1006,12 +1084,12 @@ corresponding built-in. sub myreverse (@) myreverse $a, $b, $c sub myjoin ($@) myjoin ":", $a, $b, $c sub mypop (\@) mypop @array - sub mysplice (\@$$@) mysplice @array, @array, 0, @pushme + sub mysplice (\@$$@) mysplice @array, 0, 2, @pushme sub mykeys (\%) mykeys %{$hashref} sub myopen (*;$) myopen HANDLE, $name sub mypipe (**) mypipe READHANDLE, WRITEHANDLE sub mygrep (&@) mygrep { /foo/ } $a, $b, $c - sub myrand ($) myrand 42 + sub myrand (;$) myrand 42 sub mytime () mytime Any backslashed prototype character represents an actual argument @@ -1055,9 +1133,13 @@ follows: ... } -A semicolon separates mandatory arguments from optional arguments. +A semicolon (C<;>) separates mandatory arguments from optional arguments. It is redundant before C<@> or C<%>, which gobble up everything else. +As the last character of a prototype, or just before a semicolon, you can +use C<_> in place of C<$>: if this argument is not provided, C<$_> will be +used instead. + Note how the last three examples in the table above are treated specially by the parser. C is parsed as a true list operator, C is parsed as a true unary operator with unary @@ -1269,10 +1351,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; } @@ -1324,7 +1405,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.