I was wondering what people think is the most perfect general
beginning to a Perl program that could be used in most or all
programs. This would include whatever flags, pragmas and modules
might be useful and important.
I don't do this very often on my own scripts, but when I end up
having to maintain a script that someone else wrote, I sometimes add
the line:
use Fatal qw
void open opendir chdir rename);
near the top of the script.
This line will terminate the program with a useful error message if
any of the commands mentions (that is, open(), opendir(), chdir(), or
rename()) fail and the return value is not handled. The ":void"
keyword prevents the script from exiting as long as the return value
is handled, such as:
open(FILE, ">/blah.txt") or die "Cannot write to blah.txt: $!";
Any decent Perl programmer should ALWAYS check the return values of
these functions. I can't stress this enough. I have seen several
times where a programmer thinks that some Perl code like:
open(OUT, "> path/data.txt");
my ($key, $value);
while (($key, $value) = each %hash)
{
print OUT "$key:$value\n";
}
"won't work" because the each() function doesn't work the way I told
him it would. In reality, it is because the open() function failed.
I look at the other programmer's code and say, "You should ALWAYS
check the return value of an open() statement." The other programmer
will counter, saying something like: "There is NO reason for it not
to work."
Nevertheless, I force him add "or die "Cannot write to data.txt:
$!" and then, when we re-run the script, we find that the open()
command failed (due to an incorrect path or possibly for some other
reason that we didn't think of before).
Unfortunately, this "there is NO reason for it not to work"
attitude that too many programmers have make them think that the bug
lies in another part of the program, like in the call to each(). As a
result, I'm often told that another part of the Perl script won't work
they way I explained it would simply because the other programmer
never even stops to consider that their call to open(), opendir(),
chdir(), or rename() could ever fail.
And because some programmers STILL won't check the return value, it
is a pain to inherit and maintain their code. That is why I will
sometimes add the line:
use Fatal qw
void open opendir chdir rename);
near the top of a script, especially if the script is so long that it
is difficult to change every call to the listed functions. If they do
check all the return values of the listed functions (as every
programmer should), then including the Fatal module should have no
effect. But if they don't (which happens all too often,
unfortunately), this line will catch their errors (and report a useful
error message) despite the fact that they don't.
You may even consider putting this line in the scripts you write
yourself, just in case you ever forget to handle the return value.
Hopefully this would never happen, but mistakes do happen
occasionally.
I hope this helps, wana.
-- Jean-Luc