1 / 13

Perl: User Defined Functions

Perl: User Defined Functions. sub funny_print { my ($name, $age) = @_; print <<END_FUNNY_PRINT; The person’s name is $name, and the ages is $age. END_FUNNY_PRINT return 1; } funny_print ("Steve", 50); # or &funny_print("Steve", 50); funny_print "Steve", 50; # or

georgehall
Télécharger la présentation

Perl: User Defined Functions

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: User Defined Functions sub funny_print { my ($name, $age) = @_; print <<END_FUNNY_PRINT; The person\’s name is $name, and the ages is $age. END_FUNNY_PRINT return 1; } funny_print ("Steve", 50); # or &funny_print("Steve", 50); funny_print "Steve", 50; # or # This last form is OK AFTER the defn. CSE 341 S. Tanimoto Perl-2 -

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

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

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

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

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

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

  8. Regular Expressions in Perl $sentence = "Spring weather has arrived." if ($sentence =~ /weather/) { print "Never bet on the weather." ; } # $string =~ /Pattern/ The result of this kind of pattern matching is a true or false value. CSE 341 S. Tanimoto Perl-2 -

  9. Specifying Patterns /Pattern/ # Literal text; # true if it occurs anywhere in the string. /^Pattern/ # Must occur at the beginning. "Pattern recognition is alive" =~ /^Pattern/ "The end" =~ /end$/ \s whitespace \S non-whitespace \w a word char. \W a non-word char. [a-zA-Z_0-9] \d a digit \D a non-digit \b word boundary \B not word boundary CSE 341 S. Tanimoto Perl-2 -

  10. Specifying Patterns (Cont.) $test = "You have new mail -- 5-24-99"; if ($test =~ /^You\s.+\d+-\d+-\d+/ ) { print "The mail has arrived."; } if ($test =~ m( ^ You \s .+ \d+ - \d+ - \d+ ) { print "The mail has arrived."; } CSE 341 S. Tanimoto Perl-2 -

  11. Extracting information $test = "You have new mail -- 5-24-99"; if ($test =~ /^You\s.+(\d+)-(\d+)-(\d+)/ ) { print "The mail has arrived on "; print "day $2 of month $1 in year $3.\n"; } # Parentheses in the pattern establish # variables $1, $2, $3, etc. to hold # corresponding matched fragments. CSE 341 S. Tanimoto Perl-2 -

  12. Search and Replace $sntc = "We surfed the waves the whole day." $sntc =~ s/surfed/sailed/; print $sntc; # We sailed the waves the whole day. $sntc =~ s/the//g; print $sntc; # We sailed waves whole day. # g makes the replacement “global”. CSE 341 S. Tanimoto Perl-2 -

  13. Interpolation of Variables in Patterns. $exclamation = "yeah"; $sntc = "We had fun." $sntc =~ s/\w+/$exclamation/g; print $sntc; # yeah yeah yeah. # a pattern can contain a Perl variable. CSE 341 S. Tanimoto Perl-2 -

More Related