1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl...

16
1 More Perl • Strings • References Complex data structures Multidimensional arrays • Subprograms Perl OOP – Methods Constructors and Instances – Inheritance

Transcript of 1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl...

Page 1: 1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.

1More Perl

• Strings• References• Complex data structures

– Multidimensional arrays

• Subprograms• Perl OOP

– Methods– Constructors and Instances– Inheritance

Page 2: 1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.

2Strings

• Array of strings@guys = qw(Al Bob Cy); # instead of

@guys = ("Al", "Bob", "Cy");

• Concatenation – join(separator, list)

join(":", "313", "is", "ok"); # 313:is:ok

• Splitting– split(/pattern/, string)

split(/:/, "313:is:ok"); # ("313","is","ok")

split(/[:;]/, "313:is;ok"); # ("313","is","ok")

• Replacement$text =~ s/foo/bar/; # replaces 1st foo by bar

$text =~ s/\"//g; # deletes all quotes

• rich pattern matching regular expressions

Page 3: 1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.

3Boolean

• Values equivalent to "false"• undef• "" # since it can represent undef• 0 # since it can represent undef• 0.0 # since it can represent 0• "0" # since it can represent 0

• Everything else is equivalent to "true"

Page 4: 1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.

4References

• References are pointers to data structures• References are needed

– Complex data structures• Build as arrays and hashes whose elements are references

– Parameter passing

• Denoted by \my $scalar = 313;

my @array = qw(ics 313);

my %hash = (dept => "ics", course => 313);

# references

my $ref_scalar = \$scalar;

my $ref_array = \@array;

my $ref_hash = \%hash;

Page 5: 1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.

5References to Arrays and Hashes

• References to arrays and hashes can be initialized directly– Arrays

• my $ref_courses = [313, 415];

– Hashes• my $ref_grades = {313 => "A-", 415 => "A+"};

• Note the different bracketing

• These are also called anonymous references

Page 6: 1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.

6Dereferencing

• Dereferenced with ${}, @{} or %{}print ${$ref_scalar}; # prints $scalar

$len = @{$ref_array}; # gets length of @array

keys(%{$ref_myHash}); # keys of %hash

• Another way to dereference is with ->$ref_array->[0]

$ref_hash->{"key"}

Page 7: 1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.

7Multidimensional Arrays

• Multidimensional array is an– Array of references to subarrays

my $matrix = [[1,0], [0,1], [0,0]];

• Elements can be accessesed directlyprint "m[1,0]: $matrix->[1][0]";

– Another way is$matrix->[1]->[0];

• Also– An array can hold references to hashes– A hash can hold references to arrays

Page 8: 1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.

8Subprograms

• Syntaxsub name {…}

• Parameters are accessible via array @_my ($p1, $p2, $p3) = @_; # same as $p1 = @_[0] etc.

– shift returns next parametermy $p1 = shift ;

my $p2 = shift ;

• Subprograms return a value– it not explicit, last expression is returned

• Examplesub square {

my $num = shift;

return $num * $num;

}

Page 9: 1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.

9Pass-by-Reference

• All parameters are passed as one flattened array• Elements of scalars are concatenated!• Solution

– Pass references to arrays or hashesmy @v1 = (1,2,3);

my @v2 = (4,5,6);

my $product = product(\@v1,\@v2);

– Dereference elements of parameter array within the subprogramsub product {

my ($vector1, $vector2) = @_;

my $product = 0;

foreach my $i (0..$#{$vector1}) {

$product += $vector1->[$i] * $vector2->[$i];

}

return $product;

}

Page 10: 1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.

10eval function

• eval evaluates an arbitrary string– It accepts a string parameter,– Evaluates it, and– Returns the result

• Exampleprint "Enter expression: ";

my $expression = <STDIN>;

chop($expression);

my $result = eval($expression);

print "$expression = $result";

– Produces this sample outputEnter expression: 254+12*25

254+12*25 = 554

Page 11: 1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.

11References to Functions

• References to functions can be– Created using \&name– Assigned to variables– Passed as parameters– Called using $ref_sub->()

• Examplesub do_it { # executes any subprogram!

my $ref_sub = shift; # function reference to call

return $ref_sub->(@_); # call it with remaining params

}

– Can be used assub square {…}

my $ref_square = \&square; # reference to square sub

my $result = do_it ($ref_square, 22);

print "result = $result";

Page 12: 1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.

12OOP in Perl

• A Perl class is defined as a package/module – Package files have same name as the class and extension .pm

• Methods are normal subprograms • Fields are defined and initialized in a constructor• A module must return "true" at the end

1; # this is the simplest

• Examplepackage Person;

sub new {…} # constructor

sub set_name {…}

sub set_age {…}

sub say_hi {…}

1;

Page 13: 1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.

13Constructors

• Constructor is subprogram called newmy $guy = Person->new("Joe", 29);

• It creates a reference to a hash that contains the fields– The fields' names are keys of the hash– The fields' values are values in the hash– Typically, the parameters initialize the fields

• 1st parameter is the class name

– It blesses the hash and returns reference to the hash

• Examplesub new {

my $self = {}; # reference to hash

($self->{class},$self->{name},$self->{age}) = @_;

# turn this into an instance; return it

bless($self, $self->{class});

}

Page 14: 1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.

14Methods

• Declare the method as subprogram

• Reference to the called object is the 1st parameter sub say_hi {

my $self = shift;

print "Hi, I'm $self->{name}.";

print " I'm $self->{age} years old.\n";}

• Perl adds automatically the 1st parameter to the call$guy->say_hi();

Page 15: 1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.

15Using a Class

#!/usr/bin/perl

use strict;

use warnings;

use Person;

my $guy = Person->new("Joe", 29);

$guy->say_hi;

$guy->set_name("John");

$guy->set_age(31);

$guy->say_hi;

Page 16: 1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.

16Inheritance

• Define superclass with use base• Overwrite methods• Use to access overwritten method in superclass

my $self = shift;

$self->SUPER::say_hi;

• Examplepackage Brudda;

use base "Person";

sub say_hi {

my $self = shift;

print "Howzit, bra? ";

$self->SUPER::say_hi;

}