Matt Garrish <> wrote in comp.lang.perl.misc:
>
> wrote:
>
> [please don't top post]
>
> > wrote:
> > > Matt Garrish wrote:
> > > > wrote:
> > > >
> > > > > I can't seem to get rid of this message :
> > > > >
> > > > > Use of uninitialized value in string eq at ./xml_simple line 154.
> > > > >
> > > > It means what it says, which is that you're attempting to compare
> > > > values using the eq operator and one or both of the scalars is
> > > > undefined. That it's reported at the beginning of the block means you
> > > > have to figure out which of the comparisons inside has the undefined
> > > > value and either give it a default value before using it, or if you
> > > > really don't care, turn of the warnings for that block.
> > > >
> > > thanks - I wouldn't have put this on the board had i not tried that....
> > >
> > OK - it can be any code inside the "block"
> >
> > That makes more sense - I kept trying to narrow it down to line 154.
> >
> > I'll try and let you know.
>
> The quick-and-dirty approach to debugging these kinds of warnings is to
> put a print statement right before each of the comparisons. You'll
> figure out really quickly that way which value is undefined as it won't
> print either.
A long while ago I wrote some code to support this technique. A sub
detect() takes a number of scalar Perl expressions given as strings
and generates code that checks each for definedness and prints a
result. You eval() this code in the context of your program.
Example:
use Detector;
my ( $a, $b, %c, @d);
$b =12;
@d = ( 0) x 10;
eval detect qw( $a $b $c{one} $d[1]);
This will report $a and %c{one} as undefined. Detector.pm contains
package Detector;
use base 'Exporter';
our @EXPORT = qw( detect);
sub detect {
my @exprs = @_;
join ";\n", map check_code( $_), @exprs;
}
sub check_code {
my $expr = shift;
"defined $expr or print q($expr undefined), qq(\\n)"
}
1;
I never use it, the manual method you describe is good enough.
Anno