From: Rob Kinyon Date: Wed, 6 Jan 2010 03:21:23 +0000 (-0500) Subject: (RT #40782) '0' as a hashkey wasn't iterated over correctly. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c11b7bfb5d0dc7efdd045e1118887aaa7d2faf16;p=dbsrgits%2FDBM-Deep.git (RT #40782) '0' as a hashkey wasn't iterated over correctly. --- diff --git a/Changes b/Changes index 57578f3..79a4c7d 100644 --- a/Changes +++ b/Changes @@ -5,6 +5,7 @@ Revision history for DBM::Deep. - Fix deep recursion errors (RT#53575) - Avoid leaving temp files lying around (RT#32462) - (RT #48031) Fixed bug with localized $, (Thanks, SPROUT!) + - (RT #40782) Fixed bug when handling a key of '0' (Thanks Sterling!) 1.0014 Jun 13 23:15:00 2008 EST - (This version is compatible with 1.0013) diff --git a/lib/DBM/Deep/Engine.pm b/lib/DBM/Deep/Engine.pm index 8f6a8f8..1193bd8 100644 --- a/lib/DBM/Deep/Engine.pm +++ b/lib/DBM/Deep/Engine.pm @@ -556,7 +556,7 @@ sub get_next_key { my ($obj, $prev_key) = @_; # XXX Need to add logic about resetting the iterator if any key in the reference has changed - unless ( $prev_key ) { + unless ( defined $prev_key ) { $obj->{iterator} = DBM::Deep::Iterator->new({ base_offset => $obj->_base_offset, engine => $self, diff --git a/lib/DBM/Deep/Hash.pm b/lib/DBM/Deep/Hash.pm index 6b43d96..f09f674 100644 --- a/lib/DBM/Deep/Hash.pm +++ b/lib/DBM/Deep/Hash.pm @@ -116,7 +116,7 @@ sub _copy_node { my ($db_temp) = @_; my $key = $self->first_key(); - while ($key) { + while (defined $key) { my $value = $self->get($key); $self->_copy_value( \$db_temp->{$key}, $value ); $key = $self->next_key($key); diff --git a/t/02_hash.t b/t/02_hash.t index ee3b409..92ff9f3 100644 --- a/t/02_hash.t +++ b/t/02_hash.t @@ -27,15 +27,15 @@ is( $db->get("key2"), undef, "get() works with put()" ); is( $db->fetch("key2"), undef, "... fetch() works with put()" ); is( $db->{key2}, undef, "... and hash-access also works" ); -$db->store( "key3", "value3" ); -is( $db->get("key3"), "value3", "get() works with store()" ); -is( $db->fetch("key3"), "value3", "... fetch() works with put()" ); -is( $db->{key3}, 'value3', "... and hash-access also works" ); +$db->store( "0", "value3" ); +is( $db->get("0"), "value3", "get() works with store()" ); +is( $db->fetch("0"), "value3", "... fetch() works with put()" ); +is( $db->{0}, 'value3', "... and hash-access also works" ); # Verify that the keyval pairs are still correct. is( $db->{key1}, "value1", "Key1 is still correct" ); is( $db->{key2}, undef, "Key2 is still correct" ); -is( $db->{key3}, 'value3', "Key3 is still correct" ); +is( $db->{0}, 'value3', "Key3 is still correct" ); ok( $db->exists("key1"), "exists() function works" ); ok( exists $db->{key2}, "exists() works against tied hash" ); @@ -68,25 +68,25 @@ while ( my ($key, $value) = each %$db ) { is( $temphash->{key1}, 'value1', "First key copied successfully using tied interface" ); is( $temphash->{key2}, undef, "Second key copied successfully" ); -is( $temphash->{key3}, 'value3', "Third key copied successfully" ); +is( $temphash->{0}, 'value3', "Third key copied successfully" ); $temphash = {}; my $key = $db->first_key(); -while ($key) { +while (defined $key) { $temphash->{$key} = $db->get($key); $key = $db->next_key($key); } is( $temphash->{key1}, 'value1', "First key copied successfully using OO interface" ); is( $temphash->{key2}, undef, "Second key copied successfully" ); -is( $temphash->{key3}, 'value3', "Third key copied successfully" ); +is( $temphash->{0}, 'value3', "Third key copied successfully" ); ## # delete keys ## is( delete $db->{key2}, undef, "delete through tied inteface works" ); is( $db->delete("key1"), 'value1', "delete through OO inteface works" ); -is( $db->{key3}, 'value3', "The other key is still there" ); +is( $db->{0}, 'value3', "The other key is still there" ); ok( !exists $db->{key1}, "key1 doesn't exist" ); ok( !exists $db->{key2}, "key2 doesn't exist" );