home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

10.3 A Slight Diversion: die

Consider this a large footnote, in the middle of the page.

A filehandle that has not been successfully opened can still be used without even so much as a warning[ 1 ] throughout the program. If you read from the filehandle, you'll get end-of-file right away. If you write to the filehandle, the data is silently discarded (like last year's campaign promises).

[1] Unless you are running with the -w switch enabled.

Typically you'll want to check the result of the open and report an error if the result is not what you expect. Sure, you can pepper your program with stuff like:

unless (open (DATAPLACE,">/tmp/dataplace")) {
    print "Sorry, I couldn't create /tmp/dataplace\n";
} else {
    # the rest of your program

But that's a lot of work. And it happens often enough for Perl to offer a bit of a shortcut. The die function takes a list within optional parentheses, spits out that list (like print ) on the standard error output, and then ends the Perl process (the one running the Perl program) with a nonzero exit status (generally indicating that something unusual happened[ 2 ]). So, rewriting the chunk of code above turns out to look like this:

unless (open DATAPLACE,">/tmp/dataplace") {
    die "Sorry, I couldn't create /tmp/dataplace\n";
# rest of program

[2] Actually, die merely raises an exception, but since you aren't being shown how to trap exceptions, it behaves as described. See eval in Chapter 3 of Programming Perl or perlfunc (1) for details.

But we can go even one step further. Remember that we can use the || (logical-or) operator to shorten this up, as in:

open(DATAPLACE,">/tmp/dataplace") ||
    die "Sorry, I couldn't create /tmp/dataplace\n";

So the die gets executed only when the result of the open is false. The common way to read this is "open that file or die!" And that's an easy way to remember whether to use the logical-and or logical-or.

The message at death (built from the argument to die ) has the Perl program name and line number automatically attached, so you can easily identify which die was responsible for the untimely exit. If you don't like the line number or file revealed, make sure that the death text has a newline on the end. For example,

die "you gravy-sucking pigs";

prints the file and line number, while

die "you gravy-sucking pigs\n";

does not.

Another handy thing inside die strings is the $! variable, which contains the error string describing the most recent operating system error. It's used like this:

open(LOG, ">>logfile") || die "cannot append: $!";

This might end up saying " cannot append: Permission denied " as part of the message.

There's also the "close call" function, which most people know as warn . It does everything die does, just short of actually dying. Use it to give error messages on standard error without a lot of extra hassle:

open(LOG,">>log") || warn "discarding logfile output\n";