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


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

in Table To:


© 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