Presentation is loading. Please wait.

Presentation is loading. Please wait.

Perl: Arrays, Functions, References, Etc.

Similar presentations


Presentation on theme: "Perl: Arrays, Functions, References, Etc."— Presentation transcript:

1 Perl: Arrays, Functions, References, Etc.
Slices, splices Contexts: list, scalar Functions Calling forms Argument fetching Packages and Modules References: “intelligent pointers” Anonymous Variables (e.g., lists as values). CSE S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

2 CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc
# 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 array ', array is /7 apple pi. CSE S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

3 CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc
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 @misc_primes # Interchanging two array elements: @low_primes(0,2) # Assignment to a list of scalars: ($one, $two) = (1, 2, 3, 4); CSE S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

4 CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc
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; $offset, $length, (9,16,25)); print "Lucky numbers Lucky numbers are CSE S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

5 Splicing to Remove Elements
To remove elements when splicing, specify fewer new elements (or no replacement elements) than elements to be replaced. @lucky = (1,2,3,4,5,6,7,8); 2, 3); print "Lucky numbers Lucky numbers are CSE 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); 2, 0, (9, 16, 25)); print "Lucky numbers Lucky numbers are CSE S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

7 Splicing Near the End of an Array
For an 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 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 S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

9 CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc
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 S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

10 CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc
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 S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

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

13 CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc
Perl: Pronouns $_ # “It” (scalar default variable) @people = ("John", "Tran", "Pam"); foreach $person { print $person, " "; } foreach { print $_, " "; } print; print " "; } John Tran Pam John Tran Pam John Tran Pam CSE S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

14 CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, 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 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 CSE S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

16 CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, 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 S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

17 CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc
Modules #!/usr/bin/perl -w use MapleTreesModule; # The compiler searches for MapleTreesModule.pm # following all the paths in an array # 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 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 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 S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

20 CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc
Anonymous Variables sub someTrees { = qw(oak pine elm); return } $treesRef = someTrees(); sub moreTrees { ["banyan", "beech"]; # anonymous # both functions return references. CSE S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc


Download ppt "Perl: Arrays, Functions, References, Etc."

Similar presentations


Ads by Google