From your various posts in this thread:
I'm dealing with large hash structures and big input data ... [validation] is highly inefficient
Please clarify. How big and deep are your data structures? Did you try running the code I posted here on your data, and how long did it take?
Also, how are you accessing your data structure? Directly, as in printf "%s", $data->{foo}{bar}{quz}{baz}, do you copy subsections into temporary variables, do you walk the structure recursively, etc.? Again, please see SSCCE, if you could show us something that's actually representative of your code, we can give better advice.
I don't want to add any kind of extra checking code to avoid the error ... I just want to know if it's possible (without writing any specific checking code) to know the name of the variable that produces the error
I believe that Rolf showed (Update: now even including a post by the P5Porter who implemented it himself!) conclusively that in many cases, this isn't built into Perl, so the answer to your question is no. You'll have to add the checks yourself, even if you "don't want to" :-P
You could check the arguments:
use Carp qw/cluck/;
sub myprintf {
cluck "myprintf: undef argument(s)"
if grep {!defined} @_;
printf @_
}
myprintf "%s-%s-%s-%s\n", 'quz', undef, 'baz', undef;
__END__
myprintf: undef argument(s) at - line 4.
main::myprintf("%s-%s-%s-%s\x{a}", "quz", undef, "baz", undef) cal
+led at - line 7
Use of uninitialized value in printf at - line 5.
Use of uninitialized value in printf at - line 5.
quz--baz-
Or you could check for undefs when accessing the data:
use Carp;
sub dive {
my ($ref,@path) = @_;
for my $i (0..$#path) {
$ref = $path[$i]=~/^\[(\d+)\]$/
? $ref->[$1] : $ref->{$path[$i]};
if (!defined $ref) {
carp "undef at: ".join('',
map {/^\[\d+\]$/?$_:"{$_}"} @path[0..$i]);
return undef;
}
}
return $ref;
}
my $data = { this => { is => { a =>
{ deep => { structure => [ 'abc', 'def', undef ]
} } } } };
printf "<%s>\n", dive($data, qw/ this is a deep structure [0] /);
printf "<%s>\n", dive($data, qw/ this is a deep structure [3] /);
printf "<%s>\n", dive($data, qw/ this is an example /);
__END__
<abc>
undef at: {this}{is}{a}{deep}{structure}[3] at - line 10.
main::dive(HASH(0x9e47a4), "this", "is", "a", "deep", "structure",
+ "[3]") called at - line 22
Use of uninitialized value in printf at - line 22.
<>
undef at: {this}{is}{an} at - line 10.
main::dive(HASH(0x9e47a4), "this", "is", "an", "example") called a
+t - line 23
Use of uninitialized value in printf at - line 23.
<>
-
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.