Is there any way to trigger a piece of code on a warning generated by -w?
I have a piece of code that gets run intermittently, and every so often, it gets a warning about an uninitialized variable. Now, I could put some code that tests the variable that I *suspect* is bad before usage (this is a multiple hash dereference), but I don't know that I'd catch it, and I'd have to wait for the program to run again.
What I'd rather do is setup a code trap that would record the states of those variable in the subroutine, and a few others, using Data::Dumper or somesuch, to a file.
Consider the following fragment of code. We know that 3 parameters are being passed, since the @_ == 3 test passes, but without wrapping a test around each parameter, the perl diagnostics don't tell us *which* variable is the cause for the error messages.
#!/usr/local/bin/perl -w
use strict;
{
test ('arf', undef, 'dog');
}
sub test
{
@_ == 3 or die "Incorrect number of arguments";
my ($a, $b, $c) = @_;
my %hash = ('arf' => {'spot' => {'dog' => 3}});
print $hash {$a}->{$b}->{$c};
}
Anyone got any good ideas on this? Besides using tie() to every variable I might possibly reference... And why the perl interpeter couldn't print *what* variable is uninitialized?
--Chris
e-mail jcwren
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.