1 |
One can predeclare subroutines with
|
2 |
sub name; # command and use before implementation appears
|
3 |
One can generate subroutines using EVAL or anonymously as in:
|
4 |
sub newprint {
-
my $x = shift;
-
return { my $y=shift; print "$x, $y!\n"; }; # return anonymous subroutine
|
5 |
}
|
6 |
$h = newprint("Howdy"); # $h Holds anonymous subroutine
|
7 |
&$h("World"); # Calls anonymous subroutine and we get
|
8 |
Howdy, World! printed
|
9 |
Note the $x in anonymous subroutine is private and retains value Howdy in $h even when you call newprint again with
|
10 |
$g= newprint("Greetings"); # $g has separate instance of $x
|