You can use PERL locally on your PC, or you can use it directly on the Unix system. The advantage of using it locally is that the turnaround when you run-test-run is almost instant. The advantage of using it on Unix is that when it runs, you're pretty sure it'll work as a cgi script, too.
Running on the PC. Once you've installed Perl in a directory (perhaps c:\perl) on your PC, just open an MS-DOS window for editing, and cd to get to c:\perl. Then use your editor to create a tiny little hello program like
print "hello again, world!\n";
then save it as prog1.pl, and open another MS-DOS window into your perl directory. type
perl prog1.pl
and it'll run. Now try adding about 3 more lines (more print statments), then injecting some syntax errors (maybe leave off the semicolon), and run it again.
Running on Unix. To run perl directly on Unix, type
/usr/local/bin/perl test.pl
assuming that your program's name is test.pl. You might try that with our hello.pl example.
You can speed things up a bit by editing the hidden file ".cshrc" and adding this line:
alias perl /usr/local/bin/perl
The next time you log in, you will be able to just type 'perl' instead of '/usr/local/bin/perl'. (To force this capability into action right now, just login again.)
Working in the Unix environment is something you should all know how to do, but I'm not going to teach you. Go get a book, learn the ten top Unix commands and about fifteen common 'vi' editor commands, and you're in business. Do it SOON....
Basic PERL Facts
If you're fairly experienced with computers by now, you should be able
to learn Perl from the examples and from Web reference information. I gave
you a link to one such site (http://perl-com.songline.com/faq/)
in the syllabus. Here's another: ftp://ftp.cis.ufl.edu/pub/perl/faq/FAQa
Variables
Scalars (single variables) look like this:
$a = "This is an alphabetic variable.";
$n = 34.4; # A numeric variable
Anytime you use a symbol beginning with $, it's automatically declared. Variables have no special type; they take on the type of whatever is assigned to them. As you see, assignment is with the "=" operator.
Arrays are referred to as a whole in this way:
@fruit=("banana","apple","orange");
To get at these elements, we could use
print $fruit[0]."\n"; # to print banana. The dot (.) is string concatenation.
or equivalently,
print "$fruit[0]\n";
The double quotes allow the interpretation of variables within them. If you wanted to print exactly what you see (instead of the contents of $fruit[0]) you could command:
print '$fruit[0]';
The Special Variable
Perl can be thought of as a big filtering system. If you don't give
it a variable, it automatically assumes that you're talking about a variable
called "the current line" and symbolized by $_ (dollar underscore.)
For instance
$_ = "My goodness!\n";
print;
would print the contents of the special variable.
Query 7.1: Experiment with perl. What would happen if you loaded
up an array named @list with five items,
and then tried
print @list;
Control operators are few and simple.
while (boolean expression)
{stuff you want to do
}
if (boolean expression)
{stuff you want to do}
elsif (another boolean expression)
{other stuff you want to do}
elsif (yet another boolean)
{... you can stack 'em up forever. Or don't use any elsif if you don't
want one.}
Example:
# argecho.pl -- show any command-line arguments (argument-echo)
$argNum = @ARGV;
$i = 0;
while ($i < $argNum)
{
print $i, " -- ", $ARGV[$i], "\n";
$i++;
}
Go make some syntax errors. It's instructive.
File i/o; pattern matching
Construct a little file named citymile.dat with these contents:
Miami, FL, Orlando, FL 257
Boston, MA, Chicago, IL, 963
Tampa, FL, Orlando, FL 105
Then construct a perl program as follows:
#chigo.pl -- print if line contains the word "Chicago"
while (<STDIN>)
{ print if /Chicago/;}
And stimulate it as follows:
perl chigo.pl < citymile.dat
It should print Boston, MA, Chicago, IL, 963
If you run it interactively, as follows
perl chico.pl
then type some line, which is ignored
but if you mention Chicago, it is echoed
but if you mention Chicago, it is echoed
until you type a line containing control-c to end it
^C at chico.pl line 5, <STDIN> line 6The pattern matching phrase /Chicago/ was automatically applied to all the lines. The 'print if /Chicago/' phrase was automatically applied to the special variable, over and over until standard-in ran out of stuff.
Regular Expressions
You can fish for many things with these search strings, using
meta-characters. An example: the character "." (period) will match any
character. So if you search for
/M.ke/you will find Make, Mike, Mjke and even M$ke. But you wouldn't find Maake because there isn't precisely one character between the M and the k.
There's a whole bunch of neat search characters; I'll give you a photocopy of a list of them.
Splitting Strings
A very important capability is called 'split'. It looks like this:
@newVals = split(/-/, $valString);
This says to split the string in $valString into an array named @newVals. If $valString="yes-no-down-up", then after this split operation, @newVals will contain the following elements as $newVals[0], [1], etc:
"yes","no","down","up"
We'll do a lot with split, on Thursday.
Query 7.2: Go get Perl running on your PC if you haven't already. Try
all the examples above, and make up more of your own. Read the FAQ's about
Perl, and get busy learning how this stuff works!