Data-WeakMap
view release on metacpan or search on metacpan
lib/Data/WeakMap/Key/Tie.pm view on Meta::CPAN
package Data::WeakMap::Key::Tie;
use 5.014;
use warnings FATAL => 'all';
use Scalar::Util 'weaken';
use Carp 'croak';
our $VERSION = "v0.0.4";
sub TIESCALAR {
my ($class, $weakmap, $key) = @_;
my $self = { weakmap => $weakmap, prop => "$key", key => $key };
weaken($self->{$_}) foreach qw/ weakmap key /;
bless $self, $class;
}
sub FETCH {
my ($self) = @_;
return $self->{key};
}
lib/Data/WeakMap/Tie.pm view on Meta::CPAN
package Data::WeakMap::Tie;
use 5.014;
use warnings FATAL => 'all';
use Scalar::Util 'weaken';
use Carp 'croak';
our $VERSION = "v0.0.4";
sub TIEHASH {
my ($class, $mbp) = @_;
my $self = [$mbp];
weaken($self->[0]);
bless $self, $class;
}
sub STORE {
my ($self, $key, $value) = @_;
croak 'key argument is not a ref' if ref $key eq '';
my $struct = ${ $_[0][0] };
weaken($struct->{tied_keys}{$key} = $key);
$struct->{values}{$key} = $value;
require Data::WeakMap::Key::Tie;
tie $struct->{tied_keys}{$key}, 'Data::WeakMap::Key::Tie', $struct->{dummy}, $key;
return $self;
}
sub FETCH {
my ($self, $key) = @_;
lib/Data/WeakMap/Tie.pm view on Meta::CPAN
my $z = keys %{ $struct->{tied_keys} };
return (each %{ $struct->{tied_keys} })[1];
}
sub NEXTKEY {
my ($self, $lastkey) = @_;
my $struct = ${ $_[0][0] };
weaken($struct->{tied_keys}{$lastkey});
return (each %{ $struct->{tied_keys} })[1];
}
sub SCALAR {
my ($self) = @_;
my $struct = ${ $_[0][0] };
return scalar %{ $struct->{tied_keys} };
t/00_weakmaps.t view on Meta::CPAN
ok((%$map) ? 0 : 1, '(%$map) in boolean context == 0, when empty');
my @input_keys = map { [$_ * 10] } (1 .. 10);
@$map{@input_keys} = (201 .. 210);
ok((keys %$map) ? 1 : 0, '(keys %$map) in boolean context == 1, when non-empty');
ok((%$map) ? 1 : 0, '(%$map) in boolean context == 1, when non-empty');
};
TODO: {
local $TODO = "can't do it now, because the each function will automatically unweaken the keys for some reason";
subtest 'attempt to do partial iteration with "each"' => sub {
plan tests => 5;
my ($map, $keys, $values) = create_map;
{
my @keys1 = ([ 10 ], [ 20 ], [ 30 ]);
$map->{$_} = 100 foreach @keys1;
is(keys(%$map), 3, 'map has 3 scoped lexical elements');
t/00_weakmaps.t view on Meta::CPAN
my $total_count = keys %$keys;
my $strong_count = 0;
foreach my $key (values %$keys) {
$strong_count++ unless isweak $key;
}
if ($strong_count == 0) {
pass "all $total_count keys in underlying object are still weak refs";
} else {
fail "$strong_count keys (out of $total_count) in the underlying object have been unweakened";
}
}
( run in 0.760 second using v1.01-cache-2.11-cpan-65fba6d93b7 )