X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlsub.pod;h=74d0b1ac26fed091d13a05a0fbfd40c241606d29;hb=3c4fb04a912b266806354630dd98a7e36a830fbe;hp=4c7dcd620914c0309652aab7790ce140f6eac290;hpb=d822fdf9523774354b4abafec1aa0c8639788575;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlsub.pod b/pod/perlsub.pod index 4c7dcd6..74d0b1a 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 @@ -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 @@ -521,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 @@ -1049,7 +1034,7 @@ 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 @@ -1098,9 +1083,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 @@ -1312,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; } @@ -1367,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.