PSDT Main Web

Safe Perl
by Peter Scott

Perl has been compared to English, in that it is richly expressive, supporting multiple dialects, idioms, and colloquialisms. When you think of the range of expression in English, from Shakespeare to Ice T, from William F. Buckley to Homer Simpson, you get an idea of how eclectic the Perl language is.

Unfortunately, Perl's forgiving nature leads it to readily accept inputs that aren't what you meant to say. If you take unnecessary risks in writing Perl programs, the results may be as ambiguous as if a rapster were to give a speech at the Hong Kong Jockey Club.

There are certain, uh, prophylactic steps you can take to prevent these kind of risks from happening accidentally. I call these, collectively, "Safe Perl".

Use -w
This command-line switch enables the generation of warnings when certain common mistakes are made. They are not out-and-out errors because it is always possible for any of these conditions to be encountered legitimately; but they are worth drawing your attention to because the vast majority of times they are perpetrated, they are in fact errors. The most common error that will be caught by this flag is the use of an uninitialized variable: you prevent this by explicitly assigning values to variables before they used, rather than relying upon the implicit conversion of an undefined value to either the null string or numeric zero.

Use 'use strict'
This pragma tells Perl to generate error messages and stop upon encountering certain dangerous conditions. The most common one of these is using a variable that has not been explicitly declared; the reason that this is useful is because otherwise, if you make a typo in a variable name, it will not be flagged by Perl, but instead be treated as a brand-new instance of a new variable that springs into being with an undefined value.

use strict enforces the good behavior of making all (or nearly all) of your variables lexical; i.e., declared with the keyword "my". Such variables will be destroyed at the end of the current scope, instead of being global variables, which is the Perl default.

The first two lines of any Perl program you write, will therefore be:

#!/path-to-perl -w
use strict;


You will then be sure that your program generates no warnings or error messages before you declared it finished. Some people believe that the -w flag should not be left on in operational code; I disagree. If you have programmed your code to be "-w-clean", then any occurrence of a warning, no matter when it occurs, is an error that that deserv

es your attention, whether or not it is found by you or your customers.

Testing
It is important to test early and often when writing Perl programs. Whenever you write a short idiom which you are unfamiliar with, or a piece of code whose effects you are uncertain of, write an independent, short, test case to verify that it works. The beauty of Perl is that you can usually do such a thing in a short, one-line test, usually directly from the command line. Let's say that you are unsure of the effects of a - sign in a character class in a regular expression when you want it to match literally:

perl -Mstrict -wle 'print "Result: $1" if "(206) 374-2772" =~ /([\d-]+)/;

Note that we are still using strict and -w, and the -l flag results in an automatic newline being appended to print statements for convenience.

I will be adding more examples of Safe Perl here as time goes by, some of them drawn from my book, "Perl Debugged".

About | Services | News | Tips | Publications | Contact

Top

© Pacific Systems Design Technologies
Revised 9/19/01