CC: | <arunbear [...] cpan.org> |
Subject: | Iterator next() method produces an error when passed a start key and tree is empty |
Date: | Tue, 14 Jul 2009 21:15:15 -0500 |
To: | <bug-tree-rb [...] rt.cpan.org> |
From: | "Wes Thompson" <wes [...] miaporta.com> |
The code below demonstrates that an error is thrown when accessing data
through an iterator returned by Tree::RB->iter('akey').
The error occurs when no key/value pairs are contained in the Tree::RB
object and the iterator is passed a key to start iterating on.
Preferred/expected behavior would be for the $iter->next() method call to
return undef.
ERROR STRING THROWN:
Can't call method "successor" on an undefined value at
/usr/local/share/perl/5.10.0/Tree/RB.pm line 227
EXAMPLE CODE THAT THAT PRODUCES ERROR:
use strict;
use warnings;
use Tree::RB;
my $tree;
my $iterbad;
my $itergood;
my $nodebad;
my $nodegood;
$tree = Tree::RB->new(); #a new empty Tree::RB object
#$tree->put( 'a','b' ); #uncomment this line and the error goes away
$itergood = $tree->iter(); #an iterator that works
$iterbad = $tree->iter('somekey'); #an iterator that throws error
$nodegood = $itergood->next(); #does not generate an error
$nodebad = $iterbad->next(); #<--generate an unexpected error
SUPERFICIAL FIX (Tree::RB line 227)
This made the error go away but it is probably the wrong fix. The better
fix is to fix the logic to prevent reaching line 227.
FROM:
my $next = $y->successor or return
TO:
my $next = $y->successor if $y or return