Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

31
Topic 1: Welcome CSE2395/CSE3395 Perl Programming

Transcript of Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

Page 1: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

Topic 1: WelcomeTopic 1: Welcome

CSE2395/CSE3395Perl Programming

CSE2395/CSE3395Perl Programming

Page 2: Topic 1: Welcome CSE2395/CSE3395 Perl 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

Page 3: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 4: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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).

Page 5: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 6: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 7: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 8: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 9: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 10: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 11: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 12: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 13: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 14: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 15: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 16: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 17: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

17Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University

ScheduleSchedule

Page 18: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 19: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 20: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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";

Page 21: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 22: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 23: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 24: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 25: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 26: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 27: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 28: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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";

Page 29: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 30: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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

Page 31: Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

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