[boulder.pm] print/exit on error?
Walter Pienciak
walter at frii.com
Thu Aug 17 10:35:20 CDT 2000
On Thu, 17 Aug 2000, Robert L. Harris wrote:
> Every now and then some of the guys around here bring me scripts to look
> at. Currently I have one that reads through about 250 files with about
> 100k lines apiece. Somewhere int he middle, they're getting an error on
> a substr operation, line is too short.
>
> What is the cleanest way to make a perl script do something along the line
> or print out a variable (the file it's currently reading), $_, and then
> exit when it gets such an error? I could check $? or such for error
> condition and then exit, but is that the best way?
>
> Robert
Most of the code I actually maintain is littered with debugging lines:
BEGIN { *DB = sub () {1} } # debugging output 0=off 1=on 2=verbose
and a bunch of stuff like
print "DB: REDIRECT! new url is [$redirected_url]\n" if DB;
or
print "DB: [$u] = [$robots_txt]\n" if (DB > 1);
or
print "DB: now processing [$file]\n" if DB;
which when I have it turned on lets me track what's what pretty easily.
That's a general thing. As for how to exit when Wrong Things Happen,
that varies with what your code is, but typical methods are
die XML::Checker::error_string($code, @_) if ($code < 200);
open( SENDMAIL, "| $SENDMAIL" ) or die "No sendmail?! $!";
&err_bad_multi_file( $@ ) if ( $@ ); # do some cleanup and die
flock( SUMMARY, $LOCK_EX) or exeunt( __LINE__, "Can't flock SUMMARY: $!" );
Hope these ideas help.
Walter
More information about the Boulder-pm
mailing list