... your suggested approach is highly inefficient and not useful at all in my situation.
Of course, I have no idea what your situation is, but my post was motivated by the conviction that some measure of data validation is essential in any data processing application, however time-consuming it may be.
... I'm not looking for any type of methodology to circumvent or prevent the error from happening ... [is it] possible (without writing any specific checking code) to know the name of the variable that produces the error ...
But if you're not going to prevent or handle the warning in any way, what do you care about the name of the variable? Just suppress the warning.
But again, you know your business best...
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] |
The best way to figure out which variable caused the warning, in situations where the warning message itself doesn't already show you the variable name, is to check for the warning condition before the warning occurs -- which is what's been described to you already.
Alternatively, break up the single printf into multiple printf's, each only printing one variable -- then whichever line number the warning occurs on will tell you unambiguously which variable was the culprit, because it's the only possible variable on that line. Alternatively, use 5.010; and change printf "will warn: x=|%s| y=|%s| ha=|%s| hb=|%s|\n", $x, $y, $h{a}, $h{b}; to printf "will id: x=|%s| y=|%s| ha=|%s| hb=|%s|\n", $x//"<undef>", $y//"<undef>", $h{a}//"<undef>", $h{b}//"<undef>"; -- by using the //"<undef>" and including the variable name before each portion of the print, any variable that is uninitialized (ie, undefined), will show up as "<undef>". Alternatively, use a debugger (perl -d, or your favorite IDE) to manually find where the error occurs. Alternatively, make a wrapper function for printf, and manually check all arguments for undefinedness (you could even wait until after the printf, so you still get your warning, if you so desire.
#!perl
use warnings;
use strict;
use 5.010; # required for //
my %h = (a=>undef, b=>2);
my ($x,$y) = (undef, 'why not');
warn "-"x20, __LINE__, "\n";
printf "will warn: x=|%s| y=|%s| ha=|%s| hb=|%s|\n", $x, $y, $h{a}, $h
+{b};
warn "-"x20, __LINE__, "\n";
printf "will id: x=|%s| y=|%s| ha=|%s| hb=|%s|\n", $x//"<undef>", $y
+//"<undef>", $h{a}//"<undef>", $h{b}//"<undef>";
warn "-"x20, __LINE__, "\n";
sub dbg_printf {
printf @_;
for my $i ( 0 .. $#_ ) {
my @ord = qw(th st nd rd th th th th th th);
warn ">> it was the ${i}" . $ord[$i%10] . " argument to dbg_pr
+intf() that was not defined \n" unless defined $_[$i];
}
}
warn "-"x20, __LINE__, "\n";
dbg_printf("will warn, but expand: x=|%s| y=|%s| ha=|%s| hb=|%s|\n", $
+x, $y, $h{a}, $h{b});
warn "-"x20, __LINE__, "\n";
To sum up: to debug a warning, if the warning message is not sufficient to tell you where the problem is, you will have to write some checking code and/or use a debugging environment. | [reply] [d/l] [select] |