COMP421
Unix Environment for Programmers
Lecture 09: Perl Introduction___________________________________
Jeff Wiegley, Ph.D.
Computer Science
jeffw@csun.edu
09/12/2005
| “Any sufficiently advanced technology is indistinguishable from a Perl script.” –Programming Perl, 2nd edition |
| “there is a need to encrypt perl programs???” –Stefan Schmiedl |
1
Practical Extraction and Report Language
(Pathologically Eclectic Rubbish Lister)_________
For instance: To read in the data from all the files listed as command line arguments and remove all end-of-line characters.
The -w is useful for causing warnings to be printed during certain conditions such as attempting to access an undefined variable.
2
Continued:____________________________________________
3
Scalar data:___________________________________________
The basic “type” of data held by a variable is called scalar data and consists of only two things:
scalar variable names must begin with ‘$’ as in ‘$foo’.
4
String operators:_____________________________________
Strings can be concatenated by the string concatenation operator ‘.’.
results in the string “hellothere”.
Strings can copied/multiplied with the string multiplier operator ‘x’.
results in the string “hellohellohellohello”.
Strings are automatically converted to numbers as necessary.
results in $strange holding the value 98.35
5
The $ variable:______________________________________
The automatic variable $ is very handy.
Many functions set, or operate on, this variable when no other argument has been given.
Care needs to be taken in its use because it’s global6 so it can easily get set to something else before you’re done using it in the present context.
6
Chop and Chomp:___________________________________
Some quick examples of handy functions:
$teststr = "hello world\n";
chop($teststr); |
The $teststr gets the end of line character removed from it.
$teststr = "hello world";
chop($teststr); |
The $teststrgets remains unchanged.
7
Lists/Arrays:_________________________________________
Another data “type” Perl uses are lists
A list is ordered scalar data. For example: ("fred", "barney", 1, 45)
An array is a variable which holds a list.
@array = ("fred", "barney", 1, 45)
As you can see, array variable names start with ‘@’ as in ‘@goo’.
The scalar and array namespaces are independent. $foo is a completely different variable from @foo.7
You can use lists as both rvalues and lvalues...
($a, $b) = (@somearray));
scalar variables $a and $b will be assigned the first two elements of the @somearray array.
8
Slices:__________________________________________________
list usage can get really funky:
$item = $array[4] (retrieves the fifth element.)
Notice that syntax involves $ and not @!
$item = $array[-3] (retrieves the third last element)
($a, $b) = multireturnfunction($foo));
($a, $b) = ($somearray[3,7]));
$a and $b are set to the 4th and and 8th array elements respectively.
($a, $b) = ($b, $a);
Swaps the two elements.
9
Scalar vs List context_______________________________
Perl can tell from context whether or not something is operating in scalar or list context.
$length = @somearray;
$length expects a scalar value; not a list. So, perl returns the length of the array instead of an arbitrary element from the array.8
Many of Perl’s behaviors are controlled by scalar and list context.
10
<…> operator:_________________________________________
Files I/O is represented by FILE HANDLES. file handles have their own namespace (so “$variable” is a scalar variable, but “variable” is a file handle; because it doesn’t start with $.)
By traditional convention file handle names are usually capitalized. So “variable” would normally be “VARIABLE” instead.
11
basic File operations:________________________________
Open a file for reading:
open FILEH "<input.txt";
Open a file for writing:
open FILEH ">input.txt";
Open a file for appending:
open FILEH ">>input.txt";
Start a command and write to its standard input:
open FILEH "| sed -e ’s,foo,buu,g’";
Run a command and read the command’s stdout as input:
open FILEH "cat file.txt|";
Close a file: close FILEH;
Print a string to a particular filehandle: print FILEH, "Desired string\n";
12
<…> operator:_________________________________________
The most simple way to read data from a file handle is to use the <> operator.
Read a single line from the file opened as VARIABLE:
When used in list context however, the entire file is read and returned as an array of lines:
13
Simplified <>__________________________________________
When used without a file handle the <> operator changes its behavior:
This can be rather confusing. But basically <> iterates over all the elements of the @ARGV array and treats each element as a file to obtain input from.
If you want to mix command line switches and file arguments for a Perl program that uses <> then you need to do the following:
14
Conditional Branching:______________________________
if statements are just like C (and similar to Java) except the {…} braces are mandatory.
if ( ... ) {
... } elsif { ... } else { ... } |
(Notice that “elsif” is not a typo.)
But wait there’s more than one way to do it...
15
“Quicky” conditions:________________________________
You can also use english language-type constrcution...
And you can also compound statements:
Such expressions are evaluated left to right and only until a truth value as been determined.
16
Loops (While):_______________________________________
Again, same as C but braces are mandatory:
while ( ... ) {
... } |
There are three statements that control loop execution:
17
More loops:___________________________________________
Perl’s motto is “There’s more than one way…”.
|
|
18
Iterating over elements of an array:_______________
Iteration over array elements is common task. Perl has a loop for exactly that:
foreach $variable ( @somelist ) {
... }; |
If the variable name is ommitted then foreach will assign elements to the variable $_.
19
“Quicky loops”:______________________________________
It’s just a crazy, useful, free language.9
It has the advantage of being able to do it “your” way.
The disadvantage of of a lack of structure, often leading to incomprehensable code.
20
Hashes:________________________________________________
One great feature of Perl is built in hash tables: Like $ and @ for identifying scalar and array namespaces, % is used to identify another namespace for hashes.
%hoo is a hash named “hoo”.
$hoo{"key"} = "value" assigns a value to a key.
basically, hashes are method for providing a one way association of keys to values; like is used in Java’s TreeMap class.
There are a couple of functions for hashes:
21
Making complicated datastructures rapidly:_____
Datastructures are used to organize data.
How well you organize data directly effects how easily you can solve a problem.
Perl allows Hashes or Hashes of Arrays of Hashes or any other combination.
So for instance you can have a statement such as:
It takes some getting use to but compounding Perl’s basic types like this can lead to very powerful organization without the need to program new data structures yourself. But it can be a bit confusing to read.
22
Regular Expressions:________________________________
Perl is really good at manipulating text. This is largely due to its inclusion of regular expressions as part of its syntax.
23
Making complicated datastructures rapidly:_____
24
Making complicated datastructures rapidly:_____
25
Making complicated datastructures rapidly:_____
26
Making complicated datastructures rapidly:_____
27
Making complicated datastructures rapidly:_____
28
Making complicated datastructures rapidly:_____
29
Making complicated datastructures rapidly:_____
30