| Front Page | News Headlines | Technical Headlines | Planning Features | Advanced Search |
Robelle Sponsor Message

January 2002

Boosting Your e3000 Productivity

An Introduction to Perl

By Dave Lo

Perl is an interpreted language that is the brainchild of Larry Wall. He continues to develop and guide the language, which, through the help of the net community, is available on virtually every computer platform, from Apple’s Macintosh to MPE.

Perl, officially known as either Practical Extraction and Reporting Language or Pathologically Eclectic Rubbish Lister, is a popular language for implementing web page CGI scripts, processing string data, even system administration. The official Perl web site is www.perl.com.

However, Perl is much more than a sometimes-odd-looking web scripting language. It has enough power to be a full programming language. One glance at some of the O’Reilly Perl books will testify to that (Perl for Bioinformatics, Perl for System Administration, Perl for Website Management).

If you think of perl as a shell-like programming language that evolved from quick-and-dirty handling of text, lists, associate arrays, and regular expressions, then you’re already thinking Perl. Let’s dive in!

Scalar variables

0 # Perl has no line numbers, they’re here for reference only
1 $num = 123;
2 $str = “abc”;
3 print “number=$num string=$abc”;

Line 0 is a single-line Perl comment, which are similar to Unix shell comments. There are no multi-line comments.

Perl is not a strictly typed language, so a variable can hold either numeric or string values. Also, no declarations of variables are needed before they are used. Line 1 and 2 assign a number and a string. These type of variables are known as scalar variables (they hold a single primitive value), and their names are prefixed by a $ sign. One characteristic of Perl is that all variables names have a prefix character such as $. This may seem strange, but not when you think of similarities to Unix shell scripts.

List variables

1 @list = (12.3, “abc”, 4..6);
2 foreach $i (@list) {
3 print “$i\n”;
4 }
5 $count = @list;
6 print “There are $count items\n”;

Another fundamental variable type in Perl is List. Line 1 shows the assignment of a list variable, whose name is prefixed with a @ sign. A list variable is like a 1-dimensional array. But unlike strictly typed languages, a list in Perl can contain mixed types of data. Here, the list contains 5 values: the number 12.3, the string “abc”, and numbers 4, 5, and 6.

Lines 4-6 are a typical way of looping through all the items in a list, using the foreach construct. In line 5, notice that literal strings in double quotes can contain variables that are dereferenced.

Line 7 may initially look like an error (assigning a list variable to scalar variable), but it is a common occurrence in Perl and shows an important concept: context. Think of context as fancy type-conversion. Here, because the left-hand-side of the assignment is a scalar variable, the right-hand-side must also be scalar. So the list variable is “evaluated in a scalar context”, which does the convenient thing of returning the number of items in the list. You’ll discover that Perl has many of these types of conveniences built-in.

We could also have accessed the list in a traditional array-like fashion by numeric indexing. Like C, Perl starts array indexes at 0.

9 for ($i=0; $i<@list; $i++) {
10 print “$list[$i]\n”;
11 }

Note in Line 10 that we prefixed the list with a $. This is because list[$i] is a scalar value, so a scalar prefix is needed. Another important point is that scalar and list variables names are in different namespaces. This means you can simultaneously have a $abc scalar variable and @abc list variable. Use this feature carefully, otherwise you end up write hard-to-understand code such as $abc = $abc[$abc].

Hash variables

A powerful feature in Perl are hashes (otherwise known as associate arrays). Hashes are like arrays that are indexed not by sequential numbers, but by string value. It is a simple way to store key-value pairs.

$review{“Monsters Inc.”} = “funny and original”;
$review{“Harry Potter”} = “true to the book”;
$review{“Lord of the Rings”} = “also true to the book except for Arwen”;

Here is an example of parsing a string similar to those commonly returned from an HTML form:

1 $line=”option=1&company=Robelle&product=Qedit,Suprtool”;
2 @pairs=split(/&/,$line);
3 foreach $item (@pairs) {
4 ($name, $value) = split(/=/,$item);
5 $form{$name} = $value;
6 }
7 @list = keys(%form);
8 foreach $name (@list) {
9 print “$name = $form{$name} \n”;
10 }

In Line 2 and 4, the split function takes a regular expression (although we are using it here just for a simple string search) and a string, finds the substrings that are separated by the regexp, and returns a list of those sub-strings. So in Line 2, we are looking for the substrings separated by an ampersand &. Split returns this list of three strings:

option=1
company=Robelle
products=Qedit,Suprtool
In Line 4, the split works in a similar way to break up “option=1” into a list of two elements (“option”, “1”). Notice that Perl allows simultaneous assignments of several variables. The assignment puts the first element in $name and second element in $value.

In Line 5, the assignment to a hash looks almost like assigning to an array assignment, except that curly braces are used instead of square brackets

In Line 7, the keys function returns a list of all the key values in a hash (“option”, “company”, “product”). Notice that a hash is prefixed by a % sign when used in hash context. If you wanted to get all the values in a hash, you would use the values function, which would have returned (1, “Robelle”, “Qedit,Suprtool”)

Perl on MPE/iX

Perl for MPE/iX is available for download from the HP Jazz Web site: jazz.external.hp.com/src/hp_freeware/perl/. Perl was ported to the HP 3000 by Mark Bixby. Here are some notes on Perl/iX from the Jazz Web site:

“The following prerequisites apply to Perl on MPE/iX: MPE/iX 6.0 or greater. This software has not been tested on versions earlier than 6.0. Approximately 325,000 sectors of available disk space.” Perl has been linked to use the shared libraries /lib/*.sl and /usr/lib/*.sl; if these libraries are missing or have restrictive permissions then Perl will not run. These libraries are a standard part of MPE FOS starting with 6.0. If for some reason you are missing these libraries, you can recreate them by logging on as MANAGER.SYS and then running the shell script /PERL/PUB/mpebin/LIBS.hp3000.

Integration With MPE

A few MPE-specific modules are starting to become available for Perl. The following is a partial list; none of these are bundled with this distribution, so if you want to play with them you’ll have to download and build them yourself:

MPE::CIvar — Ken Hirsch’s interface for MPE/iX JCWs, CI variables, and the HPCICOMMAND intrinsic. Please see invent3k.external.hp.com/~MGR.HIRSCH/CIvar.html for more info.

MPE::IMAGE — Ted Ashton’s interface for MPE/iX TurboIMAGE databases. Please see search.cpan.org/search?dist=MPE-IMAGE for more info.

Web Resources for Perl

These include www.perl.org, the Perl user community; www.perl.com, O’Reilly’s official Perl home; and www.cpan.org, the Comprehensive Perl Archive Network for perl distribution, doc, modules. If you have trouble installing packages from CPAN, read Ken Hirsch’s installation tips at invent3k.external.hp.com/~MGR.HIRSCH/cpan.html

O’Reilly books

• Learning perl - A good introduction to the language

• Programming perl - Known as the “camel book”, it is the definitive Perl reference written by the authors of Perl

• perl cookbook - Solutions for common operations. For example, Problem: How to do something to every word in a file?

Solution: Split each line on whitespace:

while (<>) {
for $chunk (split) {
# do something with $chunk
}
}

 


Copyright The 3000 NewsWire. All rights reserved.