Basic HTML version of Foils prepared Sept 20 97

Foil 25 Some Remarks about Subroutines - II

From Perl5 and Advanced Perl4 Features Computational Science for Information Age Course CPS616 -- Sept 20 97. by Geoffrey C. Fox,Nancy McCracken,Tom Scavo


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



© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Sun Apr 5 1998