in reply to how to prevent open from dying
I suspect that code is followed by one or more I/O operations on that file, and you certainly don't want to attempt those operations if the file isn't open. So, while you can simply replace die with warn or print (or any other statement), what you probably want to do is simple exception handling.
Something like this ought to do:
use strict; use warnings; #always a good idea! #... some loop that sets $inputFile to the file path ... eval { # try to open the file, die and explain the problem on failure open my $INFILE, '<', $inputFile or die "Cannot read '$inputFile': +$!" #.. do I/O on $INFILE (e.g. <$INFILE>, [doc://sysread], etc.) ..# }; if ($@) { # this catches the message from any "die" in the eval{} block # the message will be in the scalar '$@'; print "!: Error while working on file-> $@" } #.. move on to the next file ..#
With this style of code, any operation inside the eval that causes a die (like an error while reading) will terminate only the eval block and set $@ to be the message from die. You can then handle this message appropriately and move on to the next file.
This is similar in spirit to Java's try{ } catch{} syntax.
|
|---|