Perl (pdf)

whooploafΛογισμικό & κατασκευή λογ/κού

13 Δεκ 2013 (πριν από 5 χρόνια και 2 μήνες)

125 εμφανίσεις

Systems Development
(original lecture content by Julia Dawson)
PLEASE turn off mobile
phones, pagers etc.
UFCE47-20-1 Lecture 8 2011-12 3
UFCE47-20-1 Lecture 8 2011-12 4
This lecture

• Introducing Perl
• Why & What is Perl?
• Using Perl
• References
UFCE47-20-1 Lecture 8 2011-12 5
Practical Extraction and Report Language
Pathologically Eclectic Rubbish Lister(?)
• This is the second of three programming languages
you will meet in this module
• This lecture is based on the course book (Bates)
• …so you should consult it for a fuller picture
• Practice makes perfect!!
UFCE47-20-1 Lecture 8 2011-12 6
Other Perl books

• Learning Perl; Schwartz & Phoenix;
• (... known as the Llama Book)
In general, if you are anything other than a (very)
good programmer, you will find this the best book to buy.
• N.B. unfortunately, the 3rd edition of this book no longer has the very
useful chapter on cgi programming in Perl - you may still be able to find
copies of the 2nd edition, though.
• Programming Perl; Wall, Christiansen, Schwartz;
• (if you're a really good programmer, you could buy this in place of
Learning Perl)
• (...Known as the Camel Book - this is a good buy for an experienced
programmer – it very quickly covers the basics and then
the rest of the book is a reference)
UFCE47-20-1 Lecture 8 2011-12 7
What is Perl?
• Originally Perl was developed as a ‘glue
language’ to pull separate applications
• It replaced or combined other languages
(awk, sed, C) which were of smaller scope
• It is particularly suited for text manipulation –
this makes it incredibly useful for web-based
applications which are themselves largely
UFCE47-20-1 Lecture 8 2011-12 8
Perl is

