CS 3430: Introduction to Python and Perl
Lecture 15
Department of Computer Science
Utah State University
Overview
●
Introduction To Perl
●
Installation, Editing, Documentation
●
Running Perl programs
●
Numbers and strings
●
Con...
Perl Overview
Learning Objectives
●
Familiarity, not competence
●
Online resources
●
Basic coding principles
History
●
Larry Wall began to develop Perl in 1987
●
The language was originally named Pearl, but there
was a language by ...
Perl Principles
●
There is more than one way to do it
●
Perl: Swiss Army chainsaw of programming
languages
●
No unnecessar...
What Perl is Used For
●
Internet and Web Scripting
●
Component Integration
●
Database Programming
●
Rapid Prototyping
●
Da...
Perl's Strengths
●
Free
●
Portable
●
Mixable
●
Object-oriented
Perl's Weaknesses
● Slower than C/C++
● Long and rather steep learning curve
● Code can be difficult to read
 Joke: Perl ...
Influences on Perl
● AWK
● C/C++
● Lisp
● Pascal
● Unix shell
● BASIC-PLUS
Resources
● http://www.perl.org - site for everything
that is Perl – documentation, IDEs, tutorials,
links to helpful reso...
Installation, Editing, Documentation
Perl Distributions
● Linux, Unix, Mac OS – most likely already installed
● http://strawberryperl.com - Perl distribution f...
Online Documentation
● Main website for documentation is
http://perldoc.perl.org/
● ActivePerl installs html version of do...
Online Documentation
● Perl documentation is divided into sections
● Sections you might find useful as a beginner:
 perlr...
Running Perl Code
“Hello, Perl!” on Windows
● Create hello_perl.pl in your favorite editor
● Type in the following two lines into that file:...
Notes on Perl's Syntax
●
Whitespace does not matter as in C/C++
●
Curly braces { } group code in blocks as in C/C++
●
Semi...
“Hello, Perl!” on Linux/Unix
●
Call the Perl interpreter directly:
 /home/user$ perl hello_perl.pl
