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)_________

2


Continued:____________________________________________

3


Scalar data:___________________________________________

The basic “type” of data held by a variable is called scalar data and consists of only two things:

  1. A string. Ranging from 0 bytes to filling up memory. Comes in two flavors:
    1. Double quoted strings:
      • Are variable interpolated (variables are replaced with current value).
      • Many escape sequences exist such as ‘\n’.
    2. Single quoted strings:
      • Are not variable interpolated
      • Only two escape sequences: ‘\” and ‘\\
  2. A number. Stored as a double precision floating point value. Perl has no concept of “integers”4

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:

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:

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:

$aline = <VARIABLE>;

When used in list context however, the entire file is read and returned as an array of lines:

@everything = <VARIABLE>;
Even cooler: chomp(@everything = <VARIABLE>;)

13


Simplified <>__________________________________________

When used without a file handle the <> operator changes its behavior:

  1. When command line arguments are absent then <> <STDIN>.
  2. If command line arguments were given <> processes each command line arguments as a name of a file to slurp input from. Thus in the example ./perl.pl -v hello <> would first return all the lines from the file named -v and then all the lines from the file named hello.

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...

$a = $b unless ($c == 100);
$a = $b if ($c == 100);

And you can also compound statements:

$a=$b && $c=$d ($c=$d will only execute if $b had a non-zero value.)
FILE=open("<name") || die "could not find file";

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:

  1. last label is used to terminate the loop labeled label (Or the inner-most loop if label is omitted same as break; would in C.)
  2. next label terminates only the current iteration of the loop labeled label (Or the inner-most loop if label is omitted, same as continue; would in C.)
  3. redo label Restarts at the beginning of the loop labeled label without evaluating the loop conditional. (Or the inner-most loop if label is omitted. There is no equivalent in C.)

17


More loops:___________________________________________

Perl’s motto is “There’s more than one way”.

      unless ( ... ) {  
        ...  
      }

      do {  
        ...  
      } while ( ... );

      for ( ... ; ... ; ... ) {  
        ...  
      };

      do {  
        ...  
      } until ( ... );

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

$b++ until ($b==4);
$b-- while ($b>0);

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:

%myhash{$akey}[6]{$anotherkey}=$filename}
  1. {$akey} will return some value that is actually an array.
  2. [6] will return the seventh element of that array.
  3. {$anotherkey} will treat that item as a hash and assign the value in $filename to the key $anotherkey.

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