perl.cvs.perlfaq
[Top] [All Lists]

[svn:perlfaq] r11447 - perlfaq/trunk

Subject: [svn:perlfaq] r11447 - perlfaq/trunk
From: comdog@xxxxxxxxxxxx
Date: Mon, 23 Jun 2008 10:38:50 -0700 (PDT)
Newsgroups: perl.cvs.perlfaq

Author: comdog
Date: Mon Jun 23 10:38:50 2008
New Revision: 11447

Modified:
   perlfaq/trunk/perlfaq7.pod

Log:
* perlfaq5: What's the difference between calling a function as &foo and foo()?
        + replaced answer with one with examples


Modified: perlfaq/trunk/perlfaq7.pod
==============================================================================
--- perlfaq/trunk/perlfaq7.pod  (original)
+++ perlfaq/trunk/perlfaq7.pod  Mon Jun 23 10:38:50 2008
@@ -682,21 +682,40 @@
 
 =head2 What's the difference between calling a function as &foo and foo()?
 
-When you call a function as C<&foo>, you allow that function access to
-your current @_ values, and you bypass prototypes.
-The function doesn't get an empty @_--it gets yours!  While not
-strictly speaking a bug (it's documented that way in L<perlsub>), it
-would be hard to consider this a feature in most cases.
-
-When you call your function as C<&foo()>, then you I<do> get a new @_,
-but prototyping is still circumvented.
-
-Normally, you want to call a function using C<foo()>.  You may only
-omit the parentheses if the function is already known to the compiler
-because it already saw the definition (C<use> but not C<require>),
-or via a forward reference or C<use subs> declaration.  Even in this
-case, you get a clean @_ without any of the old values leaking through
-where they don't belong.
+(contributed by brian d foy)
+
+Calling a subroutine as C<&foo> with no trailing parentheses ignores
+the prototype of C<foo> and passes it the current value of the argumet
+list, C<@_>. Here's an example; the C<bar> subroutine calls C<&foo>,
+which prints what its arguments list:
+
+       sub bar { &foo }
+       
+       sub foo { print "Args in foo are: @_\n" }
+       
+       bar( qw( a b c ) );
+
+When you call C<bar> with arguments, you see that C<foo> got the same C<@_>:
+
+       Args in foo are: a b c
+
+Calling the subroutine with trailing parentheses, with or without arguments,
+does not use the current C<@_> and respects the subroutine prototype. Changing
+the example to put parentheses after the call to C<foo> changes the program:
+
+       sub bar { &foo() }
+       
+       sub foo { print "Args in foo are: @_\n" }
+       
+       bar( qw( a b c ) );
+
+Now the output shows that C<foo> doesn't get the C<@_> from its caller.
+
+       Args in foo are: 
+
+The main use of the C<@_> pass-through feature is to write subroutines
+whose main job it is to call other subroutines for you. For further
+details, see L<perlsub>.
 
 =head2 How do I create a switch or case statement?
 

<Prev in Thread] Current Thread [Next in Thread>
  • [svn:perlfaq] r11447 - perlfaq/trunk, comdog <=