●
Run it as a executab...
-w Warnings Option
●
On Windows, Linux/Unix: >perl -w some_program.pl
●
On Linux/Unix: place #!/usr/bin/perl -w at the
beg...
-w Warnings Option
●
If you run warnings_example.pl, you notice that the
Perl interpreter keeps on chugging even if the va...
Parentheses and Functions
●
Using parentheses with function calls is optional
print "Hellon";
print ("Hellon");
●
If code ...
Adding Two Numbers
●
Source code: add_numbers.pl
●
$num1 - $ is a type identifier that states that the variable
$num1 is a...
Code Blocks
●
We can use {…} to group statements in a block
●
Blocks can be placed inside other blocks
●
Levels of nesting...
Numbers and Strings
Basic Data Types
●
Perl has four built-in data types: literals, scalars,
arrays, and hashes (dictionaries)
●
A literal is ...
Numbers
●
Integers:
0
1972
-10
●
Underscores can be used as commas to increase
legibility:
122038493
122_038_493
Numbers
●
Octals start with 0:
01 0523
●
Hexadecimals start with 0x:
0xFF 0x17abcdef
●
Binary numbers start with 0b:
0b111...
Numbers
●
Decimals:
1.234 .5
●
E notation:
1e2 -5e10 2e-5 1.75e3
●
Code sample: numbers.pl
Strings
●
In Perl, string processing is called interpolation
●
Single quoted strings are not interpolated with the
excepti...
Strings
use warnings;
print 'tHellonworldn!';
print "tHellonworldn!";
Strings
use warnings;
## this is useful on Windows
## escaping , aka backwhacking
print "n";
print "C:PerlCS3430n";
print ...
q and qq
●
q/some text/ treats some text as a single quoted
string
●
Delimeters after q do not have to be /, they can be {...
q and qq
●
qq/some text/ treats some text as a double quoted
string
●
Delimeters after qq do not have to be /, they can be...
Here-Documents
●
A here-document allows you to write large amounts of
text in your program and treat them as strings
●
A h...
Here-Documents
print <<EOT;
This is a bunch of text.
The quick brown fox ...
Good bye.
EOT
Arithmetic Operators
Addition $x + $y
Subtraction $x - $y
Multiplication $x * $y
Division $x / $y
Modulus $x % $y
Exponent...
Operator Precedence
●
Operators in expressions with parentheses are evaluated
first
●
Exponentiation operators are applied...
Example
Write a Perl program that evaluates the polynomial y
= a*x^2 + b*x + c at the user-supplied values of a, x,
b, c.
...
Assignment Operators
● Perl provides the C-style assignment operators for
abbreviating arithmetic operations in assignment...
Assignment Operators
$c = $c + 5; $c += 5;
$c = $c – 5; $c -= 5;
$c = $c * 5; $c *= 5;
$c = $c / 5; $c /= 5;
$c = $c % 5; ...
Increment and Decrement Operators
++$c; increment $c by 1 and use the new value
of $c in the expression where $c
resides
$...
Equality and Relational Operators
> $x > $y
< $x < $y
>= $x >= $y
<= $x <= $y
== $x == $y
!= $x != $y
String Operators
●
Perl provides a collection of comparison operators on
string scalars
●
String operators are: eq (“equal...
string_comp.pl
Example
String Concatenation and Repetition
●
The dot (.) is the string concatenation operator
print “a” . “b” . “c” . “n”;
●
A st...
Scalar Contexts
Numeric and String Contexts
●
Scalar variables can refer to strings and numbers,
depending on where they occur in the prog...
scalar_context.pl
Example
Control Structures
if/else & if/elseif/else Selection
Let us implement this pseudocode:
If salesperson's sales >= 100
Print “$1500 bonus!”
El...
if_unless.pl
if_else_if.pl
Example
do/while and do/until Loops
●
In addition to the standard while and until loops, Perl
has do/while and do/until loops
●
wh...
do/while and do/until Loops
do {
statements;
} while ( condition );
do {
statements;
} until ( condition );
References
●
www.perl.org
●
James Lee. Beginning Perl, 2nd
Edition, APRESS
●
Dietel, Dietel, Nieto, McPhie. Perl How to Pr...
Upcoming SlideShare
Loading in...5
×

Cs3430 lecture 15

190

Published on

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

  • Be the first to like this

No Downloads
Views
Total Views
190
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Cs3430 lecture 15

  1. 1. CS 3430: Introduction to Python and Perl Lecture 15 Department of Computer Science Utah State University
  2. 2. Overview ● Introduction To Perl ● Installation, Editing, Documentation ● Running Perl programs ● Numbers and strings ● Control Structures
  3. 3. Perl Overview
  4. 4. Learning Objectives ● Familiarity, not competence ● Online resources ● Basic coding principles
  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 later
  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)
  7. 7. What Perl is Used For ● Internet and Web Scripting ● Component Integration ● Database Programming ● Rapid Prototyping ● Data Mining
  8. 8. Perl's Strengths ● Free ● Portable ● Mixable ● Object-oriented
  9. 9. Perl's 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 later
  10. 10. Influences on Perl ● AWK ● C/C++ ● Lisp ● Pascal ● Unix shell ● BASIC-PLUS
  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 libraries
  12. 12. Installation, Editing, Documentation
  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 class
  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 sqrt
  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 conditionals
  16. 16. Running Perl Code
  17. 17. “Hello, Perl!” on Windows ● Create hello_perl.pl in your favorite editor ● Type in the following two lines into that file: ● Run it from command line (set the PATH variable to point to perl.exe):  C:Perlsrc>perl hello_world.pl use warnings; print “Hello, Perl!n”;
  18. 18. Notes on Perl's 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 legible
  19. 19. “Hello, Perl!” on Linux/Unix ● Call the Perl interpreter directly:  /home/user$ 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/user$ ./hello_perl.pl
  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 program
  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 homework
  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
  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 $sum
  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 legibility ● Example: blocks.pl
  25. 25. Numbers and Strings
  26. 26. 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 string
  27. 27. Numbers ● Integers: 0 1972 -10 ● Underscores can be used as commas to increase legibility: 122038493 122_038_493
  28. 28. 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_1111
  29. 29. Numbers ● Decimals: 1.234 .5 ● E notation: 1e2 -5e10 2e-5 1.75e3 ● Code sample: numbers.pl
  30. 30. 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 processed
  31. 31. Strings use warnings; print 'tHellonworldn!'; print "tHellonworldn!";
  32. 32. Strings use warnings; ## this is useful on Windows ## escaping , aka backwhacking print "n"; print "C:PerlCS3430n"; print 'C:PerlCS3430 ', "n";
  33. 33. q and qq ● q/some text/ treats some text as a single quoted string ● Delimeters after q do not have to be /, they can be {}, [], <>, or || print q/C:Perlbinperl.exe/, "n"; print q[/usr/bin/perl.exe], "n"; print q{/usr/bin/perl.exe}, "n";
  34. 34. q and qq ● qq/some text/ treats some text as a double quoted string ● Delimeters after qq do not have to be /, they can be {}, [], <>, or || print qq{tHe said, "Hello, world!"}, "n"; print qq[t/usr/bin/perl.exe], "n"; print qq|t'"Hi," said Jack. "Have you read a book today?"'n|;
  35. 35. Here-Documents ● A here-document allows you to write large amounts of text in your program and treat them as strings ● A here-document starts with << immediately followed by a start label, then some text, and a terminating label that must be the same as the start label ● The terminating label must be on a separate line by itself with no semicolon
  36. 36. Here-Documents print <<EOT; This is a bunch of text. The quick brown fox ... Good bye. EOT
  37. 37. Arithmetic Operators Addition $x + $y Subtraction $x - $y Multiplication $x * $y Division $x / $y Modulus $x % $y Exponentiation $x ** $y
  38. 38. Operator Precedence ● Operators in expressions with parentheses are evaluated first ● Exponentiation operators are applied; if there are multiple such operators, they are applied left to right ● Multiplication, division operators are applied; if there are multiple such operators, they are applied left to right ● Addition and subtraction operators applied; if there are multiple such operators, they are applied left to right
  39. 39. Example Write a Perl program that evaluates the polynomial y = a*x^2 + b*x + c at the user-supplied values of a, x, b, c. Solution in operator_precedence.pl
  40. 40. Assignment Operators ● Perl provides the C-style assignment operators for abbreviating arithmetic operations in assignment expressions ● Any statement of the form variable = variable operator expression; where operator is one of the binary operators (+, -, *, /) can be written in the form variable operator= expression;
  41. 41. Assignment Operators $c = $c + 5; $c += 5; $c = $c – 5; $c -= 5; $c = $c * 5; $c *= 5; $c = $c / 5; $c /= 5; $c = $c % 5; $c %= 5; $c = $c ** 5; $c **= 5;
  42. 42. Increment and Decrement Operators ++$c; increment $c by 1 and use the new value of $c in the expression where $c resides $c++; use the current value of $c in the expression in which $c resides, then increment $c by 1 --$c; Same as ++$c except $c is decremented by 1 $c--; same as $c++ except $c is decremented by 1
  43. 43. Equality and Relational Operators > $x > $y < $x < $y >= $x >= $y <= $x <= $y == $x == $y != $x != $y
  44. 44. String Operators ● Perl provides a collection of comparison operators on string scalars ● String operators are: eq (“equals”), ne (“not equals”), lt (“less than”), gt (“greater than”), le (“less than or equal”), ge (“greater than or equal”) ● Strings are compared alphabetically, with the letters later in the alphabet having “greater” value ● “rabbit” gt “dragon” is true, because the ASCII value of 'r' (114) > the ASCII value of 'd' (100)
  45. 45. string_comp.pl Example
  46. 46. String Concatenation and Repetition ● The dot (.) is the string concatenation operator print “a” . “b” . “c” . “n”; ● A string s followed by the string repetition operator (x) followed by an integer n concatenates n copies of s together print “Yeah” . “!” x 3 . “n”;
  47. 47. Scalar Contexts
  48. 48. Numeric and String Contexts ● Scalar variables can refer to strings and numbers, depending on where they occur in the program ● Perl converts the value to a string or a number depending on the context in which the scalar variable is used ● Uninitialized or undefined variables have the special value undef which evaluates differently in different contexts: in a numeric context, undef evaluates to 0; in a string context, it evaluates to “”
  49. 49. scalar_context.pl Example
  50. 50. Control Structures
  51. 51. if/else & if/elseif/else Selection Let us implement this pseudocode: If salesperson's sales >= 100 Print “$1500 bonus!” Else If salesperson's sales >= 50 Print “$200 bonus!” Else Print “You did not earn your bonus.”
  52. 52. if_unless.pl if_else_if.pl Example
  53. 53. do/while and do/until Loops ● In addition to the standard while and until loops, Perl has do/while and do/until loops ● while and until loops check their condition first and then execute the body of the loop ● do/while and do/until check their condition after they execute the body of the loop
  54. 54. do/while and do/until Loops do { statements; } while ( condition ); do { statements; } until ( condition );
  55. 55. References ● www.perl.org ● James Lee. Beginning Perl, 2nd Edition, APRESS ● Dietel, Dietel, Nieto, McPhie. Perl How to Program, Prentice Hall
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×