cse 341 -- s. tanimoto perl arrays, functions, lists, refs, etc 1 perl: arrays, functions,...

20
CSE 341 -- S. Tanimoto P erl Arrays, Functions, Li 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).

Upload: cody-powell

Post on 24-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar

CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

1

Perl: Arrays, Functions, References, Etc.

Arrays Slices, splices Contexts: list, scalarFunctions

Calling formsArgument fetching

Packages and ModulesReferences: “intelligent pointers”Anonymous Variables (e.g., lists as values).

Page 2: CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar

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.

Page 3: CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar

CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

3

Array SlicesA 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);

Page 4: CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar

CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

4

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

Page 5: CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar

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.

Page 6: CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar

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.

Page 7: CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar

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.

Page 8: CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar

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.

Page 9: CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar

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;

Page 10: CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar

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;

Page 11: CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar

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 context235711print "@primes"; # Scalar context2 3 5 7 11

print "Date: ", localtime(), "\n";Date: 4737111689612591

print "Date: ", scalar localtime(), "\n";Date: Wed Nov 28 13:55:01 2001

Page 12: CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar

CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

12

Perl: User Defined Functionssub 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); # orfunny_print "Abe", 50; # This last form is OK AFTER the defn.

Page 13: CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar

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

Page 14: CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar

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]; ...}

Page 15: CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar

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 @_

Page 16: CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar

CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

16

Packages#!/usr/bin/perl -wPackage OakTrees;sub getseeds { print "acorns"; }

Package PalmTrees;sub getseeds { print "coconuts" ; }

getseeds() ; # prints coconuts;OakTrees::getseeds(); # prints acornsPalmTrees::getseeds(): # prints coconuts

# A package is a separate namespace for # functions and variables.# To cross packages use fully-qualified name

Page 17: CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar

CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

17

Modules#!/usr/bin/perl -wuse 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.

Page 18: CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar

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.

Page 19: CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar

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;

Page 20: CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar

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.