Perl programs

You are the 37441st visitor to this page since March 20, 2001.

This page contains some information about Perl programming. It is really not organized in any special manner. My intention was to collect some stuff that I occasionally find useful, and give access to it. Browse through the material if you like, and if you find anything useful anything interesting, or you think that something is missing send me a note and I might get around to doing something about this page.

I have recently added a Perl Cookbook with recipes for solving some problems you might run into. The recipes solves problems for which I haven't found a solution in the Perl Cookbook by Tom and Nathan. The contents of this page is:

  1. One-Liners
  2. Perl 5 Modules
  3. Programs both Perl 4 and Perl 5
    c++stat - diffsolve - e-mail - lcomplexity - mail-form - match - pgrep - rename - sum-garbage - sum-process - uniqpath - wordcount - mh2ns - scheduling.tgz - pack
  4. Examples
  5. XSUB packages

One-liners

These are one liners that might be of use. Some of them are from the net and some are one that I have had to use for some simple task. If Perl 5 is required, the perl5 is used.
perl -ne '$n += $_; print $n if eof'
perl5 -ne '$n += $_; END { print "$n\n" }'
To sum numbers on a stream, where each number appears on a line by itself. That kind of output is what you get from cut(1), if you cut out a numerical field from an output. There is also a C program called sigma that does this faster.

perl5 -pe 's/(\w)(.*)$/\U$1\L$2/'
perl5 -pe 's/\w.+/\u\L$&/'
To capitalize the first letter on the line and convert the other letters to small case. The last one is much nicer, and also faster.

perl -e 'dbmopen(%H,".vacation",0666);printf("%-50s: %s\n",$K,scalar(localtime(unpack("L",$V)))while($K,$V)=each(%H)'
Well, it is a one-liner. :)
You can use it to examine who wrote you a letter while you were on vacation. It examines the file that vacation(1) produces.

perl5 -p000e 'tr/ \t\n\r/ /;s/(.{50,72})\s/$1\n/g;$_.="\n"x2'
This piece will read paragraphs from the standard input and reformat them in such a manner that every line is between 50 and 72 characters wide. It will only break a line at a whitespace and not in the middle of a word.

perl5 -pe 's#\w+#ucfirst lc reverse $&#eg'
This piece will read lines from the standard input and transform them into the Zafir language used by Zafirs troops, i.e. "Long Live Zafir!" becomes "Gnol Evil Rifaz!" (for some reason they always talk using capital letters).
Andrew Johnson and I posted slightly different versions, and we both split the string unnecessarily. This one avoids splitting the string.

perl -pe '$_ = " $_ "; tr/ \t/ /s; $_ = substr($_,1,-1)'
This piece will remove spaces at the beginning and end of a line and squeeze all other sequences of spaces into one single space.
This was one of the "challenges" from comp.lang.perl.misc that occurs frequently; I am just unable to resist those. :)

Perl 5 Modules

Here are some modules that I have written. There are several different types, some are serious and some are less serious.
Yet Another Graph Package
A Graph package to handle different types of graphs. There are currently three graph packages available on the net, which is why I do not put this one on CPAN.
List package
A package containing some useful functions operating on lists.

Set package
A package implementing sets of scalars using hashes, similar to Set::Scalar from the Perl Modules List. Comes with a test program.

Math::Polynomial, Version 0.02
A package implementing algebraic operations on polynomials. Bundled with it is an interpolation algorithm based on Lagrange's formula. The manual for Math::Polynomial.pm and Math::Interpolate.pm are also available here.

This package is also registered at CPAN, so go check there if you like.

Perl Programs

Since I am a true beliver in the principle "Learning by Examples" (I don't mean the books) you here have some simple programs. Some of them are quite simple, while other are quite tricky. Some are serious applications, some are just simple hacks.
c++stat A program that collects info about C++ programs. Like LOC, % comments, etc. This is just intended to be a test.
diffsolve This is a small script that solves difference constraints using a variant of the Bellman-Ford algorithm for Single-Source Shortest Path. Available is also a simple example of a set of constraints.
e-mail A program that checks if an e-mail address is syntactically correct. The definition is taken more or less straight from RFC-822, so it should work. By application of some Intelligence® is is propably possible to make it slightly more effective.
lcomplexity A small program that computes the linear complexity profile of a sequence of bits using the Berlekamp-Massey algorithm. The bits are fed to the program as an argument consisting of a sequence of zeroes and ones.
mail-form This is a hack inspired by the form-mail.pl program written by Reuven M. Lerner. This is a slightly more generic interface to allow people to send mail.
Instead of having a hard-coded reply and mail, this script fetches a mail template and a reply template using HTTP, perform some simple substitutions on the templates, and then send a mail and a reply.
match I wrote this script since somebody in comp.lang.perl.misc requested a Perl script to search for words matching certain patterns. I.e. if you supply a pattern "ABBA" you should find all four-letter words beginning and ending in some letter (the same) having two other letters in between (also the same) which is different from the letters in the beginning (and the end).

Examples of such words are "deed" and "noon". It should on the other hand not match "aaaa" since the first and second letter has to be different.

pgrep A Perl version of the grep(1) command. The main difference is that it accepts Perl regular expressions instead of the normal regular expressions. Manual included in runoff format.
rename Small script written by Larry Wall. It is used to rename several files at the same time. Manual included in runoff format.
sum-garbage A program that takes as arguments a number of users and write a table of the amount of "garbage" that he/she has. The script uses the module File::Find supplied in the standard distribution of Perl 5.
sum-process A program that does a ps aux and collects some statistics about each user.
uniqpath A small program that removes duplicated pathname in your PATH environment variable. Will print out the new path on standard output.
wordcount A program that counts the number of occurances of words in a text. Wrote it mainly to test the speed of associative arrays. Try something like
{ man nroff; man perlfunc; man twm; } | time wordcount

mh2ns A program written to convert an entire MH hiearachy of mails into the format used by Netscape.

scheduling.tgz A small package of two simple Perl program to compute the validity of tasks under Rate-Monotonic Scheduling. There is also a program that computes the maximum response time for each task in a set of tasks using Response Time Analysis. Rate-Monotonic Scheduling and Response Time Analysis is used to compute and validate static scheduling schemes for real-time systems.
pack Simple script to pack strings into binary formats. Uses the internal pack command inside Perl.

Perl Examples

IPC communicating example: client and server
An example of how you can use the IPC facilities of Perl. The example is from the camel book (or the manual).

XSUB packages

There is an interface between Perl and C (or C++) which is normally denoted xsub's. The following are some examples of modules implemented as Xsubs.

There is a Makefile (for GNU make) and a typemap, which are used when constructing a loadable module.

simple XSUB package [ XSUB - Perl Module - Test Program ]
The simple module is a short example of how an Xsub may look. It contains a minimal amount of code and is intended as a demonstration only. The package consists of the Xsub file simple.xs, the Perl module simple.pm used to bootstrap the Xsub and finally a test program simpletst.pl to test the package.

Last modified: Tue Jun 25 12:16:26 2002

Mats Kindahl / DoCS (matkin@docs.uu.se)