This month we feature several practical Perl programs contributed by Patrick Ryan.Dear Editor:
I'm contributing several Perl scripts written for our system. I have abandoned the Bourne shell, awk, and sed, converting completely to Perl for all script programming. At its little heart, Perl is a fantastic tool for manipulating text. As a bonus, it has access to most of the C library and system calls. Perl is about as close as you can get to interpreted C.
I wrote most of these scripts as exercises for myself when I was learning Perl. As such, they are not the best designed programs I've ever written. Each one, however, demonstrates some useful functionality of Perl in a reasonably concise manner.
Note that the
pnews network program makes use of
the Perl 5 "use" keyword. This code is safely nestled inside
some version test logic and an eval statement. These programs
should work under either Perl4 or Perl5.
Some time ago, I wrote the ``Introduction to Perl'' document, which was kindly converted to HTML and made available on the Web by Neil Bowers.
Editor's Note: Here are brief descriptions of each Perl tool. Even if you aren't interested in the tool itself, the programs provide examples of well-crafted Perl.
crondaemon executes specified commands at specified dates and times. The crontab command is supplied so a user can list or install a configuration file used by the
crontabversions recognize the
-eoption to invoke a visual editor, generally
vi, to let the user update the configuration file. However, pre-SVR4 versions of
crontabdid not recognize this option so users had to perform several error-prone steps to update the configuration file.
edcron Perl program lets the user update
cron-configuration file on any system with the
crontab command, whether or not it recognizes the
-e option. Briefly, here's how it works: first
crontab -l to list the
configuration file, storing the output in a temporary file. An
editor is invoked to update the contents of the temporary file.
vi editor is employed unless the
EDITOR environment variables
specify an alternate editor. Finally,
invoked to install the edited temporary file as the
cron-configuration file for the user who invoked
kp) program traverses the output from
ps. As it goes along,
kplooks for certain patterns in the output and kills processes that it recognizes. The invocation syntax is:
Here, the arguments are:
Here are some examples:
#kp -9 '*d$'
#kp -n ryan
ps output format often
varies from system to system--that is, the number of output
columns and their ordering is not standardized--you may need to
kp for your system.
pgrepprogram is a quick-and-dirty implementation with Perl of pattern matching that gives you access to Perl's complete regular expression capabilities. I find Perl's word boundary matching feature (
\b) especially useful.
The syntax is simply:pgrep pattern
Some usage examples:
gh.ccontaining "printf" pattern:
% pgrep printf gh.c gh.c: fprintf(stderr, gh.c: fprintf(stderr,"bad address: %s\n",h); gh.c: printf("%s %s ", ip, host->h_name); gh.c: printf("%s ", *s); gh.c: printf("\n"); gh.c: fprintf(stderr,"%s:",msg); gh.c: fprintf(stderr,"HOST_NOT_FOUND\n"); % 
% pgrep '/\bprintf\b/' gh.c gh.c: printf("%s %s ", ip, host->h_name); gh.c: printf("%s ", *s); gh.c: printf("\n");
% pgrep /host/i gh.c gh.c: * get host info gh.c: struct hostent gh.c: *host; gh.c: for (s = host->h_aliases; *s; ++s) gh.c: case HOST_NOT_FOUND : gh.c: fprintf(stderr,"HOST_NOT_FOUND\n");
Note: The surrounding slashes in the regular expression argument are optional.
The invocation arguments are:
$NNTPSERVERenvironment variable is used.
A few examples:
The program will prompt you for newsgroups and a subject, invoke your favorite editor, and post the message.
%pnews -t help
Send a "help" message to your NNTP daemon and print the result.
tarlsto demonstrate the use of the Perl
tar-archive format is one of the few widely accepted file formats in the UNIX system world. The record structure can usually be found in
tarlsprogram simply lists the contents of a tar file.
The command-line syntax is:tarls tar-format-file
Here's an example displaying the contents of
% tarls /tmp/cfiles.tar ryan/ims 786 Mon Aug 29 15:26:19 EDT 1994 touchport.c ryan/ims 4982 Thu Dec 15 14:28:10 EST 1994 utmpdmp.c ryan/ims 989 Thu Dec 8 15:37:57 EST 1994 daemon_start.c % 
whichprogram with their operating systems. The UNIX
whichcommand looks for a specified program along the command-search path specified in the C shell's
.cshrcfile, and displays its path name. (It also expands command alias definitions). When I came across a UNIX-based platform that didn't have this useful utility--UNICOS, to be exact--I cobbled up this version with Perl.
Unlike the distributed version, my version searches the list of
directories in the
PATH environment variable.
In particular, the value of the Perl
is split into fields and each resulting directory examined
for the specified command-line argument.
Like the distributed version, my version also displays the
alias expansion for the specified argument.
Some usage examples:
% which cc /usr/ucb/cc /opt/SUNWspro/SC2.0.1/cc %  % which pd pd aliased to 'pushd' % 
Last Modified: Sunday, 10-Sep-95 10:02:00 PDT