Perl Environmental Tutorial
Using Perl on Orcas and Sanjuan
Using Perl in Orcas and Sanjuan
Perl is located in the directory
/uns/bin/perl5 (recommended, since it is a newer version)
and also
/usr/local/bin/perl5
To avoid typing the directory everytime using Perl, just add the following line to your .cshrc (or .mycshrc) file in your home directory.
set path = (/uns/bin/ $path)
or
set path = ( $path /usr/local/bin/)
Note: Do not use /usr/local/bin/perl, because we want to use version 5.x or better. To check the version that you are using, type in
perl -v
or
perl5 -v
"Hello World" Example
Let's start with the most basic example. Create a file called hello.pl using emacs (or text editor of your choice), then type in the following:
#!/usr/local/bin/perl5 -w
print "Hello world.\n"; # print helloworld.
save it as hello.pl, and at prompt orcas% (or sanjuan), type in
perl5 hello.pl
to execute, and you will see "Hello world." being printed.
To find out more about the syntax of perl, go to
http://agora.leeds.ac.uk/Perl/start.htmlMatching Strings Example
This is an actual perl
assignment (part 1) of spring 99's 341 class. This example demonstate the use of hash tables, arrays, string matching, and opening files. It finds out the line numbers of where each word occurs. You need to create a text file, and name it sample.txt. Then type in the following:This is a sample file
and the word zebra occurs
on lines 2 and 5. The
numbers here are treated
just like words such as zebra
We also need to create a perl file, called it match.pl (download it from here match.pl)
Now, from an orcas command prompt, type in "perl5 match.pl < sample.txt" to execute it (make sure that your sample.txt and match.pl are in the same directory).
and the following results will appear
2 : 3
5 : 3
a : 1
and : 2, 3
are : 4
as : 5
file : 1
here : 4
is : 1
just : 5
like : 5
lines : 3
numbers : 4
occurs : 2
on : 3
sample : 1
such : 5
the : 2, 3
this : 1
treated : 4
word : 2
words : 5
zebra : 2, 5
The result shows what line number each word occurs in the file.
To compare to another version of match, see Greg's implementation of roughly the same functionality, match-gjb.pl. Notice that, though shorter, it's probably a lot harder to understand than Chun's simpler version. It is for this reason that Perl has the (only half-joking) reputation of being a "write-only" language (i.e., some claim that no one can read anybody else's Perl code).
For more information about string matching, go to
http://agora.leeds.ac.uk/Perl/matching.html
and also
http://agora.leeds.ac.uk/Perl/sandtr.html#remembering
for arrays, go to
http://agora.leeds.ac.uk/Perl/arrays.html
and
http://agora.leeds.ac.uk/Perl/associative.html
for file handling, go to
http://agora.leeds.ac.uk/Perl/filehandling.html
Also see Perl Links for more Perl sites.