Basic HTML version of Foils prepared April 16,1996

Foil 25 Some Remarks about Subroutines - II

From Perl5 and Advanced Perl4 Features Computational Science for Information Age Course CPS616 Material -- April 16,1996. by Geoffrey C. Fox


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



© 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 Sat Sep 6 1997