Top Banner
Programming Perls* • Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard Things Possible Perl is a language for easily manipulating text, files, and processes Combines concepts from unix, sed, awk, shell scripts Language of system administrators, web developers and more… Practical Extraction and Report Language Pathologically Eclectic Rubbish Lister y of the examples in this lecture e from “Learning Perl”, 3 rd Ed, Schwartz & T. Phoenix, O’Reilly, 2001
34

Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Dec 17, 2015

Download

Documents

Amos Brown
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Programming Perls*• Objective: To introduce students to the perl

language.– Perl is a language for getting your job done.– Making Easy Things Easy & Hard Things Possible– Perl is a language for easily manipulating text, files, and

processes– Combines concepts from unix, sed, awk, shell scripts– Language of system administrators, web developers and

more…– Practical Extraction and Report Language– Pathologically Eclectic Rubbish Lister

*Many of the examples in this lecture come from “Learning Perl”, 3rd Ed, R. Schwartz & T. Phoenix, O’Reilly, 2001

Page 2: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Topics

• Getting Started

• scalars

• lists and arrays

• hashes

• I/O

• File handles

• regular expressions

Page 3: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Hello, World!

#!/usr/bin/perlprint "Hello, World!\n";

#!/usr/bin/perl@lines = `perldoc -u -f atan2`;foreach (@lines) { s/(\w)<([^>]+)>/$1<\U$2>/g; print;}

Page 4: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

A More Complicated Example#!/usr/bin/perl -wopen(FIND,"find . -print |") || die "Couldn't run find: $!\n";

FILE:while ($filename = <FIND>) { chomp($filename); next FILE unless -T $filename; if (!open(TEXTFILE, $filename)) { print STDERR "Can't open $filename--continuing...\n"; next FILE; } while (<TEXTFILE>) { foreach $word (@ARGV) { if (index($_,$word) >= 0) { print "$filename: $word\n"; next; } } }}

Page 5: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Getting Help

• man perl

• perldoc

• Learning Perl

• Programming Perl

• www.cpan.org, www.pm.org, www.perlmonks.org

Page 6: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

scalars

• numbers: 3, 3.14159, 7.24e15

• strings: ‘fred’, “barney”, “hello\n”

• variables: $name, $count

• assignment: $name = ‘fred’; $count = 1; $count += 1; $name = $fred . ‘flinstone’;

• special variables: $_

Page 7: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

operators

• numbers– 2+3, 5.1-2.4, 3 * 12, 14/2, 10/3– ==, !=, <, >, <=, >=

• strings– concatenation: str1 . str2– replication: str x num– eq, ne, lt, gt, le, ge

Page 8: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

print

• single vs. double quotes– ‘$firstname flinstone’– “$firstname flinstone”

• print “My name is $name\n”

• print $firstname, $lastname, “\n”

• STDOUT, STDERR

Page 9: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Conditionals

• Boolean value (any scalar value)– false: undef, 0, ‘’, ‘0’– true: everything else

$count = 10;if ($count > 0) { print $count, “\n”; $count -= 1;} else { print “blast off\n”;}

Page 10: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Loops

$count =10;

while ($count > 0) {

print $count, “\n”;

$count -= 1;

}

print “blast off\n”;

Page 11: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Getting User Input

• line input operator: <STDIN>

• $line = <STDIN> # includes \n

• chomp removes \n

• chomp($line)

Page 12: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Sum Odd Numbers

#!/usr/bin/perl

#Add up some odd numbers

$n = 1;

print "How many odd numbers do you want to add? ";

$howmany = <STDIN>;

chomp($howmany);

while ($n <= $howmany) {

$sum += 2*$n - 1;

$n += 1;

}

print "The sum of the first $howmany odd numbers = $sum\n";

Page 13: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Exercise 2.1

• Write a program that computes the circumference of a circle with radius 12.5. Use $pi = 3.141592654

Page 14: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Exercise 2.2