• …Free!!! (
• …a “glue” language
• …a scripting language (as is javaScript)
• …available for most operating systems (some features OS-specific)
• …popular for systems development
• …very powerful text-handling facilities
• …valuable for web development (server-side)
• …easy to learn (compared to other programming languages)
• …the parent of PHP
• …object oriented but only when you want it to be
• …a language for getting the job
• …spelt Perl
not Pearl
There is much help freely available on the web, including vast amounts
of source code – use with care!
UFCE47-20-1 Lecture 8 2011-12 9
Perl can get you a job

• Some info from Lyle Hopkins (Bristol and Bath Perl Mongers -
• Perl job salaries start at £20k for a beginner, rapidly rising to £30 - £35k,
then beyond £60k for those with lots of experience
• E.g. Local jobs with salaries between £35 - £50,000
• Many Perl jobs are telecommuting
• Stewart Smith (a local recruiter) said
•"In my experience (as a recruitment consultant) proper Perl roles seem
to attract salaries at least £5K higher than a .Net or Java role requiring a
similar amount of commercial experience.“
• A number of students have got Perl summer jobs after their first
• And some students have done entire Perl-programming
placement years
UFCE47-20-1 Lecture 8 2011-12 10
Using Perl
• The following examples show some of the ways
to use Perl
• We move very quickly thro' some fundamental
programming ideas
• The Perl motto is TIMTOWTDI (Tim Toady)
• There Is More Than One Way To Do It
• You
read Chapter 9 of the course book to
"flesh out" these ideas
• You
practice writing simple Perl programs of
your own
UFCE47-20-1 Lecture 8 2011-12 11
First Perl program (script)
• This really is it!
• Just save this to file e.g.
• Run it using: perl
• Later you will need to run Perl scripts
• To do that it is best to start with this good habit :
• Always make the first line of your script point to where ever Perl
is on you system (note - this only works with Unix – on Windows
systems the .pl extension identifies Perl source code files)
• On a Unix system set the file permissions - you can use 755 to
start with (e.g. chmod 755
print “Hello World”;
#! /usr/local/bin/perl –w
print “Hello World”;
NB CEMS uses a non-
standard location! (Most
places it would be
UFCE47-20-1 Lecture 8 2011-12 12
Storing information
• Scalars are what Perl calls variables – any “single”
thing, a number or a line of text (‘string’), for
• The name of a scalar starts with a “$” e.g.
• $toy, $sweet or $game
• No need to pre-declare scalars (or any other
variables in Perl)
• Assigning a value:
• $toy = “lego”;
• $game = 5;
#! /usr/local/bin/perl –w
$sweet = “chocolate”;
$presents = 12;
print “I like $sweet\n”;
print “How many? $presents\n”;
I like chocolate
How many? 12
UFCE47-20-1 Lecture 8 2011-12 13
Getting information
• Programs can interact with the user
• Ask for a value and then read it into a variable
• Take the “newline” off the end (using the ‘chomp’
function) & output the value
#! /usr/local/bin/perl –w
print "What do you want for Xmas?: ";
$want = <STDIN>;
chomp ($want);
print "You want $want\n";
What do you want for Xmas?: peace
You want peace
is a
contents of
$want are
printed out
UFCE47-20-1 Lecture 8 2011-12 14

making decisions
• I have one present to give -
• …it’s a book. If you ask for it you can have it –
otherwise go without!
#! /usr/local/bin/perl –w
print "What do you want for Xmas: ";
chomp($want = <STDIN>);
if ($want =~ /book/)
print "The book is yours\n";
print "I don't have $want\n";
Note the strange =~
the ‘if’ condition is
true when $want
contains the string
Only ‘book’ returns
true, ‘Book’ doesn’t.
To ignore case use
=~ /book/i
UFCE47-20-1 Lecture 8 2011-12 15
Playing with Strings
• A string is a sequence of characters
• a word, a line, a name etc…
#! /usr/local/bin/perl –w
print "Enter some text: ";
chomp($line = <STDIN>);
print "You gave " . length($line) .
" characters\n";
$line = uc($line); # to uppercase
print "In uppercase: $line\n";
if (length($line) > 5)
print “More than 5 characters\n";
Comparing numbers is
different to strings e.g. >5
not =~ 5
Comments start with a# -
the rest of the line is
ignored by the program
"Some words" is a
string, $line a string
variable (or scalar)
Enter some text: squander
You gave 8 characters
In uppercase: SQUANDER
More than 5 characters
UFCE47-20-1 Lecture 8 2011-12 16
• Programs often need to do things many times -
usually in a "loop". Two basic types of loop
#! /usr/local/bin/perl –w
for ($i=0; $i<=3; $i++)
print "$i Hello\n";
$total = 100
while ($total > 0) {
# get number from user
print " Enter number($total left)";
$in = <STDIN>;
chomp $in;
# subtract number from total
$total = $total - $in;
1. Fixed number of times
(print Hello 4 times) ($i++
means ‘increment $i by
2. While something is true
(subtract number until
total becomes less than 0)
Can you spot the
deliberate mistake
in this bit of code?
(something is
UFCE47-20-1 Lecture 8 2011-12 17
0 Hello
1 Hello
2 Hello
3 Hello
Enter number(100 left) 45
Enter number(55 left) 7
Enter number(48 left) 13
Enter number(35 left) 37
0 Hello
1 Hello
2 Hello
3 Hello
Enter number(100 left) 6
Enter number(94 left) 200
Run 1
Run 2
UFCE47-20-1 Lecture 8 2011-12 18
Reading from a File
• Files are referred to by a filehandle
• Normally filehandle variable names are in all-
uppercase for ease of reading
• The program can
(finish) if the file does not
• An error message can be shown using ‘die’
• Example - reading a file called shop.txt
open (IN,’shop.txt’) or die "Can't find file:
UFCE47-20-1 Lecture 8 2011-12 19
Program to read a file
Input File (shop.txt)
ice cream
#! /usr/local/bin/perl –w
# Must be an existing input file
$inFile = "shop.txt";
open (IN, $inFile) or
die "Can't find file: $inFile";
# loop over input file
foreach $line (<IN>)
print "** $line";
** eggs
** potatoes
** ice cream
** avocados
** yogurt
UFCE47-20-1 Lecture 8 2011-12 20
Pattern Match
• What about checking to see if something
is on the shopping list?
• This uses as simple pattern match
• See the previous slide "Selection"
• Get input from user, chomp it and check list
• $item = <STDIN>
• chomp $item
• =~ /$item/
• (see next slide for the actual code)
UFCE47-20-1 Lecture 8 2011-12 21
Check Shopping
#! /usr/local/bin/perl –w
$inFile = "shop.txt";
open (IN, $inFile) or
die "Can't find file: $inFile";
print "Check for what?: ";
$item = <STDIN>;
chomp $item;
foreach $line (<IN>)
if ($line =~ /$item/)
print "$item is on the list\n";
OUTPUT (2 runs)
Check for what?:
eggs is on the list
Check for what?:
You should try and extend this example to
output a sensible message when the item
isn’t on the list.
UFCE47-20-1 Lecture 8 2011-12 22

More Help
• Perl “homes” are:
• The standard text to learn Perl:

Learning Perl
Randal L. Schwartz, Tom Phoenix;
• There are a huge number of Perl resources on
the WWW – use a search engine
• Free Perl for Windows
(if you download rather
than purchase the DVD)
UFCE47-20-1 Lecture 8 2011-12 23
Lab session

The labs this week and next are designed to get
you started with Perl
• You will find it best to begin with simple
examples (like the ones in this lecture) and
extend them to add new features
• In the next lecture we will look at array, hashes
and simple regular expressions