Error checking is actually taken care of by "use autodie;". I personally don't like to do that because often a die message can explain to the user the context of what is happening in addition to the actual file that couldn't be opened, e.g. "can't open configuration file: $file_name". This sort of thing becomes more important if there is a GUI involved as opposed to a command line.
Also other "tweaking" of the die message can be done. There is a difference between die "xyzzy" and die "xyzzy\n" This controls whether or not the user gets the Perl line of code number. Sometimes, it can confuse users if too much info is given with terminology that they don't understand.
###### with auto die ######
open XXX, '<', "XXX";
#Can't open 'XXX' for reading: 'No such file or directory' at C:\Proje
+cts_Perl\testing\die_messasges.pl line 7
###### without auto die ###
# trailing \n in the die message suppresses the line number.
open XXX, '<', "XXX" or die "couldn't open Config file, XXX!\n";
# couldn't open Config file, XXX!
open XXX, '<', "XXX" or die "couldn't open XX file!";
#couldn't open XX file! at C:\Projects_Perl\testing\die_messasges.pl l
+ine 5.
open XXX, '<', "XXX" or die "couldn't open config file XX!, $!";
# couldn't open config file XX!, No such file or directory at C:\Proje
+cts_Perl\testing\die_messasges.pl line 6.
update:
I didn't show every possibility.
Some points: 1)autodie is pretty cool, especially for short quick scripts. But, there is no context information. In a complex app, it may not be apparent to the user what this file is about. 2) Add or not the trailing "\n" to a die message to control reporting of Perl line number. 3) Often $! is just confusing noise depending upon the App. I use all of the above options in one situation or another. I can't say: "always do it way #X".
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.