Skip Menu |

This queue is for tickets about the XML-Bare CPAN distribution.

Report information
The Basics
Id: 83159
Status: resolved
Priority: 0/
Queue: XML-Bare

People
Owner: cpan [...] codechild.com
Requestors: gkpect [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: 0.50



Subject: find_node doesn't work with 0 values
find_node cant find node with key value 0 if there is only one node of mentioned name. XML example: --- <xml> <ob> <key>0</key> <val>a</val> </ob> </xml> --- Code example: --- $object->find_node( $root->{xml}, 'ob', key => 0 ); --- This is because of different checks for single node and list of nodes. From find_node method: ----- if( ref( $node ) eq 'HASH' ) { foreach my $key ( keys %match ) { my $val = $match{ $key }; next if ( !$val ); # <-- seems it should be changed to ( !defined $val ) if( $node->{ $key }->{'value'} eq $val ) { return $node; } } } if( ref( $node ) eq 'ARRAY' ) { for( my $i = 0; $i <= $#$node; $i++ ) { my $one = $node->[ $i ]; foreach my $key ( keys %match ) { my $val = $match{ $key }; croak('undefined value in find') unless defined $val; if( $one->{ $key }->{'value'} eq $val ) { return $node->[ $i ]; } } } } ----- Wouldn't it be better to replace scalar with arrayref and work similarly in both cases? Something like: ----- $node = [$node] if ref ($node) ne 'ARRAY'; ... work with arrayref ... ----- In additional - why not to use foreach to iterate through nodes?
Subject: xml_bare_bug_1.pl
#!/usr/bin/perl use Data::Dumper; use XML::Bare; my $xml = <<EOF; <xml> <ob> <key>0</key> <val>a</val> </ob> </xml> EOF my $object_parse = new XML::Bare( text => $xml ); my $root = $object_parse->parse(); my $node = $object_parse->find_node( $root->{xml}, 'ob', key => '0' ); print Dumper( $node );
Agreed that this is a problem. It has been a long standing bug in the module that nodes with a 0 value don't get handled correctly. Thanks for your input. On Wed Feb 06 08:47:35 2013, sbond wrote: Show quoted text
> find_node cant find node with key value 0 if there is only one node of > mentioned name. > > XML example: > --- > <xml> > <ob> > <key>0</key> > <val>a</val> > </ob> > </xml> > --- > > Code example: > --- > $object->find_node( $root->{xml}, 'ob', key => 0 ); > --- > > This is because of different checks for single node and list of nodes. > > From find_node method: > ----- > if( ref( $node ) eq 'HASH' ) { > foreach my $key ( keys %match ) { > my $val = $match{ $key }; > next if ( !$val ); # <-- seems it should be changed to ( > !defined $val ) > if( $node->{ $key }->{'value'} eq $val ) { > return $node; > } > } > } > if( ref( $node ) eq 'ARRAY' ) { > for( my $i = 0; $i <= $#$node; $i++ ) { > my $one = $node->[ $i ]; > foreach my $key ( keys %match ) { > my $val = $match{ $key }; > croak('undefined value in find') unless defined $val; > if( $one->{ $key }->{'value'} eq $val ) { > return $node->[ $i ]; > } > } > } > } > ----- > > Wouldn't it be better to replace scalar with arrayref and work similarly > in both cases? > > Something like: > ----- > $node = [$node] if ref ($node) ne 'ARRAY'; > ... work with arrayref ... > ----- > > > In additional - why not to use foreach to iterate through nodes?