Python & Perl: Lecture 21

1,105 views
1,045 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,105
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Python & Perl: Lecture 21

  1. 1. Python & Perl Lecture 21 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
  2. 2. Overview ● On To Perl ● Installation, Editing, Documentation ● Running Perl Code ● Numbers & Stringswww.youtube.com/vkedco www.vkedco.blogspot.com
  3. 3. Perl Overviewwww.youtube.com/vkedco www.vkedco.blogspot.com
  4. 4. Learning Objectives ● Familiarity, not competence ● Online resources ● Basic coding principleswww.youtube.com/vkedco www.vkedco.blogspot.com
  5. 5. History ● Larry Wall began to develop Perl in 1987 ● The language was originally named Pearl, but there was a language by that name ● Perl stands for “Practical Extraction and Report Language” ● Rumor has it that the name Perl came first and the expansion came laterwww.youtube.com/vkedco www.vkedco.blogspot.com
  6. 6. Perl Principles ● There is more than one way to do it ● Perl: Swiss Army chainsaw of programming languages ● No unnecessary limits ● Perl is the duct tape of the Internet (not as true as it used to be anymore)www.youtube.com/vkedco www.vkedco.blogspot.com
  7. 7. What Perl is Used For ● Internet and Web Scripting ● Component Integration ● Database Programming ● Rapid Prototyping ● Data Mining ● GUIwww.youtube.com/vkedco www.vkedco.blogspot.com
  8. 8. Perls Strengths ● Free ● Portable ● Mixable ● Object-orientedwww.youtube.com/vkedco www.vkedco.blogspot.com
  9. 9. Perls Weaknesses ● Slower than C/C++ ● Long and rather steep learning curve ● Code can be difficult to read – Joke: Perl is a “write only” language – Hard even for the original programmer to read laterwww.youtube.com/vkedco www.vkedco.blogspot.com
  10. 10. Influences on Perl ● AWK ● C/C++ ● Lisp ● Pascal ● Unix shell ● BASIC-PLUSwww.youtube.com/vkedco www.vkedco.blogspot.com
  11. 11. Resources ● http://www.perl.org - site for everything that is Perl – documentation, IDEs, tutorials, links to helpful resourcesa ● http://cpan.perl.org - Comprehensive Perl Archive Network – a lot of helpful Perl tools and third-party librarieswww.youtube.com/vkedco www.vkedco.blogspot.com
  12. 12. Installation, Editing, Documentationwww.youtube.com/vkedco
  13. 13. Perl Distributions ● Linux, Unix, Mac OS – most likely already installed ● http://strawberryperl.com - Perl distribution for various Windows “When I am on Windows, I use Strawberry Perl” – Larry Wall ● http://www.activestate.com/activeperl/downloads - Perl distributions for various platforms ● Q: Which version should I install? ● A: Any version 5.8 or higher should work for this classwww.youtube.com/vkedco www.vkedco.blogspot.com
  14. 14. Online Documentation ● Main website for documentation is http://perldoc.perl.org/ ● ActivePerl installs html version of documentation ● If you install ActivePerl, perldoc command interactively to get documentation on various Perl functions ● C:>perldoc -f print ● C:>perldoc -f sqrtwww.youtube.com/vkedco www.vkedco.blogspot.com
  15. 15. Online Documentation ● Perl documentation is divided into sections ● Sections you might find useful as a beginner: – perlrun: How to execute the Perl interpreter – perldata: Perl data types – perlop: Perl operators and precedence – perlfunc: Perl built-in functions – perlre: Perl regular expressions – perlsub: Perl user-defined subroutines – perlsyn: Perl syntax such as loops and conditionalswww.youtube.com/vkedco www.vkedco.blogspot.com
  16. 16. Running Perl Codewww.youtube.com/vkedco www.vkedco.blogspot.com
  17. 17. “Hello, Perl!” on Windows ● Create hello_perl.pl in your favorite editor (I use Gnu Emacs) ● Type in the following two lines into that file: use warnings; print “Hello, Perl!n”; ● Run it from command line (set the PATH variable to point to perl.exe): – C:Perlsrc>perl hello_perl.plwww.youtube.com/vkedco
  18. 18. Notes on Perls Syntax ● Whitespace does not matter as in C/C++ ● Curly braces { } group code in blocks as in C/C++ ● Semicolons at the end of statements as in C/C++ ● Unlike in Python, indentation is not required but makes code legiblewww.youtube.com/vkedco www.vkedco.blogspot.com
  19. 19. “Hello, Perl!” on Linux/Unix ● Call the Perl interpreter directly: –/home/vladimir$ perl hello_perl.pl ● Run it as a executable script: – Add #!/usr/bin/perl at the beginning of hello_perl.pl – Make the file executable: chmod +x hello_perl.pl – Run it: /home/vladimir$ ./hello_perl.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  20. 20. -w Warnings Option ● On Windows, Linux/Unix: >perl -w some_program.pl ● On Linux/Unix: place #!/usr/bin/perl -w at the beginning of your program ● On Windows, Linux/Unix: place use warnings; at the beginning of your programwww.youtube.com/vkedco www.vkedco.blogspot.com
  21. 21. -w Warnings Option ● If you run warnings_example.pl, you notice that the Perl interpreter keeps on chugging even if the value of the $x variable is not assigned ● The recommended method is to place use warnings; at the beginning of your file ● Place use warnings; at the beginning of every Perl file you submit as homeworkwww.youtube.com/vkedco www.vkedco.blogspot.com
  22. 22. Parentheses and Functions ● Using parentheses with function calls is optional print "Hellon"; print ("Hellon"); ● If code looks like a function, it is treated as a function print 1 * 2 + 5; ## prints 7 print (1 * 2 + 5); ## prints 7 print (1 * 2) + 5; ## prints 2www.youtube.com/vkedco www.vkedco.blogspot.com
  23. 23. Adding Two Numbers ● Source code: add_numbers.pl ● $num1 - $ is a type identifier that states that the variable $num1 is a scalar variable, i.e., holds a scalar value ● The statement $num1 = <STDIN>; reads user input from <STDIN> and places it into $num1 ● chomp $num1 removes n from the end of the string ● $sum = $num1 + $num2; converts the values in $num1 and $num2 to numbers, adds them and places the value into $sumwww.youtube.com/vkedco www.vkedco.blogspot.com
  24. 24. Code Blocks ● We can use {…} to group statements in a block ● Blocks can be placed inside other blocks ● Levels of nesting can be arbitrarily deep ● Indentation can be used to increase legibilitywww.youtube.com/vkedco www.vkedco.blogspot.com
  25. 25. Example use warnings; { print "this is "; print "a block "; print "of statements.n"; } print "we are on topn"; { print "1st tiern"; { print "2nd tiern"; } print "back to 1st tiern"; } print "back to topn";www.youtube.com/vkedco www.vkedco.blogspot.com
  26. 26. Debugging Perl Code ● You may find it useful to use special tools in addition to print statements when debugging your programs ● One such tool is perldebug ● Run perldoc perldebug or read the online documentation on perldebug ● One very useful feature of perldebug is that it runs as an interactive Perl interpreterwww.youtube.com/vkedco www.vkedco.blogspot.com
  27. 27. Numbers and Stringswww.youtube.com/vkedco www.vkedco.blogspot.com
  28. 28. Basic Data Types ● Perl has four built-in data types: literals, scalars, arrays, and hashes (dictionaries) ● A literal is a value that never changes ● In Perl, there are three types of literals: integers, floats, and strings ● A scalar is a single value that can be either a number or a stringwww.youtube.com/vkedco www.vkedco.blogspot.com
  29. 29. Numbers ● Integers: 0 1972 -10 ● Underscores can be used as commas to increase legibility: 122038493 122_038_493www.youtube.com/vkedco www.vkedco.blogspot.com
  30. 30. Numbers ● Octals start with 0: 01 0523 ● Hexadecimals start with 0x: 0xFF 0x17abcdef ● Binary numbers start with 0b: 0b11111 0b00001 ● Underscores are still allowed for readability: 0xFF45_FF12 0b1111_0001_1111www.youtube.com/vkedco www.vkedco.blogspot.com
  31. 31. Numbers ● Decimals: 1.234 .5 ● E notation: 1e2 -5e10 2e-5 1.75e3www.youtube.com/vkedco www.vkedco.blogspot.com
  32. 32. Example print "Different types of numbers.n"; print " integer: ", 255, "n"; print " octal: ", 0700, "n"; print " hexidecimal: ", 0xabc, "n"; print " binary: ", 0b1111, "n"; print " floating point: ", 1.5678, "n"; print " floating point: ", 2.445e+5, "n"; print " using underscores: ", 122_038_493, "n"; ## scientific notation print 1e2, "n"; print -5e10, "n"; print 2e-5, "n"; print 0.00002, "n"; print 1.75e3, "n";www.youtube.com/vkedco www.vkedco.blogspot.com
  33. 33. Strings ● In Perl, string processing is called interpolation ● Single quoted strings are not interpolated with the exception of and ● Single quoted strings are treated as ordinary text ● Double quoted strings are interpolated ● In double quoted strings, variables are replaced with their values and escape sequences are processedwww.youtube.com/vkedco www.vkedco.blogspot.com
  34. 34. Strings use warnings; ## This print command prints string as text print tHellonworldn!; ## This print command interpolates string print "tHellonworldn!";www.youtube.com/vkedco www.vkedco.blogspot.com
  35. 35. Source Code http://vkedco.blogspot.com/2012/04/python-perl-lecture-21.htmlwww.youtube.com/vkedco www.vkedco.blogspot.com
  36. 36. Feedback Bugs, comments to vladimir dot kulyukin at usu dot eduwww.youtube.com/vkedco www.vkedco.blogspot.com
  37. 37. Reading & References ● www.perl.org ● James Lee. Beginning Perl, 2nd Edition, APRESS ● Dietel, Dietel, Nieto, McPhie. Perl How to Program, Prentice Hallwww.youtube.com/vkedco www.vkedco.blogspot.com

×