Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-24 10:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found