in reply to To die or not to die
You could always check if the file exists before trying to open it, and use that to decide whether to open it for input or output. That way, you know that if it exists but you can't open it for input, the error is serious enough to warrent a die (or warn depending on your error handling).
if ( -e $file ) { open FH, $file or die "Failed: $! trying open existing $file\ +n"; # read it open FH, '>', $file or die "Failed: $! trying to re-open $file\n"; + } else { open FH, '>'.$file or die "Failed: $! trying to create new $file\ +n"; # do stuff }
This way, if you get out of the if/else, you know you have a file open for output ready for (re)writing.
It also means that you get a meaningful error msg if the file is read-only.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: To die or not to die
by Aristotle (Chancellor) on Oct 29, 2002 at 20:50 UTC |