Esteemed Monks,
I am writing a quick script to analyze a bunch of files using substr and sometimes I see "substr outside of string." If this happens once, I'd like to skip the entire file. This is where I am having trouble.
Ideally, I would like to see this:
Checking file_a...
Checking file_b...
Skipping file_c... (due to the substr message)
Currently I see this:
Checking file_a...
Checking file_b...
substr outside of string at ./script_name.pl line 35, <$fh> line 1.
substr outside of string at ./script_name.pl line 35, <$fh> line 2.
substr outside of string at ./script_name.pl line 35, <$fh> line 3.
(and so on until the end of the file...)
My first approach was using an eval block with $@ until I discovered (through perldiag) that the substr message is a warning, not an error. The only way I recall being able to handle warnings is via %SIG--is this correct?
I then tried using $SIG{__WARN__} and I was able to suppress the warning; however, I cannot get it to skip the rest of the file.
The relevant code looks like this:
use warnings;
use strict;
use File::Find;
# $data_dir is determined.
$SIG{__WARN__} = sub {
print "Skipping...\n";
# skip the rest of the loop for the file.
}
sub find_process {
# open each file that meets the naming criteria.
# begin looping through the file.
# attempt to access the substr.
# skip this file if there was a substr warning and report it.
}
find(\&find_process, $data_dir);
Thank You.
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.