Perl Tutorial-Part II
CSCI 3136 Principles of Programming and Languages
1 Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/
Perl Tutorial-Part II CSCI 3136 Principles of Programming and - - PowerPoint PPT Presentation
Perl Tutorial-Part II CSCI 3136 Principles of Programming and Languages Slides mainly taken from Perl Programming, Quentin Smith 1 http://stuff.mit.edu/iap/perl/ Outline Run a Perl Program Data Types Context Operators Flow
1 Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 2
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 3
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 4
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 5
– ++, --, +, -, *, /, +=, *=, >>, <<
– Concatenation (.), repetition (x), assignment (.=, x=)
– qq (“), q (‘), qx(`), qw, pattern matching
– <, >, ==, !=, =>, <= – lt, gt, eq, ne, le, ge – &&, ||, !, and, or, not – ? :
– sort, reverse, push/pop, unshift/shift, split/join, grep, map
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 6
– if – unless
– while – until – for – foreach
– Simple statements may be modified
7 Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 8
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 9
my ($a, $b) = (0, 1); if (!$a && !$b) {print "Neither\n";} # Conventional if (not $a and not $b) {print "Neither\n";} # Same, but in English if (not ($a or $b)) {print "Neither\n";} # Same, but parentheses unless ($a or $b) {print "Neither\n";} # Same, but clearest ($a,$b) = (1,0); unless($a) { print “a is not true”; }elsif($b) { print “b is true”; }else{ print “a is true but b is not true”; }
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 10
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 11
my $counter = 10; while ($counter >= 0) { print $counter.”,”; $counter--; } Output: 10,9,8,7,6,5,4,3,2,1,0, $counter = 5; until ($counter < 0) { print $counter.”,” $counter--; } Output: 5,4,3,2,1,0, $counter = 0; do { print $counter.”,” $counter--; } while ($counter >= 0); Output: 0,
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 12
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 13
for (my $i = 10; $i >= 0; $i--) { print "$i,"; # Countdown } Output: 10,9,8,7,6,5,4,3,2,1,0, foreach my $i (reverse 0..10) { print "$i,\n"; # Same } Output: 10,9,8,7,6,5,4,3,2,1,0, %hash = (dog => "lazy", fox => "quick"); foreach my $key (keys %hash) { print "The $key is $hash{$key}.\n"; # Print out hash pairs } Output: The fox is quick. The dog is lazy.
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 14
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 15
my $default = 0; my $a = $default unless defined $a; my $b = 3; $b = $default unless defined $b; print “$a,$b\n”; Output: 0,3 my $balance = 5000; $balance += $deposit if $deposit; print “$balance\n”; Output: 5000 my withdrawal = 300; $balance -= $withdrawal if $withdrawal and $withdrawal <= $balance; Print “$balance\n”; Output: 4700
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 16
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 17
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 18
sub ten { @array = (1..10); return wantarray() ? @array : 100; } @ten = ten(); # (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) $ten = ten(); # 100 ($ten) = ten(); # (1) ($one, $two) = ten(); # (1, 2)
@array = (1..10); return @array; } @ten = ten(); # (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) $ten = ten(); # 10
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 19
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 20
sub add_one { # Like pass by value my ($n) = @_; # Copy first argument return ($n + 1); # Return 1 more than argument } sub plus_plus { # Like pass by reference $_[0] = $_[0] + 1; # Modify first argument } # no return statement my ($a, $b) = (10, 0); add_one($a); # Return value is lost, nothing changes $b = add_one($a); # $a is 10, $b is 11 plus_plus($a); # Return value lost, but a now is 11 $b = plus_plus($a); # $a and $b are 12
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 21
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 22
print factorial(5) . "\n"; # Parentheses required sub factorial { my ($n) = @_; return $n if $n <= 2; $n * factorial($n - 1); } print ((factorial 5) . "\n"); # Parentheses around argument not required, # but need to ensure there are no extra arguments print &factorial(5) . "\n"; # Neither () nor & required
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 23
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 24
sub fibonacci { my ($n) = @_; die "Number must be positive" if $n <= 0; return 1 if $n <= 2; return (fibonacci($n-1) + fibonacci($n-2)); } foreach my $i (1..5) { my $fib = fibonacci($i); print "fibonacci($i) is $fib\n"; } fibonacci(1) is 1 fibonacci(2) is 1 fibonacci(3) is 2 fibonacci(4) is 3 fibonacci(5) is 5
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 25
my @fruit = qw(apple banana cherry); my $fruitref = \@fruit;
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 26
my @fruit = qw(apple banana cherry); my $fruitref = \@fruit; print "I have these fruits: @$fruitref.\n"; print "I want a $fruitref->[1].\n"; Output: I have these fruits: apple banana cherry. I want a banana.
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 27
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 28
my $fruits = ["apple", "bananas", "cherries"]; #my @fruitarr = ("apple", "bananas", "cherries"); #my $fruits = \@fruits my $wheels = {unicycle => 1, bike => 2, tricycle => 3, car => 4, semi => 18}; #my %wheelhash = (unicycle => 1, bike => 2, tricycle => 3, car => 4, semi => 18); #my $wheels = \$wheelhash; print "A car has $wheels->{car} wheels.\n"; Output: A car has 4 wheels.
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 29
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 30
my $fruits = ["apple", "bananas", "cherries"]; my $veggies = ["spinach", "turnips"]; my $grains = ["rice", "corn"]; my @shopping_list = ($fruits, $veggies, $grains); print "I should remember to get $shopping_list[2]->[1].\n"; print "I should remember to get $shopping_list[0][2].\n"; Output: I should remember to get corn. I should remember to get cherries.
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 31
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 32
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 33
close INPUT;
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 34
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 35
print STDOUT "Hello, world.\n"; # STDOUT not needed
print STDERR "Oh no, here's an error message.\n"; warn "Oh no, here's another error message.\n"; close STDERR;
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 36
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 37
print "What type of pet do you have? "; my $pet = <STDIN>; # Read a line from STDIN, say parrot #$pet is “parrot\n” chomp $pet; # Remove newline, $pet contains “parrot” print "Enter your pet's name: "; my $name = <>; # STDIN is optional chomp $name; print "Your pet $pet is named $name.\n"; Output: What type of pet do you have? parrot Enter your pet's name: Polly Your pet parrot is named Polly.
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 38
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 39
while (my $line = <CUSTOMERS>) { my @fields = split(":", $line); # Fields separated by colons print "$fields[1] $fields[0]\n"; # Display selected fields print "$fields[3], $fields[4]\n"; print "$fields[5], $fields[6] $fields[7]\n"; } print while <>; # cat utility in Unix/Linux print STDOUT $_ while ($_ = <STDIN>); # same, but more verbose Output: Last name:First name:Age:Address:Apartment:City:State:ZIP Smith:Al:18:123 Apple St.:Apt. #1:Cambridge:MA:02139 Al Smith 123 Apple St., Apt. #1 Cambridge, MA 02139
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 40
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 41
while (my $line = <CUSTOMERS>) { my @fields = split(":", $line); print LABELS "$fields[1] $fields[0]\n"; print LABELS "$fields[3], $fields[4]\n"; print LABELS "$fields[5], $fields[6] $fields[7]\n"; }
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 42
# Use another process as input
# Print labels to printer instead of to a file
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 43
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 44
my $filename = "pie_recipe"; if (-r $filename) {
}else { print "The file $filename is not readable.\n"; }
if (-w $filename) {
}else { print "The file $filename is not writable.\n"; }
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 45
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 46
my $string = "Did the fox jump over the dogs?"; #contain the letters "dog" in order? print "1: $string\n" if $string =~ /dog/; # matches #not contain the letter "z"? print "2: $string\n" if $string !~ /z/; # matches #begin with the letters "Y" or "y"? print "3: $string\n" if $string =~ /^[Yy]/; #end with a question mark? print "4: $string\n" if $string =~ /\?$/; # matches #contain only letters? print "5: $string\n" if $string =~ /^[a-zA-Z]*$/; #contain only digits? print "6: $string\n" if $string =~ /^\d*$/;
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 47
my $string = "Did the fox jump over the dogs?"; print "1: $string\n" if $string =~ /dog/; # matches print "2: $string\n" if $string =~ m/dog/; # matches print "3: $string\n" if $string =~ m(dog); # matches print "4: $string\n" if $string =~ m|dog|; # matches
underscore), \w is the same as [a-zA-Z1-9_]
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 48
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 49
my $string = "Did the fox jump over the dogs?"; print "1: $string\n" if $string =~ m/[bdl]og/; # bog, dog, log print "2: $string\n" if $string =~ m/dog[^s]/; # no match print "3: $string\n" if $string =~ m/\s\w\w\wp\s/; # matches
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 50
my $string = "Did the fox jump over the dogs?"; print "1: $string\n" if $string =~ m/^[Yy]/; # no match print "2: $string\n" if $string =~ m/\?$/; # match print "3: $string\n" if $string =~ m/the\b/; # match
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 51
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 52
my $string = "Did the fox jump over the dogs?"; print "1: $string\n" if $string =~ m/z*/; # matches print "2: $string\n" if $string =~ m/z+/; # no match print "3: $string\n" if $string =~ m/\b\w{4}\b/; # matches "jump“ print “4: $string\n” if $string =~ m/H?/; # matches print “5: $string\n” if $string =~ m/\b\w{2,6}\b/; # matches, # any word whose length is between 2 and 6
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 53
my $string = "Did the fox jump over the dogs?"; print "1: $string\n" if $string =~ m/(fox){2}/; # "foxfox" print "2: $string\n" if $string =~ m/(the\s).*\1/; # matches
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 54
my $string = "Did the fox jump over the dogs?"; $string =~ s/dog/cat/; # substitute "cat" for "dog" $string =~ s(fox)(kangaroo); # substitute "kangaroo" for "fox" print "$string\n"; $string =~ tr/a-z/n-za-m/; # Rot13 print "$string\n"; Output: Did the kangaroo jump over the cats? Dvq gur xnatnebb whzc bire gur pngf?
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 55
Slides mainly taken from Perl Programming, Quentin Smith http://stuff.mit.edu/iap/perl/ 56
my $breakfast = 'Lox Lox Lox lox and bagels'; $breakfast =~ s/Lox //g; print "$breakfast\n"; my $paragraph = "First Line\nSecond Line\nThird Line\n"; my ($first, $second) = ($paragraph =~ /(^.*$)\n(^.*$)/m); print "$first, $second\n"; Output: lox and bagels First Line, Second Line