1 / 10

Perl: Functions, References, Etc.

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.

darrin
Télécharger la présentation

Perl: Functions, References, Etc.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Perl: Functions, References, Etc. • Functions • Calling forms • Argument fetching • References: “intelligent pointers” • Packages and Modules CSE 341 S. Tanimoto Perl-Functions, Refs, etc. -

  2. 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. -

  3. 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. -

  4. 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. -

  5. 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. -

  6. 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. -

  7. 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. -

  8. 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. -

  9. 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. -

  10. 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. -

More Related