One can predeclare subroutines with |
sub name; # may be used before implementation appears |
Subroutines may be defined anonymously: |
sub newprint { |
my $x = shift; |
# return anonymous subroutine: |
return { my $y=shift; print "$x $y!\n"; }; |
} |
$h = newprint("Howdy"); # store anonymous subroutine |
&$h("World"); # call anonymous subroutine, which prints |
"Howdy World!" |
Note the $x in anonymous subroutine is private and retains value "Howdy" in $h even when newprint is called again: |
$g = newprint("Hello"); # $g has separate instance of $x |