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.


In reply to Re^5: determine the variable causing the error: Use of uninitialized value by pryrt
in thread determine the variable causing the error: Use of uninitialized value by ruqui

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.