In perl.git, the branch blead has been updated

<http://perl5.git.perl.org/perl.git/commitdiff/67bf5a37856bcd50cc76a9af8d4264b205a27c1b?hp=fd998cbffc88a8e50fa34259c36a8db338168383>

- Log -----------------------------------------------------------------
commit 67bf5a37856bcd50cc76a9af8d4264b205a27c1b
Author: Lukas Mai <[email protected]>
Date:   Sun Jul 30 17:17:13 2017 +0200

    perlsub: don't recommend leaky code for recursive "my" subs
-----------------------------------------------------------------------

Summary of changes:
 pod/perlsub.pod | 43 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 32 insertions(+), 11 deletions(-)

diff --git a/pod/perlsub.pod b/pod/perlsub.pod
index 434bb8c7b9..689d4a3837 100644
--- a/pod/perlsub.pod
+++ b/pod/perlsub.pod
@@ -1073,25 +1073,46 @@ declared, and only after that declaration:
     no warnings "experimental::lexical_subs";
     use feature 'lexical_subs';
 
-    foo();             # calls the package/global subroutine
+    foo();              # calls the package/global subroutine
     state sub foo {
-       foo();          # also calls the package subroutine
+        foo();          # also calls the package subroutine
     }
-    foo();             # calls "state" sub
-    my $ref = \&foo;   # take a reference to "state" sub
+    foo();              # calls "state" sub
+    my $ref = \&foo;    # take a reference to "state" sub
 
     my sub bar { ... }
-    bar();             # calls "my" sub
+    bar();              # calls "my" sub
 
-To use a lexical subroutine from inside the subroutine itself, you must
-predeclare it.  The C<sub foo {...}> subroutine definition syntax respects
-any previous C<my sub;> or C<state sub;> declaration.
+You can't (directly) write a recursive lexical subroutine:
 
-    my sub baz;                # predeclaration
-    sub baz {          # define the "my" sub
-       baz();          # recursive call
+    # WRONG
+    my sub baz {
+        baz();
     }
 
+This example fails because C<baz()> refers to the package/global subroutine
+C<baz>, not the lexical subroutine currently being defined.
+
+The solution is to use L<C<__SUB__>|perlfunc/__SUB__>:
+
+    my sub baz {
+        __SUB__->();    # calls itself
+    }
+
+It is possible to predeclare a lexical subroutine.  The C<sub foo {...}>
+subroutine definition syntax respects any previous C<my sub;> or C<state sub;>
+declaration.  Using this to define recursive subroutines is a bad idea,
+however:
+
+    my sub baz;         # predeclaration
+    sub baz {           # define the "my" sub
+        baz();          # WRONG: calls itself, but leaks memory
+    }
+
+Just like C<< my $f; $f = sub { $f->() } >>, this example leaks memory.  The
+name C<baz> is a reference to the subroutine, and the subroutine uses the name
+C<baz>; they keep each other alive (see L<perlref/Circular References>).
+
 =head3 C<state sub> vs C<my sub>
 
 What is the difference between "state" subs and "my" subs?  Each time that

--
Perl5 Master Repository

Reply via email to