ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
10.3 A Slight Diversion: dieConsider 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).
Typically you'll want to check the result of the 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 unless (open DATAPLACE,">/tmp/dataplace") { die "Sorry, I couldn't create /tmp/dataplace\n"; } # rest of program
But we can go even one step further. Remember that we can use the open(DATAPLACE,">/tmp/dataplace") || die "Sorry, I couldn't create /tmp/dataplace\n"; So the The message at death (built from the argument to die "you gravy-sucking pigs"; prints the file and line number, while die "you gravy-sucking pigs\n"; does not. Another handy thing inside open(LOG, ">>logfile") || die "cannot append: $!"; This might end up saying " There's also the "close call" function, which most people know as open(LOG,">>log") || warn "discarding logfile output\n"; |