Add test for grep() and wantarray
[p5sagit/p5-mst-13.2.git] / pod / perlsub.pod
index da8d00b..347d2f8 100644 (file)
@@ -149,13 +149,14 @@ Because like its flat incoming parameter list, the return list is also
 flat.  So all you have managed to do here is stored everything in @a and
 made @b an empty list.  See L</"Pass by Reference"> for alternatives.
 
-A subroutine may be called using the "&" prefix.  The "&" is optional in
-Perl 5, and so are the parentheses if the subroutine has been pre-declared.
-(Note, however, that the "&" is I<NOT> optional when you're just naming
-the subroutine, such as when it's used as an argument to defined() or
-undef().  Nor is it optional when you want to do an indirect subroutine
-call with a subroutine name or reference using the C<&$subref()> or
-C<&{$subref}()> constructs.  See L<perlref> for more on that.)
+A subroutine may be called using the "&" prefix.  The "&" is optional
+in modern Perls, and so are the parentheses if the subroutine has been
+pre-declared.  (Note, however, that the "&" is I<NOT> optional when
+you're just naming the subroutine, such as when it's used as an
+argument to defined() or undef().  Nor is it optional when you want to
+do an indirect subroutine call with a subroutine name or reference
+using the C<&$subref()> or C<&{$subref}()> constructs.  See L<perlref>
+for more on that.)
 
 Subroutines may be called recursively.  If a subroutine is called using
 the "&" form, the argument list is optional, and if omitted, no @_ array is
@@ -406,7 +407,7 @@ Synopsis:
     local *merlyn = \$randal;   # just alias $merlyn, not @merlyn etc 
 
 A local() modifies its listed variables to be local to the enclosing
-block, (or subroutine, C<eval{}> or C<do>) and I<any called from
+block, (or subroutine, C<eval{}>, or C<do>) and I<any called from
 within that block>.  A local() just gives temporary values to global
 (meaning package) variables.  This is known as dynamic scoping.  Lexical
 scoping is done with "my", which works more like C's auto declarations.
@@ -414,7 +415,7 @@ scoping is done with "my", which works more like C's auto declarations.
 If more than one variable is given to local(), they must be placed in
 parentheses.  All listed elements must be legal lvalues.  This operator works
 by saving the current values of those variables in its argument list on a
-hidden stack and restoring them upon exiting the block, subroutine or
+hidden stack and restoring them upon exiting the block, subroutine, or
 eval.  This means that called subroutines can also reference the local
 variable, but not the global one.  The argument list may be assigned to if
 desired, which allows you to initialize your local variables.  (If no
@@ -471,7 +472,7 @@ star on the front can be thought of as a wildcard match for all the
 funny prefix characters on variables and subroutines and such.
 
 When evaluated, the typeglob produces a scalar value that represents
-all the objects of that name, including any filehandle, format or
+all the objects of that name, including any filehandle, format, or
 subroutine.  When assigned to, it causes the name mentioned to refer to
 whatever "*" value was assigned to it.  Example:
 
@@ -488,7 +489,7 @@ Note that scalars are already passed by reference, so you can modify
 scalar arguments without using this mechanism by referring explicitly
 to C<$_[0]> etc.  You can modify all the elements of an array by passing
 all the elements as scalars, but you have to use the * mechanism (or
-the equivalent reference mechanism) to push, pop or change the size of
+the equivalent reference mechanism) to push, pop, or change the size of
 an array.  It will certainly be faster to pass the typeglob (or reference).
 
 Even if you don't want to modify an array, this mechanism is useful for
@@ -583,6 +584,37 @@ Here we're using the typeglobs to do symbol table aliasing.  It's
 a tad subtle, though, and also won't work if you're using my()
 variables, because only globals (well, and local()s) are in the symbol table.
 
+If you're passing around filehandles, you could usually just use the bare
+typeglob, like *STDOUT, but typeglobs references would be better because
+they'll still work properly under C<use strict 'refs'>.  For example:
+
+    splutter(\*STDOUT);
+    sub splutter {
+       my $fh = shift;
+       print $fh "her um well a hmmm\n";
+    }
+
+    $rec = get_rec(\*STDIN);
+    sub get_rec {
+       my $fh = shift;
+       return scalar <$fh>;
+    }
+
+Another way to do this is using *HANDLE{IO}, see L<perlref> for usage
+and caveats.
+
+If you're planning on generating new filehandles, you could do this:
+
+    sub openit {
+       my $name = shift;
+       local *FH;
+       return open (FH, $path) ? *FH : undef;
+    } 
+
+Although that will actually produce a small memory leak.  See the bottom
+of L<perlfunc/open()> for a somewhat cleaner way using the IO::Handle
+package.
+
 =head2 Prototypes
 
 As of the 5.002 release of perl, if you declare
@@ -722,6 +754,49 @@ starts scribbling on your @_ parameter list.
 This is all very powerful, of course, and should be used only in moderation
 to make the world a better place.  
 
+=head2 Constant Functions
+
+Functions with a prototype of C<()> are potential candidates for
+inlining.  If the result after optimization and constant folding is a
+constant then it will be used in place of new-style calls to the
+function.  Old-style calls (that is, calls made using C<&>) are not
+affected.
+
+All of the following functions would be inlined.
+
+    sub PI ()          { 3.14159 }
+    sub ST_DEV ()      { 0 }
+    sub ST_INO ()      { 1 }
+
+    sub FLAG_FOO ()    { 1 << 8 }
+    sub FLAG_BAR ()    { 1 << 9 }
+    sub FLAG_MASK ()   { FLAG_FOO | FLAG_BAR }
+    
+    sub OPT_BAZ ()     { 1 }
+    sub BAZ_VAL () {
+       if (OPT_BAZ) {
+           return 23;
+       }
+       else {
+           return 42;
+       }
+    }
+
+If you redefine a subroutine which was eligible for inlining you'll get
+a mandatory warning.  (You can use this warning to tell whether or not a
+particular subroutine is considered constant.)  The warning is
+considered severe enough not to be optional because previously compiled
+invocations of the function will still be using the old value of the
+function.  If you need to be able to redefine the subroutine you need to
+ensure that it isn't inlined, either by dropping the C<()> prototype
+(which changes the calling semantics, so beware) or by thwarting the
+inlining mechanism in some other way, such as
+
+    my $dummy;
+    sub not_inlined () {
+       $dummy || 23
+    }
+
 =head2 Overriding Builtin Functions
 
 Many builtin functions may be overridden, though this should be tried