in reply to Re: sub fuction inside sub functioin?
in thread sub fuction inside sub functioin?

hey you name them, they are not anonymous! ; )

This trick with local is nice! 8 )

But without trying it, I'll expect that you'll vivify global entries in the symbol table. (which are of course empty after the local scope ended)

I'd rather prefer this construction

my $inner1_cr=sub {...} ; $inner_cr->($param);

Cheers Rolf

Replies are listed 'Best First'.
Re^3: sub fuction inside sub functioin?
by ikegami (Patriarch) on Dec 07, 2008 at 20:39 UTC

    hey you name them, they are not anonymous! ; )

    Not when its compiled, and that makes a difference.

    >perl -c -we"sub outer { my $x; sub inner { $x } inner() }" Variable "$x" will not stay shared at -e line 1. -e syntax OK >perl -c -we"sub outer { my $x; local *inner = sub { $x }; inner() }" -e syntax OK

    And yeah, it's effectively a named sub thereafter. That's a bonus.

    I'd rather prefer this construction

    I usually use inner subs for recursive functions, and my would cause a memory leak there. It also needlessly complicates the calling syntax.

    sub outer { ... my $helper; $helper = sub { # $helper references helper sub ... $helper->(); # helper sub references $helper ... }; ... }

    Cyclic reference ⇒ memory leak.

      Recursions with inner subs are a very special case! I agree this should be handeld with care, unfortunately reference counting is not the best garbage collection technique...

      but using the global namespace for an inner function is somehow dangerous. What if you call an external function within outer, that tries to call a completely different function also called inner()?

      Maybe you should better use package to choose another namespace within outer() in combination with this local *name strategie to prevent this trap!

      Cheers Rolf

      UPDATE: Well OK, at least you get a warning ...

      $\="\n"; sub inner { print "global_inner" } inner(); sub whatever { inner() } sub outer { local *inner=sub {print "inner_of_outer"}; inner(); whatever(); } outer(); __END__ Subroutine main::inner redefined at c:\perl\push.pm line 9. global_inner inner_of_outer inner_of_outer

        but using the global namespace for an inner function is somehow dangerous. What if you call an external function within outer, that tries to call a completely different function also called inner()?

        First, there's no such thing as the global namespace. The code uses the current namespace.

        Secondly, are you seriously complaining that a script can't have two functions with the same name? Or did you mean to test something like

        use strict; use warnings; $\ = "\n"; sub outer2 { local *inner = sub { print "inner_of_outer2" }; inner(); } sub outer1 { local *inner = sub { print "inner_of_outer1" }; inner(); outer2(); } outer1();

        Unfortunately, that gives a warning.

        inner_of_outer1 Subroutine main::inner redefined at script.pl line 7. inner_of_outer2