Well, I would say you have these options to trap the error:
- eval it and check for errors in $@
- use an if/else statement as above.
- use a little open B,"$blah" or error_subroutine("$!")#and do stuff in the routine.
The thing is, if you do not open the file successfully and do not write/read the data you want from/to the file correctly, it could (and most likely will) cause some problems down the road.
If you are using
-w or
warnings as you should, you might get those annoying little
Use of uninitialized value errors which could keep your program from working at all.
The advice I would give you is, just
die if something goes wrong because your program just might die later (unless you clog your program with double-checks everything). So, I would use this:
unless(open FH,"$file"){
open(LOG,">>$log") or die"Error writing to error-log file...how we
+ird: $!";
print "Error at ",__LINE__," here's what happened: $!";
close LOG;
}else{
proceed as normal....
}
UPDATE: Thank you to
crazyinsomniac for pointing out that
open FH, $file || or die; will always result as true and hence never fail. Three cheers for my hero! ;)
$_.=($=+(6<<1));print(chr(my$a=$_));$^H=$_+$_;$_=$^H;
print chr($_-39); # Easy but its ok.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.