Title: Perl
1Perl Regular Expressions I
- CSCI 2467 Spring 2007
- Instructor Michael Ruth
- Computer Science Department
- University of New Orleans
- mruth_at_cs.uno.edu
2Any Questions Before We Move On?
- James H. Clark
- Co-founder of
- Netscape communications
- Silicon Graphics Inc
- BS MS in Physics from UNO
- An Important Point
- We went through Shell Programming somewhat
quickly - Lets discuss this speed, and they why
- I can never seem to remember all of your
questions - please send them to mevia email
- Any other questions before we move on?
3Topics
- Motivation
- Perl, why we need it
- Very Short History
- Perl mechanics
- Basics
- Execution
- Comments
- Hello World!
- Variables
- Scalars
- Arrays and Hashes
- Control structures
- Selection
- Looping
4Perl, why perl?
- The language is intended to be practical (easy to
use, efficient, complete) rather than beautiful
(tiny, elegant, minimal). - This, as we mentioned earlier, eases our
prototype needs - Perl has many and varied applications, compounded
by the availability of many standard and
third-party modules - Used to write CGI programs (web scripting
Amazon) - Perl is often used as a glue language, tying
together systems and interfaces that were not
specifically designed to interoperate, - and for "data munging", ie. converting or
processing large amounts of data for tasks like
creating reports - Perl is also widely used in finance and
bioinformatics, where it is valued for rapid
application development and deployment, and the
ability to handle large data sets - Additionally, perl is a very powerful scripting
language and one I am very comfortable with - There are many other powerful scripting languages
(Python, Tcl/Tk, Ruby, etc)
5The History of perl
- Larry Wall, while working as a programmer at
Unisys, released it on December 18, 1987 - The name
- Perl was originally named "Pearl", after the
Parable of the Pearl. - Wall discovered the existing PEARL programming
language before Perl's official release and
changed the spelling of the name. - Practical Extraction and Report Language is a
backronym
6Well, what is perl?
- Perl is a general-purpose programming language
originally developed for text manipulation and
now used for a wide range of tasks including - system administration,
- web development,
- network programming,
- GUI development,
- Its major features include
- support for multiple programming paradigms
- Procedural (like C)
- Object-oriented (like Java)
- Functional (like Lisp)
- automatic memory management
- built-in support for text processing
- large collection of third-party modules
- Perl is free and has a very extensive user
community - JAPH
- The Obvuscated Perl Contest
7Obfuscated Perl Contest (JAPH)
- What does the following do?
_at_Psplit//,".URRUU\c8R"_at_dsplit//,"\nrekcah xinU
/ lreP rehtona tsuJ"sub p _at_p"rp","up"(P,P)
pipe"rp","up"p(q2)f!forkmapPP
ford (p_)6p_/ P/ix?Pclose_keys
ppppppmapp_/P./ close_pwait
until?map/r/lt_gtp_dqsleep
rand(2)if/\S/print
8Perl Design
- The design of Perl can be understood as a
response to three broad trends in the computer
industry - falling hardware costs
- rising labor costs
- improvements in compiler technology.
- Many earlier computer languages, such as C, were
designed to make efficient use of expensive
computer hardware. - In contrast, Perl is designed to make efficient
use of expensive computer programmers - Perl has many features that ease the programmer's
task at the expense of greater CPU and memory
requirements - Tim Toady
9Perl mechanics I
- Some Basics
- Execution
- Comments
- Hello World
- Variables
- Scalars
- Arrays
- Hashes
- Control structures
- Selection
- Looping
10Perl Basics
- Execution
- Using sha-bang mechanism
- Using ltintepretergt ltscriptgt mechanism
- Comments
- everything past the pound is a comment
- Hello world
!/usr/bin/perl -w print (Hello, World!\n)
11Variables
- Scalars
- Simplest form of data (simple data type) that
perl manipulates - Can be a number or a string
- Treated identically
- Arrays and List Data
- A list is ordered scalar data
- An array is a variable which holds a list
- Arrays can have any number of elements 0
- Hashes
- Collections of scalar data with elements selected
by some index value - Uses the notion of key (scalar) to look up the
value associated with that key - AKA Associative arrays
12Scalar Data (Numbers)
- All numbers use the same format internally
- Perl only uses double precision floating point
values - This means there are NO integers
- Literals
- Float/Double Literals
- 1.25 1 and a quarter
- 7.25e45 7.25 x 10 to the 45th power
- -6.5e-24 -6.5 x 10 to the -24th power
- Integer Literals
- 12
- -2004
- Octal
- 0ltnumgt
- Hex
- 0xltnumgt or 0Xltnumgt
13Scalar Data (Strings)
- Strings are sequences of characters (length 0 ?)
- Literal Representation
- Single Quotes (not variable interpolated no
control characters) - hello
-
- hello\n
- Double Quotes
- Hello world!\n
- Some of the more important control characters
- \n ? newline
- \r ? return
- \t ? tab
14Operators (numeric)
- Perl provides addition, subtraction, division,
multiplication and modulus operators - (,-,/,,)
- Additionally, perl provides an exponential
operator - 23 2 to the third power
- Lastly, the comparison operators operate exactly
as expected - (lt, lt, , gt, gt, !)
- Return either true or false
15Operators (strings)
- Concatenation uses the . operator
- EX string1 . string2 ? string1string2
- String comparison operators are
- eq, ne, lt, gt, le, ge
- String repetition operator (x)
- EX red x 3 ? redredred
- EX2 5 x 3 ? 555
16Cross-Pollination
- If a string value is used as an operand for a
numeric operator (say ) it is automatically
converted to a numeric number (no warning) - Trailing nonnumerics and leading whitespace are
ignored - If a numeric is given where a string value is
needed (say x) the numeric value is converted
automatically to its string equivalent - In other words, you dont have to worry about the
variable being a scalar or string, perl will take
care of it for you
17Scalar Variables
- Just as in our shell scripts
- ltvarnamegt
- Assignment Operators
-
-
- .
- Auto increment/decrement (must be a scalar
variable) - variable variable
- variable-- --variable
18Some Simple Quick Commands
- chop
- chomp
- ltSTDINgt
- Interpolation
- Output
- undefined
19chop/chomp
- chop(ltinputgt)
- Takes a single argument (scalar variable) and
removes the last character from the string value
of that variable - EX var suey chop(var)
- var becomes sue
- chomp(ltinputgt)
- Takes a single argument (scalar variable) and
removes only newlines from the string value of
that variable - EX var Hello World\n chomp(var)
- var becomes Hello World
20ltSTDINgt as a Scalar Variable
- Every time you use the ltSTDINgt in a place where a
scalar variable is expected, perl reads the next
complete line from standard input - The string returned from ltSTDINgt normally has a
newline on the end (must use chomp) - EX chomp(a ltSTDINgt)
21Interpolation of scalars in strings
- When a string literal is double quoted it is
subject to variable interpolation - Ex
- a fred ? fred
- b a ? fred
22Output using print
- How do we get things out?
- The print function takes the values within
parenthesis and outputs them on standard output - For example
- print hello world\n
- print(hello world\n)
23The Undefined Value
- What happens when you use a variable before its
defined - You get a undef value
- Treated like zero when used numerically
- Treated like empty string when used as a string
- Normally, ltSTDINgt returns the last line read in,
but if there are no more lines to read it returns
the undef
24Lists and Arrays
- Literals
- Variables
- Operators
- Functions
- push/pop
- shift/unshift
- sort
- chomp
- ltSTDINgt
25Array Literals
- Literal Representation
- (1,2,3)
- (fred,2,3)
- ()
- List constructor operator
- (1 .. 5) same as (1, 2, 3, 4, 5)
- Quote Word
- A shortcut function
- qw(1 2 3 4 5)
26Array Variables
- Array variables holds a single list value
- Sigil is _at_ rather than
- EX _at_fred
- The value of an unassigned array is ()
- Expressions can refer to array variables as a
whole, or it can examine and modify individual
elements of the array
27Array Assignment
- Some examples
- _at_fred (1, 2, 3)
- _at_barney _at_fred
- _at_fred (1, 2, _at_barney) ? (1, 2, 1, 2, 3)
- If a scalar is assigned, then the scalar becomes
the only element of the list - _at_fred 1 is equivalent to _at_fred (1)
- If an array variable is assigned to a scalar, we
get the size of the array - size _at_fred
28Array Element Access
- All arrays in perl begin with 0
- The form is
- ltarraynamegtltindicegt
- Examples
- _at_fred (7, 8, 9)
- fred0 ? 7
- (fred0, fred1) (fred1, fred1)
- Array Slicing
- Accessing a list of elements from the same array
is called a slice - EX
- _at_fred0,1 ? (fred0, fred1)
- _at_fred0,1 _at_fred1,0
- _at_fred0,1,2 _at_fred1,1,1
- _at_fred0,1 (9,10)
29Array Element Access II
- The indices do not have to be literal integers
- Example
- a 2
- freda equivalent to fred2
- Can even use arrays as indices (slices)
- Example
- _at_barney (0,1,2)
- _at_fred(_at_barney)
- If you access a variable beyond that of the array
undef is returned w/o warning - If you set a variable beyond that of the array
the array is simple extended - You can use fred to get the last index (you can
also set it) - Lastly, a negative subscript counts backward from
the end
30Other Array Methods
- push/pop
- Pop removes and returns the last element of the
array - FM pop(varlist)
- Push accepts a list and a list of values to be
pushed onto the end of the array - FM push(varlist,list)
- shift/unshift
- Same as push/pop except that it looks at the
beginning - reverse
- Reverses the order of the elements
- sort
- Sorts the array (string)
- chomp
- Removes the \n from the end of all the strings
in the arrays
31Array and ltSTDINgt
- All remaining lines up to the end of the file and
then each line becomes an element of the array - _at_fred ltSTDINgt
- The last and empty line is not added to the array
32Hashes AKA Associative Arrays
- Hash Variables
- Literals
- Variables
- Functions
- Keys
- Values
- Each
- Delete
- Hash Slices
33Hash Variables and Literals
- Hash variables are of the form
- ltvariable namegt
- Hashes are almost never accessed as a whole, they
are normally accessed thought their variables - Elements are referenced with
- ltvariablenamegtkey where key is any scalar
expression - Ex fred fredmike A
- Literal representation of a hash
- There is no literal representation of a hash
- You can unwind arrays to hashes and vice versa
34Hash Functions
- keys(hash) function yields a list of all the
keys in the hash - In a scalar context, it yields the number of
elements in the hash - values(hash) function yields a list of all the
elements in the hash - each(hash) function is used to iterate over
every element in the hash - Returns key-value pair until empty
- EX
- while ((first, last) each(hash))
- print The last name of first is last\n
- delete fredmike removes the element
referenced by the key mike
35Hash Slices
- Just like arrays we can get slices of hashes
- Assume
- scorefred 205
- scorebarney 300
- scorewilma 195
- _at_scorefred,barney,wilma (205,300,195)
36Control Structures
- Basic issues
- If/unless
- While/until
- Do while/until
- For
- Foreach
37Control Structures Basics
- Statement Blocks
- We block off groups of statements using brackets
- BLOCK
- Conditionals are evaluated expressions (string)
- False
- The string is empty (length of zero)
- A string consisting of the single character 0
- True
- Everything else!!!
38If/unless
- IF
- if (condition)
- Statement list
- elsif (condition)
- Statement list
- else
- Statement list
- UNLESS
- unless (condition)
- Statement list
- else
- Statement list
39while/until
- WHILE
- while (condition)
- Statement list
-
- UNTIL
- until (condition)
- Statement list
40do while/until
- do while
- do
- Statement list
- while condition
- do until
- do
- Statement list
- until condition
41for/foreach
- FOR
- for (initial_exp test_exp re-init_exp)
- Statement list
-
- FOREACH
- Takes a list and iterates through it
- foreach identifier (_at_list)
- Statement list
-
- Use the identifier variable inside the statement
list - Example
- for each b (_at_fred)
- print b
42Resources
- Learning Perl
- The Llama Book
- Programming Perl ?
- The Camel Book
- Mastering Regular Expressions
- The Owls Book
- Perl Cookbook
- The Ram Book
43The future
- Perl Regular Expressions II
- Basic I/O
- Regular Expressions
- Functions
- Files
- Access
- Contents
- Directories
- Introduction to C Programming
- Among others a History of C
- More or less follow the book
44Mystic Krewe of Barkus 2/11 _at_ 2pm
45Questions?