Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl sub inside a sub

Tags:

perl

I want to have a sub inside another sub,

sub a {
    sub b {
    }
}

I want to create a new instance of sub b for every call to sub a. Is there a way to do this in Perl?

When I run the code above and print the address of sub b in sub a I always get the same address for sub b like

sub a {
    print \&b;
    sub b{
    }
}

This link on Perl Monks says that we can do this, but I always see the same address for sub b.

Is there a way to create a new instance of sub b for every call to sub a?

like image 913
PMat Avatar asked Oct 13 '12 14:10

PMat


1 Answers

sub a {
    sub b{
    }
}

is basically the same as:

sub a {

}
sub b{
}

because named subroutines live in the symbol table hence they are global. you will need to return a reference to a subroutine.

like image 111
snoofkin Avatar answered Oct 07 '22 21:10

snoofkin