(Not to hand in, but try to do by Wednesday 1/13.)
A. Perl
This assumes you have learned to use an editor. The standard ones are vi and emacs (micro-emacs). An easy one is pico , which doesn't have as many features. There are handouts for the first two. For the third, type pico and use its on-line help. Another possibility is Gnu emacs, which is good for programming; type gemacs
#!/usr/local/bin/perl5 -w # hello print "Hello world!\n";
That's all for the file. Now make the file executable by the UNIX command
chmod +x hello
Then you can run it by the command hello
Explanation of the program: The first line tells UNIX to run the program using perl5 . The Perl option -w means to check the program strictly, telling you of more Perl errors than it otherwise would.
The second line is a Perl comment line containing the name of the file, which I recommend so you can see the name of the file from a printout of the program on paper.
The print function prints the following string. In the string, stands for the newline character.
The chmod command changes the protection mode of the file; +x gives execute permission.
#!/usr/local/bin/perl5 -w # hello $s = "Hello world\n"; print $s;
#!/usr/local/bin/perl5 -w # hello $s = "Hello world"; print $s,"\n";
#!/usr/local/bin/perl5 -w # hello $s = "Hello world"; print "$s\n";
The first and fourth versions are the ones you'll typically use in programs to print out a string.
Notice that even though $s and are inside double
quotes, Perl recognizes their meaning. In contrast, try the same
thing but with single quotes:
#!/usr/local/bin/perl5 -w
# hello
$s = "Hello world";
print '$s\n';
Also notice that, unlike C++, single quotes are also used for strings, but in applications where you don't want special controls to be expanded. In ``Hello world'' there aren't any special controls so you could use either double or single quotes.
*** From now on I won't write the first two lines of the file, but do keep using them and setting execute permission.
In a longer program, it's good to put a blank line after the second line, for clarity.
cat h/wds
If there are several files, it ``catenates'' (chains) them together:
cat h/wds h/dickens
If used as a filter, cat simply copies the standard input:
cat < h/wds
Here's how to make a Perl program that does the same thing: Make
a Perl script mycat that has the usual first two lines, a blank
line (for clarity) and then this:
while(<>)
{
print;
}
Make it executable and try it in place of cat in the examles above.
There's a lot going on here. The <> means ``read from whatever argument files you find, or if there aren't any, then read from the standard input''. The while() is an ordinary while command, but in this usage it keeps reading until there is no more input. Lines are automatically read into a variable called $_ and print with nothing after it knows to print $_ . Each line read still has the newline character on it, so print doesn't need to add one.
while(<>) { chomp; $s = reverse $_; print "$s\n"; }
The chomp function simply takes a newline, if there is one, off the end of a string. That's important here so that reverse doesn't move the newline to the beginning of the line! Make the file executable and then run it:
rev /usr/dict/words | more
In contrast, a more advanced thing to do is this script, which doesn't use any while loop. Try it, using a new script file rev2
print reverse <>
What happens here is that <> refers to the entire input as an array, the function reverse recognizes that it's supposed to reverse an array instead of a string, and print recognizes that it's supposed to print the whole array, which is like a long string with embedded newlines. This illustrates one of the tricky points about Perl: The behavior of various functions can change depending on how they are used.
B. HTML
mkdir public_html
Then give read- and execute permissions to all users by
chmod a+rx public_html
(For a directory, ``execute'' permission is used to allow pass-through privileges, while ``read'' permission means everyone can list the contents. Both must be set that way for a directory to be displayed to the public by a browser.)
<HTML> <H3>Test page</H3> Hi. </HTML>
But substitute some more interesting message. Be careful about the slashes; HTML controls have matching unslashed and slashed pairs. H3 means a big heading.
Now give read permission to all users by
chmod a+r test.html
Your message is now visible all over the world! Its URL is
http://www.math.ucla.edu/~lee/test.html (if your user ID
is lee ).
To check it, start up Netscape (or Internet Explorer), either on the NT or in UNIX (by the command netscape ) and see if you can view the page.