100 likes | 261 Views
Perl: Functions, References, Etc. Functions Calling forms Argument fetching References: “intelligent pointers” Packages and Modules. Perl: User Defined Functions. sub funny_print { my ($name, $age) = @_; print <<END_FUNNY_PRINT; The person’s name is $name, and the age is $age.
E N D
Perl: Functions, References, Etc. • Functions • Calling forms • Argument fetching • References: “intelligent pointers” • Packages and Modules CSE 341 S. Tanimoto Perl-Functions, Refs, etc. -
Perl: User Defined Functions sub funny_print { my ($name, $age) = @_; print <<END_FUNNY_PRINT; The person\’s name is $name, and the age is $age. END_FUNNY_PRINT return 1; } funny_print ("Abe", 50); # or &funny_print ("Abe", 50); # or funny_print "Abe", 50; # This last form is OK AFTER the defn. CSE 341 S. Tanimoto Perl-Functions, Refs, etc. -
Perl: Pronouns $_ # “It” (scalar default variable) @people = ("John", "Tran", "Pam"); foreach $person (@people) { print $person, " "; } foreach (@people) { print $_, " "; } foreach $person (@people) { print; print " "; } John Tran Pam John Tran Pam John Tran Pam CSE 341 S. Tanimoto Perl-Functions, Refs, etc. -
Perl: Pronouns @_ # “Them” (array default variable) funny_print(Abe, 191); sub funny_print { my ($name, $age) = @_; # my $name = $_[0]; my $age = $_[1]; ... } CSE 341 S. Tanimoto Perl-Functions, Refs, etc. -
Fetching Function Args w/ shift sub add1 { my $number = shift; return $number + 1; } add1(5); # shift removes and returns the first # element of an array, and the default # array is @_ CSE 341 S. Tanimoto Perl-Functions, Refs, etc. -
Packages #!/usr/bin/perl -w Package OakTrees; sub getseeds { print "acorns"; } Package PalmTrees; sub getseeds { print "coconuts" ; } getseeds() ; # prints coconuts; OakTrees::getseeds(); # prints acorns PalmTrees::getseeds(): # prints coconuts # A package is a separate namespace for # functions and variables. # To cross packages use fully-qualified name CSE 341 S. Tanimoto Perl-Functions, Refs, etc. -
Modules #!/usr/bin/perl -w use MapleTreesModule; # The compiler searches for MapleTreesModule.pm # following all the paths in an array called @INC. # The module is a precompiled package of the same name. # When the “use” is executed, all the definitions in the # package become available, and any other code in # the package gets executed. # Packages can be in subdirectories of searched paths: use Net::Mail; # Mail.pm must be in Net. CSE 341 S. Tanimoto Perl-Functions, Refs, etc. -
References (smart pointers) A reference is a pointer that is used to access some Perl data or function. There is a reference count for each item. $scalarref = \$scalar; $five = 5; $fiveref = \$five; print 1 + $$fiveref; # prints 6. CSE 341 S. Tanimoto Perl-Functions, Refs, etc. -
References (continued) Local variables persist even if execution exits their scope if their reference count is greater than zero. (I.e., local variables can have indefinite extent.) References can be used for scalars, arrays, hashes, functions and other references. $scalar = "A Scalar"; $hash{"KEY"} = "A hash entry"; $scalarref = \$scalar; $hashref = \%hash; CSE 341 S. Tanimoto Perl-Functions, Refs, etc. -
Anonymous Variables sub someTrees { my @threeTrees = qw(oak pine elm); return \@threeTrees; } $treesRef = someTrees(); sub moreTrees { ["banyan”, "beech"]; # anonymous } # both functions return references. CSE 341 S. Tanimoto Perl-Functions, Refs, etc. -