|
January 2002 Boosting Your e3000 Productivity An
Introduction to Perl 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 OReilly 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 youre already thinking Perl. Lets dive in! 0 # Perl has no line
numbers, theyre here for reference only 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. 1 @list = (12.3,
abc, 4..6); 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. Youll 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++) { 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]. 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; 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; 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 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 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. 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 youll have to download and build them yourself: MPE::CIvar Ken Hirschs 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 Ashtons interface for MPE/iX TurboIMAGE databases. Please
see search.cpan.org/search?dist=MPE-IMAGE for more info. These include www.perl.org, the Perl user community;
www.perl.com, OReillys
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 Hirschs
installation tips at invent3k.external.hp.com/~MGR.HIRSCH/cpan.html
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 (<>) {
|