Learning Perl on Win32 Systems

Learning Perl on Win32 SystemsSearch this book
Previous: 10.3 Using Pathnames and FilenamesChapter 10
Filehandles and File Tests
Next: 10.5 Using Filehandles
 

10.4 A Slight Diversion: die

Consider the following a large footnote, but 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 throughout the program.[5] 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).

[5] This statement is true, 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,">c:/temp/dataplace")) {
        print "Sorry, I couldn't create c:/temp/dataplace\n";
} else {
        # the rest of your program
}

But that sort of change is 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 program with a nonzero exit status (generally indicating that something unusual happened[6]). So, rewriting the chunk of code above turns out to look like this:

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

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

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,">c:/temp/dataplace") ||
        die "Sorry, I couldn't create c:/temp/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 text relating to the most recent operating system error value. The variable is used like this:

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

The program 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";


Previous: 10.3 Using Pathnames and FilenamesLearning Perl on Win32 SystemsNext: 10.5 Using Filehandles
10.3 Using Pathnames and FilenamesBook Index10.5 Using Filehandles



Banner.Novgorod.Ru