Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 1: Welcome CSE2395/CSE3395 Perl Programming.

Similar presentations


Presentation on theme: "Topic 1: Welcome CSE2395/CSE3395 Perl Programming."— Presentation transcript:

1 Topic 1: Welcome CSE2395/CSE3395 Perl Programming

2 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 2 In 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

3 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 3 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

4 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 4 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).  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).

5 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 5 Strengths 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

6 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 6 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

7 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 7 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

8 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 8 Getting 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

9 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 9 Getting help  From the lecturer ► David Abramson ► David.Abramson@infotech.monash.edu.au ► 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 ► David.Abramson@infotech.monash.edu.au ► 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

10 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 10 Getting 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

11 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 11 Texts  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

12 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 12 Unit 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

13 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 13 Topics 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

14 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 14 Topics 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

15 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 15 Topics 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

16 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 16 Topics 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

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

18 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 18 Perl 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

19 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 19 Perl 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

20 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 20 A sample program # Read in lines until EOF. while (defined ($input = )) { 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 = )) { 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";

21 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 21 A sample program # Read in lines until EOF. while (defined ($input = )) { 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 = )) { 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

22 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 22 A sample program # Read in lines until EOF. while (defined ($input = )) { 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 = )) { 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"; Keyword

23 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 23 A sample program # Read in lines until EOF. while (defined ($input = )) { 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 = )) { 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"; Functions

24 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 24 A sample program # Read in lines until EOF. while (defined ($input = )) { 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 = )) { 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"; Variables

25 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 25 Running a Perl program % perl greeting Hello, world! print "Hello, World!\n"; greeting Llama3 pages 12-14

26 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 26 Running a Perl program % chmod +x greeting %./greeting Hello, world! #!/usr/bin/perl -w print "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

27 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 27 Running a Perl program % chmod +x greeting %./greeting Hello, world! #!/usr/bin/perl -w print "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

28 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 28 Timeout # You can already read and write some Perl. print "Enter a number: "; $number = ; # 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 = ; # 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";

29 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 29 Covered 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

30 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 30 Going 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

31 Original Slides by Debbie Pickett, Modified by David Abramson, 2006, Copyright Monash University 31 Next 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 2 Camel3 pages 6-8, 58-67 perldata manpage


Download ppt "Topic 1: Welcome CSE2395/CSE3395 Perl Programming."

Similar presentations


Ads by Google