• Modify the previous program to prompt and read the radius

Page 15: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Exercise 2.3

• Modify the previous program so that if the radius is less than zero, the circumference is set to zero.

Page 16: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Exercise 2.4

• Write a program that prompts for and reads two numbers, on separate lines, and prints their product.

Page 17: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Exercise 2.5

• Write a program that prompts for and reads a string and a number (on separate lines) and prints the string the number of times indicated by the number (on separate lines).

Page 18: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Arrays and Lists

• Used interchangeably• List variables @name• List literals (“fred”,2,3)• @primes = (2,3,5,7,11,13,17)• @range = 1..10• Accessing elements: $primes[3]• Length of a list: $#primes• List assignment: ($p1, $p2, $p3) = (2,3,5)

Page 19: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

List Operators

• @array = 1..5;• The pop operator removes the last element of a list• $last = pop(@array);

– @array = (1,2,3,4); $last=5

• The push operator appends an element to the end of a list

• push(@array,5);– @array = (1,2,3,4,5)

Page 20: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

List Operators

• @array = 1..5;• The shift operator removes the first element of a

list– $first = shift(@array);– $first = 1; @array = (2,3,4,5)

• The unshift operator prepends an element to the beginning of a list– unshift(@array,1);– @array = (1,2,3,4,5)

Page 21: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

List Operators

• @array = 1..5;• The reverse operator reverses the elements

of a list– @rarray = reverse(@array);

• The sort operator sorts the elements of a list– @sarray = sort(@rarray);– @students = (“Sam”, “Fred”, “Anna”, “Sue”);– print sort(@students);

Page 22: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

foreach Control Structure

foreach $i (1..10) {

print “$i\n”;

}

foreach (1..10) {

print “$_\n”;

}

Page 23: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Reading Lines

#!/usr/bin/perl

chomp(@lines = <STDIN>); # read lines, not newlines

foreach $line (@lines) {

print "$line\n";

}

Page 24: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Exercise 3.1

• Write a program that reads a list of strings on separate lines until end-of-input and prints the list in reverse order.

Page 25: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Exercise 3.2

• Write a program that reads a list of numbers on separate lines until end-of-input and then prints for each number the corresponding person’s name from the list – fred betty barney dino wilma pebbles bamm-

bamm

Page 26: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Exercise 3.3

• Write a program that reads a list of strings on separate lines until the end-of-input. Then it should print the strings in alphabetical order.

Page 27: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Hashes

• An array that can be indexed by arbitrary strings

• $family_name{“fred”} = “flintstone;

• $family_name{“barney”} = “rubble”;

foreach $person in keys( %family_name ) {

print “Full name = $family_name{$person}\n”;

}

Page 28: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Hashes

• The hash as a whole is referred to by a variable whose name starts with %

– %hash = (“barney”, “rubble”, “fred”, “flinstone”);

– %hash =(“barney” => “rubble”,– “fred” => “flinstone”);– @key-value-list = %hash

Page 29: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Hashes

• To obtain the keys in a hash– @first_names = keys(%hash);

• To obtain the values in a hash– @last_names = values(%hash);

Page 30: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

The each Function• You can loop over the key-value pairs in a hash

while ( ($key, $value) = each %hash ) { print “$key => $value\n”;}

• The order is not specified – use sort if you care.

foreach $key (sort keys %hash) { $value = $hash{$key}; print “$key => $value\n”;}

Page 31: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

The exists Function

• You can query to see if an entry with a given key has been inserted into a hash

if (exists $last_name{$person}) {

print “$person has a last name\n”;

}

Page 32: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Deleting Entries from a Hash

• delete($family_name{fred});

Page 33: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Exercise 5.1

• Write a program that will ask the user for a given name and report the corresponding family name.

Page 34: Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.

Exercise 5.2

• Write a program that reads a series of words (with one word per line) until end-of-input, then prints a summary of how many times each word was seen.