# Check if a single key or element exists given plain key or array index
##
my $self = $_[0]->_get_self;
- my $key = ($self->root->{filter_store_key} && $self->type eq TYPE_HASH) ? $self->root->{filter_store_key}->($_[1]) : $_[1];
+ my $key = $_[1];
- if (($self->type eq TYPE_ARRAY) && ($key =~ /^\d+$/)) { $key = pack($LONG_PACK, $key); }
my $md5 = $DIGEST_FUNC->($key);
##
sub store { (shift)->STORE( @_ ) }
sub get { (shift)->FETCH( @_ ) }
sub fetch { (shift)->FETCH( @_ ) }
-*delete = *DELETE;
-*exists = *EXISTS;
-*clear = *CLEAR;
+sub delete { (shift)->DELETE( @_ ) }
+sub exists { (shift)->EXISTS( @_ ) }
+sub clear { (shift)->CLEAR( @_ ) }
package DBM::Deep::_::Root;
my $self = shift->_get_self;
my ($key, $value) = @_;
- my $unpacked_key = $key;
+ my $orig = $key;
my $size = $self->FETCHSIZE;
my $numeric_idx;
$numeric_idx = 1;
if ( $key < 0 ) {
$key += $size;
- #XXX What to do here?
-# return unless $key >= 0;
+ if ( $key < 0 ) {
+ die( "Modification of non-creatable array value attempted, subscript $orig" );
+ }
}
$key = pack($DBM::Deep::LONG_PACK, $key);
my $rv = $self->SUPER::STORE( $key, $value );
- if ( $numeric_idx && $rv == 2 && $unpacked_key >= $size ) {
- $self->STORESIZE( $unpacked_key + 1 );
+ if ( $numeric_idx && $rv == 2 && $orig >= $size ) {
+ $self->STORESIZE( $orig + 1 );
}
return $rv;
}
+sub EXISTS {
+ my $self = $_[0]->_get_self;
+ my $key = $_[1];
+
+ if ( $key =~ /^-?\d+$/ ) {
+ if ( $key < 0 ) {
+ $key += $self->FETCHSIZE;
+ return unless $key >= 0;
+ }
+
+ $key = pack($DBM::Deep::LONG_PACK, $key);
+ }
+
+ return $self->SUPER::EXISTS( $key );
+}
+
sub FETCHSIZE {
##
# Return the length of the array
return $self->SUPER::STORE( $key, $value );
}
+sub EXISTS {
+ my $self = shift->_get_self;
+ my $key = ($self->root->{filter_store_key})
+ ? $self->root->{filter_store_key}->($_[0])
+ : $_[0];
+
+ return $self->SUPER::EXISTS( $key );
+}
+
sub FIRSTKEY {
##
# Locate and return first key (in no particular order)
# DBM::Deep Test
##
use strict;
-use Test::More tests => 94;
+use Test::More tests => 95;
use Test::Exception;
use_ok( 'DBM::Deep' );
is( $db->get(4), 'elem4.1' );
is( $db->fetch(4), 'elem4.1' );
+throws_ok {
+ $db->[-6] = 'whoops!';
+} qr/Modification of non-creatable array value attempted, subscript -6/, "Correct error thrown";
+
my $popped = $db->pop;
is( $db->length, 4, "... and we have four after popping" );
is( $db->[0], 'elem0', "0th element still there after popping" );
ok( $db->exists(1), "The 1st value exists" );
ok( !$db->exists(0), "The 0th value doesn't exists" );
ok( !$db->exists(22), "The 22nd value doesn't exists" );
-TODO: {
- local $TODO = "exists on negative values should work";
- ok( $db->exists(-1), "The -1st value does exists" );
-}
+ok( $db->exists(-1), "The -1st value does exists" );
ok( !$db->exists(-22), "The -22nd value doesn't exists" );
##