Perl (pdf)

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

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

87 εμφανίσεις

1
UFCE47-20-1
Systems Development
Perl
(original lecture content by Julia Dawson)
PLEASE turn off mobile
phones, pagers etc.
X
UFCE47-20-1 Lecture 8 2011-12 3
HELP is at HAND
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
Perl
http://www.perl.org
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)
2
UFCE47-20-1 Lecture 8 2011-12 7
What is Perl?
• Originally Perl was developed as a ‘glue
language’ to pull separate applications
together
• 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
text-based
UFCE47-20-1 Lecture 8 2011-12 8
Perl is

• …Free!!! (
http://www.perl.org/
)
• …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
done!
• …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 -
http://perl.bristolbath.org/)
:
• 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 IMDB.com 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
year
• 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
must
read Chapter 9 of the course book to
"flesh out" these ideas
• You
must
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. one.pl
• Run it using: perl one.pl
• Later you will need to run Perl scripts
automatically
• 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 one.pl)
print “Hello World”;
#! /usr/local/bin/perl –w
print “Hello World”;
NB CEMS uses a non-
standard location! (Most
places it would be
/usr/bin/perl)
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
example.
• 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”;
OUTPUT
:
I like chocolate
How many? 12
3
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
OUTPUT:
‘chomp’
is a
built-in
function
The
contents of
$want are
printed out
UFCE47-20-1 Lecture 8 2011-12 14
Selection

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";
}
else
{
print "I don't have $want\n";
}
Note the strange =~
the ‘if’ condition is
true when $want
contains the string
‘book’
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
Output:
UFCE47-20-1 Lecture 8 2011-12 16
Looping
• 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
one’)
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
missing!)
UFCE47-20-1 Lecture 8 2011-12 17
Looping
-
output
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
exit
(finish) if the file does not
exist
• 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:
shop.txt";
4
UFCE47-20-1 Lecture 8 2011-12 19
Program to read a file
Input File (shop.txt)
eggs
potatoes
ice cream
avocados
yogurt
#! /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";
}
OUTPUT
** 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
eggs is on the list
Check for what?:
pickle
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
References

More Help
• Perl “homes” are:

www.perl.org

www.perl.com
• The standard text to learn Perl:

Learning Perl
Randal L. Schwartz, Tom Phoenix;
O'Reilly
• 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)

http://www.activestate.com/Products/ActivePerl/
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