X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlsub.pod;h=a04dfc95c4678dbf1f28e6649e76e79a28060b87;hb=2095dafae09cfface71d4202b3188926ea0ccc1c;hp=9711ca6c0da737b8c854651dde592d40faa24f01;hpb=dbb128be9d98f2152d3ce957d4c3c518a9f86260;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlsub.pod b/pod/perlsub.pod index 9711ca6..a04dfc9 100644 --- a/pod/perlsub.pod +++ b/pod/perlsub.pod @@ -83,9 +83,9 @@ aggregates (arrays and hashes), these will be flattened together into one large indistinguishable list. If no C is found and if the last statement is an expression, its -value is returned. Otherwise, if the last statement is a control structure -like a C, the returned value is unspecified. The empty sub -returns the empty list. +value is returned. If the last statement is a loop control structure +like a C or a C, the returned value is unspecified. The +empty sub returns the empty list. X X X Perl does not have named formal parameters. In practice all you @@ -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 @@ -394,7 +394,6 @@ never fully qualified with the package name. In particular, you're not allowed to try to make a package variable (or other global) lexical: my $pack::var; # ERROR! Illegal syntax - my $_; # also illegal (currently) In fact, a dynamic variable (also known as package or global variables) are still accessible using the fully qualified C<::> notation even while a @@ -432,7 +431,35 @@ 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. + +Be aware that assignment to C variables (as in C) +are executed every time; to initialize (or re-initialize) an undefined +state scalar, you can use, for example, the defined-or assignment : + + state $x //= initial_value(); + +=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 @@ -479,8 +506,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 @@ -1012,7 +1040,7 @@ corresponding built-in. 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 @@ -1056,9 +1084,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 @@ -1325,7 +1357,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. @@ -1400,17 +1433,17 @@ nest properly. Examples of valid syntax (even though the attributes are unknown): - sub fnord (&\%) : switch(10,foo(7,3)) : expensive ; - sub plugh () : Ugly('\(") :Bad ; + sub fnord (&\%) : switch(10,foo(7,3)) : expensive; + sub plugh () : Ugly('\(") :Bad; sub xyzzy : _5x5 { ... } Examples of invalid syntax: - sub fnord : switch(10,foo() ; # ()-string not balanced - sub snoid : Ugly('(') ; # ()-string not balanced - sub xyzzy : 5x5 ; # "5x5" not a valid identifier - sub plugh : Y2::north ; # "Y2::north" not a simple identifier - sub snurt : foo + bar ; # "+" not a colon or space + sub fnord : switch(10,foo(); # ()-string not balanced + sub snoid : Ugly('('); # ()-string not balanced + sub xyzzy : 5x5; # "5x5" not a valid identifier + sub plugh : Y2::north; # "Y2::north" not a simple identifier + sub snurt : foo + bar; # "+" not a colon or space The attribute list is passed as a list of constant strings to the code which associates them with the subroutine. In particular, the second example