1 / 20

Perl: Arrays, Functions, References, Etc.

Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar Functions Calling forms Argument fetching Packages and Modules References: “intelligent pointers” Anonymous Variables (e.g., lists as values). Arrays. # Scalar assignment:

Download Presentation

Perl: Arrays, 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: Arrays, Functions, References, Etc. • Arrays • Slices, splices • Contexts: list, scalar • Functions • Calling forms • Argument fetching • Packages and Modules • References: “intelligent pointers” • Anonymous Variables • (e.g., lists as values). CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

  2. Arrays # Scalar assignment: $pi = 3.141; # Array assignment: @pies = ($pi, "22/7", "apple pi"); # Uses of an array: $r = 2; $area = $pies[0] * $r * $r; print 'The @pies array ', "is @pies.\n"; The @pies array is 3.141 22/7 apple pi. CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

  3. Array Slices A slice of an array is a subsequence of its elements. # Array assignment: @primes = (2,3,5,7,11,13,17,19); # Assignment with slices: @low_primes = @primes(0..3); @misc_primes = @primes(1,3,5,7); # Interchanging to array elements: @low_primes(0,2) = @low_primes(2,0); # Assignment to a list of scalars: ($one, $two) = (1, 2, 3, 4); CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

  4. Array Splices Splicing an array involves changing, inserting, or deleting elements of the array. @lucky = (1,2,3,4,5,6,7,8); $offset = 2; $length = 3; splice(@lucky, $offset, $length, (9,16,25)); print "Lucky numbers are @lucky.\n"; Lucky numbers are 1 2 9 16 25 6 7 8. CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

  5. Splicing to Remove Elements To remove elements when splicing, specify fewer (or no replacement elements) than elements to be replaced. @lucky = (1,2,3,4,5,6,7,8); splice(@lucky, 2, 3); print "Lucky numbers are @lucky.\n"; Lucky numbers are 1 2 6 7 8. CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

  6. Splicing to Insert Elements To insert elements, use a length of 0 for the slice (or use a length less than the number of new elements). @lucky = (1,2,3,4,5,6,7,8); splice(@lucky, 2, 0, (9, 16, 25)); print "Lucky numbers are @lucky.\n"; Lucky numbers are 1 2 9 16 25 3 4 5 6 7 8. CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

  7. Splicing Near the End of an Array For an array @a we can use $#a for the index of the last element. We can also use -1. Also, -2 can be used to reference the second to last element, etc. @lucky = (1,2,3,4,5,6,7,8); print "The last 2 numbers ", "are $lucky[-2] and #lucky[$#a].\n"; The last 2 numbers are 7 and 8. CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

  8. List Context vs Scalar Context Any Perl function or operator can have behavior that depends on the "context" in which it is called. # Scalar context: $line = <INFILE>; # reads one line. # List context: @lines = <INFILE>; # reads all lines. CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

  9. List Context List context is a calling situation in which a list is needed, as in an assignment to an array. If the function being called normally returns a scalar, the scalar will be converted to a list of one element. # List context: @list = 5; # Equivalent to $list[0] = 5; CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

  10. Scalar Context Scalar context is a calling situation in which a scalar is needed, as in an assignment to a scalar variable. If the function being called normally returns a list, the last element of the list will be used as the scalar. $scalar = (1, 2, 3); # Equivalent to $scalar = 3; CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

  11. List and Scalar Contexts with print @primes = (2,3,5,7,11); print @primes; # List context 235711 print "@primes"; # Scalar context 2 3 5 7 11 print "Date: ", localtime(), "\n"; Date: 4737111689612591 print "Date: ", scalar localtime(), "\n"; Date: Wed Nov 28 13:55:01 2001 CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

  12. 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 Arrays, Functions, Lists, Refs, Etc

  13. 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 Arrays, Functions, Lists, Refs, Etc

  14. 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 Arrays, Functions, Lists, Refs, Etc

  15. 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 Arrays, Functions, Lists, Refs, Etc

  16. 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 Arrays, Functions, Lists, Refs, Etc

  17. 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 Arrays, Functions, Lists, Refs, Etc

  18. 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 Arrays, Functions, Lists, Refs, Etc

  19. 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 Arrays, Functions, Lists, Refs, Etc

  20. 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 Arrays, Functions, Lists, Refs, Etc

More Related