Date post: | 01-Jan-2016 |
Category: |
Documents |
Upload: | thomas-logan |
View: | 216 times |
Download: | 0 times |
Topic 1: WelcomeTopic 1: Welcome
CSE2395/CSE3395Perl Programming
CSE2395/CSE3395Perl Programming
2Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
In this topicIn this topic
Introduction to Perl Introduction to this unit Outline of topics
► what’s examinable
Simple Perl programs► and how to run them
Introduction to Perl Introduction to this unit Outline of topics
► what’s examinable
Simple Perl programs► and how to run them
3Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
What is Perl?What is Perl?
A general-purpose programming language► The “Swiss-army chainsaw” of programming
A scripting language► partly compiled, partly interpreted
A rapid prototyping language► “Easy things should be easy, and hard things should
be possible”► Very flexible typing► Rich programming environment
A general-purpose programming language► The “Swiss-army chainsaw” of programming
A scripting language► partly compiled, partly interpreted
A rapid prototyping language► “Easy things should be easy, and hard things should
be possible”► Very flexible typing► Rich programming environment
4Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
What is Perl?What is Perl?
From man page► Perl is a language optimized for scanning arbitrary
text files, extracting information from those text files, and printing reports based on that information.
► It's also a good language for many system management tasks.
► The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal).
5Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
Strengths of PerlStrengths of Perl
Simple► simple problems have simple solutions
Practical► common tasks are supported by language
Portable► Unix, Windows, Mac OS, BeOS, AmigaDOS, ...
Extensible► vast library support► can link to other programming languages
Robust► difficult to crash► good security support
Free► as in speech and beer
Simple► simple problems have simple solutions
Practical► common tasks are supported by language
Portable► Unix, Windows, Mac OS, BeOS, AmigaDOS, ...
Extensible► vast library support► can link to other programming languages
Robust► difficult to crash► good security support
Free► as in speech and beer
6Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
Why use Perl?Why use Perl?
Programs that are► too small to bother with C► too complex for shell programming► write once, throw away (on command line)► in continuous development► for wide distribution► in an area where Perl is well supported
– e.g., Perl can be built in to Apache web server
Programs that are► too small to bother with C► too complex for shell programming► write once, throw away (on command line)► in continuous development► for wide distribution► in an area where Perl is well supported
– e.g., Perl can be built in to Apache web server
7Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
Why not use Perl?Why not use Perl?
Consider something other than Perl if► you crave clarity of code
– Perl sometimes considered difficult to read– “Obfuscated Perl” and “Perl Poetry” contests– try Java or Python instead
► speed is paramount– Perl is fast, but compiled C is about 20% faster
► it’s a large-scale project– Perl scales well, but not as well as C++
► you have to– Perl may not be loved by your boss– look for a better job
Consider something other than Perl if► you crave clarity of code
– Perl sometimes considered difficult to read– “Obfuscated Perl” and “Perl Poetry” contests– try Java or Python instead
► speed is paramount– Perl is fast, but compiled C is about 20% faster
► it’s a large-scale project– Perl scales well, but not as well as C++
► you have to– Perl may not be loved by your boss– look for a better job
8Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
Getting PerlGetting Perl
/usr/bin/perl► most Unix systems already have it
CPAN► Comprehensive Perl Archive Network► http://www.perl.com/CPAN► source code
Active State► http://www.activestate.com/► precompiled Windows version with installer wizard
/usr/bin/perl► most Unix systems already have it
CPAN► Comprehensive Perl Archive Network► http://www.perl.com/CPAN► source code
Active State► http://www.activestate.com/► precompiled Windows version with installer wizard
Llama3 pages 8-9; Camel3 page xxiii
9Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
Getting helpGetting help
From the lecturer► David Abramson► [email protected]► phone 9905 1183► room 193(75)
At the tutorial► more information soon
From unit’s courseware area► http://www.csse.monash.edu.au/courseware/cse2395/► these lecture notes
From unit newsgroup► monash.csse.cse2395
From each other► see courseware page for cheating policy
From the lecturer► David Abramson► [email protected]► phone 9905 1183► room 193(75)
At the tutorial► more information soon
From unit’s courseware area► http://www.csse.monash.edu.au/courseware/cse2395/► these lecture notes
From unit newsgroup► monash.csse.cse2395
From each other► see courseware page for cheating policy
10Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
Getting helpGetting help
From comp.lang.perl.misc newsgroup► worldwide home of Perl on Usenet
From Unix manual pages (“manpages”)► man perl► perldoc
From CPAN► http://www.perl.com/CPAN► Online manual pages and FAQs
From comp.lang.perl.misc newsgroup► worldwide home of Perl on Usenet
From Unix manual pages (“manpages”)► man perl► perldoc
From CPAN► http://www.perl.com/CPAN► Online manual pages and FAQs
Llama3 pages 9-12; Camel3 pages xxv-xxviii; perldoc manpage
11Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
TextsTexts
Learning Perl (“Llama book”)► Randal Schwartz, Tom Phoenix► O’Reilly & Associates, third edition 2001► second edition still relevant► recommended for those who want a tutorial
Programming Perl (“Camel book”)► Larry Wall, Tom Christiansen, Jon Orwant► O’Reilly & Associates, third edition 2000► second edition still relevant► recommended for those strong in programming
Learning Perl (“Llama book”)► Randal Schwartz, Tom Phoenix► O’Reilly & Associates, third edition 2001► second edition still relevant► recommended for those who want a tutorial
Programming Perl (“Camel book”)► Larry Wall, Tom Christiansen, Jon Orwant► O’Reilly & Associates, third edition 2000► second edition still relevant► recommended for those strong in programming
This icon indicates a book/manpage reference
12Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
Unit overviewUnit overview
Thirteen one-hour lectures► covering about a dozen topics
Assessment► two assignments (10% each)► final exam (80%)
– Conventional 3 hour exam– Previous exams were practical ones
Thirteen one-hour lectures► covering about a dozen topics
Assessment► two assignments (10% each)► final exam (80%)
– Conventional 3 hour exam– Previous exams were practical ones
13Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
Topics coveredTopics covered
Scalar values and variables► numbers and strings
List values and variables► arrays
Control structures► if, while, for, etc.
Subroutines► functions
Perl debugger
Scalar values and variables► numbers and strings
List values and variables► arrays
Control structures► if, while, for, etc.
Subroutines► functions
Perl debugger
14Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
Topics coveredTopics covered
Hash table (“hashes”)► aka associative arrays
File I/O► manipulating files and directories
Regular expressions► pattern matching
System administration► processing system files
Modules and libraries► code re-use
Hash table (“hashes”)► aka associative arrays
File I/O► manipulating files and directories
Regular expressions► pattern matching
System administration► processing system files
Modules and libraries► code re-use
15Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
Topics coveredTopics covered
Databases► persistent data structures
CGI► Common Gateway Interface► dynamic web pages
References► self-referential data structures
Databases► persistent data structures
CGI► Common Gateway Interface► dynamic web pages
References► self-referential data structures
16Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
Topics coveredTopics covered
Object-oriented programming► green-background slides examinable only for students
doing CSE3395
Object-oriented programming► green-background slides examinable only for students
doing CSE3395
17Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
ScheduleSchedule
18Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
Perl conventionsPerl conventions
Perl is like C and Java ...► blocks of code group with { braces }
– but braces always required with if and while► statements end with semicolon ;► white space usually optional► all words are case sensitive
– STDIN and stdin are different names
Perl is like C and Java ...► blocks of code group with { braces }
– but braces always required with if and while► statements end with semicolon ;► white space usually optional► all words are case sensitive
– STDIN and stdin are different names
19Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
Perl conventionsPerl conventions
Perl is different ...► comments start with # character
– until end of line– like shell programming
► parentheses on function call are optional– sometimes needed to disambiguate
► no need to predeclare variables– undefined variables evaluate to reasonable “empty” value
► variables start with special symbol– $ or @ or %, depending on type
► no main function/method– code is executed as it is met
Perl is different ...► comments start with # character
– until end of line– like shell programming
► parentheses on function call are optional– sometimes needed to disambiguate
► no need to predeclare variables– undefined variables evaluate to reasonable “empty” value
► variables start with special symbol– $ or @ or %, depending on type
► no main function/method– code is executed as it is met
20Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
A sample programA sample program
# Read in lines until EOF.
while (defined ($input = <STDIN>))
{
chomp $input;
push @lines, $input;
}
# Get a random number of the right size.
srand;
$pick = int(rand(scalar @lines));
# print line indexed by $pick
print $lines[$pick], "\n";
# Read in lines until EOF.
while (defined ($input = <STDIN>))
{
chomp $input;
push @lines, $input;
}
# Get a random number of the right size.
srand;
$pick = int(rand(scalar @lines));
# print line indexed by $pick
print $lines[$pick], "\n";
21Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
A sample programA sample program
# Read in lines until EOF.
while (defined ($input = <STDIN>))
{
chomp $input;
push @lines, $input;
}
# Get a random number of the right size.
srand;
$pick = int(rand(scalar @lines));
# print line indexed by $pick
print $lines[$pick], "\n";
# Read in lines until EOF.
while (defined ($input = <STDIN>))
{
chomp $input;
push @lines, $input;
}
# Get a random number of the right size.
srand;
$pick = int(rand(scalar @lines));
# print line indexed by $pick
print $lines[$pick], "\n";
Comments Comments
22Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
A sample programA sample program
# Read in lines until EOF.
while (defined ($input = <STDIN>))
{
chomp $input;
push @lines, $input;
}
# Get a random number of the right size.
srand;
$pick = int(rand(scalar @lines));
# print line indexed by $pick
print $lines[$pick], "\n";
# Read in lines until EOF.
while (defined ($input = <STDIN>))
{
chomp $input;
push @lines, $input;
}
# Get a random number of the right size.
srand;
$pick = int(rand(scalar @lines));
# print line indexed by $pick
print $lines[$pick], "\n";
KeywordKeyword
23Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
A sample programA sample program
# Read in lines until EOF.
while (defined ($input = <STDIN>))
{
chomp $input;
push @lines, $input;
}
# Get a random number of the right size.
srand;
$pick = int(rand(scalar @lines));
# print line indexed by $pick
print $lines[$pick], "\n";
# Read in lines until EOF.
while (defined ($input = <STDIN>))
{
chomp $input;
push @lines, $input;
}
# Get a random number of the right size.
srand;
$pick = int(rand(scalar @lines));
# print line indexed by $pick
print $lines[$pick], "\n";
FunctionsFunctions
24Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
A sample programA sample program
# Read in lines until EOF.
while (defined ($input = <STDIN>))
{
chomp $input;
push @lines, $input;
}
# Get a random number of the right size.
srand;
$pick = int(rand(scalar @lines));
# print line indexed by $pick
print $lines[$pick], "\n";
# Read in lines until EOF.
while (defined ($input = <STDIN>))
{
chomp $input;
push @lines, $input;
}
# Get a random number of the right size.
srand;
$pick = int(rand(scalar @lines));
# print line indexed by $pick
print $lines[$pick], "\n";
VariablesVariables
25Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
Running a Perl programRunning a Perl program
% perl greeting
Hello, world!
print "Hello, World!\n";
greeting
Llama3 pages 12-14
26Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
Running a Perl programRunning a Perl program
% chmod +x greeting
% ./greeting
Hello, world!
#!/usr/bin/perl -wprint "Hello, World!\n";
greeting
Llama3 pages 13-14; Camel3 pages 18-20
In Unix, can make a script executable with chmod to
run it just by typing its name
27Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
Running a Perl programRunning a Perl program
% chmod +x greeting
% ./greeting
Hello, world!
#!/usr/bin/perl -wprint "Hello, World!\n";
greeting
Llama3 pages 12-14
Magic code (#!) on first line of file
Interpreter (/usr/bin/perl -w) (-w turns on warnings)
Make script executable
Automatically translated by OS into /usr/bin/perl -w ./greeting
28Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
TimeoutTimeout
# You can already read and write some Perl.
print "Enter a number: ";$number = <STDIN>; # Read into $number.
for ($i = 2; $i <= sqrt $number; $i++){ if ($number % $i == 0) { print "$number is composite\n"; exit 0; }}
print "$number is prime\n";
# You can already read and write some Perl.
print "Enter a number: ";$number = <STDIN>; # Read into $number.
for ($i = 2; $i <= sqrt $number; $i++){ if ($number % $i == 0) { print "$number is composite\n"; exit 0; }}
print "$number is prime\n";
29Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
Covered in this topicCovered in this topic
Introduction to Perl Introduction to the unit Outline of topics Simple Perl programs
► and how to run them
Introduction to Perl Introduction to the unit Outline of topics Simple Perl programs
► and how to run them
30Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
Going furtherGoing further
CPAN► where to begin any Perl quest► http://www.perl.com/CPAN
Command-line options► e.g., -w to turn on warnings► writing quick-and-dirty one-line programs► Camel3 pages 486-505► perlrun manpage
CPAN► where to begin any Perl quest► http://www.perl.com/CPAN
Command-line options► e.g., -w to turn on warnings► writing quick-and-dirty one-line programs► Camel3 pages 486-505► perlrun manpage
31Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University
Next topicNext topic
Scalar values and operations► strings and numbers
Scalar variables Scalar operations if and unless while and until
Scalar values and operations► strings and numbers
Scalar variables Scalar operations if and unless while and until
Llama3 chapter 2Camel3 pages 6-8, 58-67perldata manpage