deewanagan has asked for the wisdom of the Perl Monks concerning the following question:

hey monks!how can i do this:i have one sub function, which has 5 other sub functions implemented inside it.Now the big sub function, takes parameters and pass it to the first (inside) sub function, then the output of that sub function should be given as the input for the second function,and so on.can u help me?

Replies are listed 'Best First'.
Re: sub fuction inside sub functioin?
by ikegami (Patriarch) on Dec 07, 2008 at 12:59 UTC
    Don't put named subs inside of other named subs, but anonymous subs are ok.
    sub outer { my ($param) = @_; local *inner1 = sub { ... }; local *inner2 = sub { ... }; ... inner2(inner1($param)); }
      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

        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.

Re: sub fuction inside sub functioin?
by GrandFather (Saint) on Dec 07, 2008 at 19:49 UTC
    use strict; use warnings; print join "\n", myBigSub (1 .. 10); sub myBigSub { my @params = &myLittleSub1; @params = myLittleSub4 (myLittleSub3 (myLittleSub2 (@params))); @params = myLittleSub5 (@params); return @params; } sub myLittleSub1 { my (@params) = @_; return reverse @params; } sub myLittleSub2 { return map {$_ * 3} @_; } sub myLittleSub3 { my @params = grep {$_ % 6} @_; return @params; } sub myLittleSub4 { my @params = @_; return @params[1, 2, 4]; } sub myLittleSub5 { my (@params) = @_; $_ = 'x' x $_ for @params; return @params; }

    Prints:

    xxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxx

    Perl's payment curve coincides with its learning curve.
Re: sub fuction inside sub functioin?
by Anonymous Monk on Dec 07, 2008 at 12:58 UTC
Re: sub fuction inside sub functioin?
by spmlingam (Scribe) on Dec 08, 2008 at 12:02 UTC
    This might help you

    use strict; use warnings; sub FirstSub { my ($Param) = @_; sub add1 {my ($arg)=@_; return $arg+1;}; sub add2 {my ($arg)=@_; return $arg+2;}; sub add3 {my ($arg)=@_; return $arg+3;}; sub add4 {my ($arg)=@_; return $arg+4;}; sub add5 {my ($arg)=@_; return $arg+5;}; $Param = add1($Param); $Param = add2($Param); $Param = add3($Param); $Param = add4($Param); $Param = add5($Param); return $Param; } print FirstSub(10);