Subject: | Uninitialized value $nb in sub child (Twig.pm) |
Date: | Wed, 20 Apr 2011 16:10:23 -0400 |
To: | bug-XML-Twig [...] rt.cpan.org |
From: | Jim King <jim.king [...] simplivt.com> |
I've been seeing this warning over the last few weeks while using XML::Twig
(v3.34):
Use of uninitialized value $nb in numeric ge (>=) at
/usr/share/perl5/XML/Twig.pm line 6146.
Use of uninitialized value $nb in foreach loop entry at
/usr/share/perl5/XML/Twig.pm line 6148.
The code here is:
sub child
{ my $elt= shift;
my $nb= shift;
if( $nb >= 0) # line 6146
I put a fix in place for this which may just be a band-aid:
sub child
{ my $elt= shift;
my $nb= shift;
* if (!defined($nb))
{
$nb = 0;
}
* if( $nb >= 0)
My assumption is that if the caller did not set $nb then they meant zero.
The same thing happens with XML::Twig v3.38:
Use of uninitialized value $nb in numeric ge (>=) at
/usr/local/share/perl/5.10.1/XML/Twig.pm line 6369.
Use of uninitialized value $nb in foreach loop entry at
/usr/local/share/perl/5.10.1/XML/Twig.pm line 6371.
Here is an example of how I am processing the file:
# this only returns the English version right now
sub printError
{
my $self = shift;
my $errnum = shift;
my $errfile = "$ENV{'SVTBUILD'}/errors.xml";
my $twig = XML::Twig->new();
$twig->parsefile($errfile);
my $root = $twig->root;
if ($root->tag ne "Errors")
{
print "Unable to parse error file $errfile\n";
exit 1;
}
my $lookfor = "error.$errnum";
foreach my $description ($root->children('description'))
{
my $fn = $description->att('type');
if ($fn eq $lookfor)
{
foreach my $message ($description->children('message'))
{
my $lang = $message->att('lang');
my $module = 'task';
my $text = $message->child_text();
if ($lang eq 'en')
{
print "ERROR: $text\n";
return;
}
}
}
}
print "Unknown error (in printError)\n";
}
Here are the contents of the file:
<?xml version="1.0"?>
<Errors>
<base>com.simplivt.task</base>
<!-- define the valid task error descriptions and localize them -->
<!-- these become com.simplivt.task.error.nnnn resources -->
<description type="error.1013">
<message lang="en">Invalid name.</message>
</description>
</Errors>
--
Jim King
SimpliVT Corporation
8 Technology Drive, 2nd Floor
Westborough, MA 01581-1756
(508) 373-7258