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