Perl lecture 1


Introduction to Perl

Perl stands for Practical Extraction and Report Language. It's a portable language that is useful for writing quick and dirty programs. Perl is in many ways a simple language. You don't have to know many special incantations to compile a Perl program - you can just execute it like a shell script. The types and stuctures used by Perl are wasy to use and understand. Perl doesn't impose arbitrary limitations on your data - your strings and arrays can grow large as they like (so long as you the have memory), and they are designed to scale well as they grow. Instead of forcing you to learn new syntax and semantics, Perl borrows heavily from other languages you may already be familiar with such as C, and sed, and awk, English, and Greek. In fact, just about any programmer can read a well-written piece of Perl code and have some idea of what it does.

Pluses: Viewed in its best light, Perl is a language that encapsulates the best features of the shell, sed, grep, awk, tr, C and Cobol. If you are familiar with these tools, you can write very powerful Perl programs rather quickly and easily. In particular, you can write programs that duplicate the functionality of shell scripts but are all in one file (indeed, they are all in one language) and thus are much more efficient.

Minuses: Perl is a jumble. It contains many, many features from many languages and programs. It contains many differing constructs that implement the same functionality. For example, there are at least 5 ways to perform a one-line if statement. While this is good for the programmer, it is extremely bad for everyone but the programmer (and bad for the programmer who tries to read his own program in 6 months), and has led to Perl being called a write-only language.

Back to top


Perl help

Unfortunately, perl is huge, so I'll only be able to give you but a flavor of it. The perl manual is online in the form of the perl man pages, which are broken up into a number of subsections. Do man perl. There are tons of online help available for Perl. Here is a good starting page http://perl.about.com/od/perlmanuals/. There are many such sites, I suggest you make use of them when required.

There are two recommended books in case you want more. First is Learning Perl by Schwartz, and Programming Perl by Schwartz and Wall. Both are published by O'Reilly & Associates.

Back to top


Calling syntax of Perl

Like awk, perl works on a program. You can specify the program as the first argument to perl, or you can put the path of perl's executable on the first line of the perl program, preceded by #!. (You can also specify the program on the command line -- see the man page). In our lab machines, perl is in /usr/local/bin/perl. So much for portability. There used to be two versions on our system. As a consequence there is a /usr/local/bin/perl and a /usr/local/bin/perl5. perl is a link to perl5. If you have a question about which version you are using just type perl -v and it will print out version information and exit.

Back to top


Simple program

Perl programs are more like C than awk -- you must request lines from standard input rather than getting them by default. Here's the canonical hello world program in perl:
UNIX> cat hw.perl
print "hello world\n";
UNIX> perl hw.perl
hello world
UNIX> 
So, like awk, there is a print statement. Unlike awk, you have to provide your own newline, and it will only print out one value (i.e. you can't have it print out a bunch of comma-separated values as in awk).

Like C, perl programs are tokens separated by whitespace (i.e. commands can span lines). You must end all commands with semicolons.

UNIX> cat simp.perl
 
print "Jim\n";
print 1.55 . "\n";
print "Jim" . " " . "Plank" . "\n";
print (("5" + 6) . "\n");
 
UNIX> perl simp.perl
Jim
1.55
Jim Plank
11
UNIX> 

Back to top


Comments in Perl

Perl comments begin with the pound sign (#) just as in the other scripts we have studied. However, they can begin anywhere on a line and extend to the newline.
	#this is a comment at the left margin

	print $red;	# and this is a comment starting elsewhere
Note the semicolon after the print statement. Just like in C, all simple statements end with a semicolon.

Back to top


Variables and Data Types

Perl has three basic variable types that we will cover; scalars, arrays, hashes. Variables do not have be to explicitly declared, just used and the type is inferred, nor do variables have to be initialized. If a variable is used prior to an assignment of a value perl gives it the value undef which is NOT null but is the undefined value. Later we will talk about testing for this value. For now you just need to know that using such a value will not usually cause an error but may not give the desired results.

Back to top


Operators

Perl uses many operators to compare strings and numeric values. Those of you who have done C programming are familiar with many of these and the rest of you have seen similar usages when using sh, sed and awk. For numeric values the following are the most common operators and their purpose

Operator Purpose Example
+ addition $a= 3 + 4; #$a = 7
- subtraction $b= 7 - 3; #$b = 4
* multiplication $c= 3 * 4; #$c =12
/ floating point division $d= 10 / 3; #$d = 3.333333...
% modulo (always integer) $e= 10 % 3; #$e = 1
$e= 10.43 % 3.9; #$e = 1
** exponentiation $f= 5 ** 3; #$f = 125

Also available are the normal range of numerical comparisons operators. These operators return 1 if the comparison is true and something which, if coerced to an integer, evaluates as 0 if false;

Operator Purpose
== equal
!= not equal
< less than
> greater than
<= less than or equal to
>= greater than or equal to

Like C, you cannot do numerical comparisons with strings. If you try, perl will convert the string to some numeric value. Leading whitespace is ignored and trailing non-numeric values are discarded. Thus " 23.928asldf543" becomes 23.928 and "johnson" and "johnson4" become 0. This same thing will happen if you try to use a string any place perl is expecting a numeric value. There is only one operator which corresponds to any of the arithmetic operators and that is "." which concatenates 2 strings. It does not modify either string rather it returns a new string which is the two operands together

 
	$line="This"." is"; # $line = "This is"
	$line=$line." my country"; # $line = "This is my country
And, sensibly, the comparison operators for strings are different from those for numeric values, they are in fact string representations of those other operators.

Operator Purpose
eq equal
ne not equal
lt less than
gt greater than
le less than or equal to
ge greater than or equal to

Back to top


Exercises

Due date: Wed, April 6th at 11:59pm
  1. Summation

    Write a script (sum.pl) to add together the numbers 1 through 83 and print the result to the screen.

  2. Choice

    Write a script (choice.pl) that will print "Systems Nominal" or "Blue Screen of Death" to the screen depending on a (hard-coded) variable.  (Hint: use a program control such as an if...else, etc. to test the value of the variable)

  3. Shopping List

    Write a script (list.pl) that creates and initializes an array with values "milk", "cheese", "bread", "meat", "fruit", and "vegetables".  Then loop through the array, printing out the values as you iterate through the array.  However when you print out the contents of the array, do not print out "meat" (out of respect for our vegetarian friends).

Be sure to comment your code where necessary.