Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
why doesn't the following code create an error if the file can't be found inside an eval block
If i add a die statement then i can get the error message from the die statement like soopen(INFH, "<$file")
But the error displayed is my custom error "can't find input file." I wanted to display the system error message but there doesn't seem to be one if you can't find the file when opening it (though you do get an error if you try and do anything with the file later). I was hoping i could put all my 'dangerous' code inside an eval block and this would catch all the system error messages for me. Is it just one of those things that perl lets you do - open a file handle on a non-existent file?eval { open(INFH, "<$file") or die "can't find input file"; }; print "Error $@", "failed" if ($@);
Would you do your error handling for this any differently? I think die statements all over the place diminish code readability myself
thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: opening missing file inside eval statement
by toolic (Bishop) on Jan 08, 2011 at 20:54 UTC | |
by oko1 (Deacon) on Jan 08, 2011 at 23:30 UTC | |
by apl (Monsignor) on Jan 09, 2011 at 14:02 UTC | |
|
Re: opening missing file inside eval statement
by Corion (Patriarch) on Jan 08, 2011 at 20:47 UTC | |
by Anonymous Monk on Jan 08, 2011 at 22:06 UTC | |
by Corion (Patriarch) on Jan 08, 2011 at 22:12 UTC | |
|
Re: opening missing file inside eval statement
by GrandFather (Saint) on Jan 08, 2011 at 22:44 UTC | |
|
Re: opening missing file inside eval statement
by PeterPeiGuo (Hermit) on Jan 08, 2011 at 21:03 UTC | |
|
Re: opening missing file inside eval statement
by JavaFan (Canon) on Jan 09, 2011 at 18:54 UTC |