File Coverage

blib/lib/EO/WeakArray.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package EO::WeakArray;
2              
3 2     2   30007 use strict;
  2         4  
  2         80  
4 2     2   10 use warnings;
  2         114  
  2         60  
5              
6 2     2   1273 use EO::Array;
  0            
  0            
7             use base qw( EO::Array );
8             use Scalar::Util qw( weaken );
9              
10             our $VERSION = 0.96;
11              
12             sub splice {
13             my $self = shift;
14             my $result = $self->SUPER::splice(@_);
15             my $offset = shift;
16             my $length = shift || 0;
17             ## in the case that we have an offset we can deal with then we are going
18             ## to start weakening the things that we can. We're not going to be
19             ## stupid and walk the entire thing either, no, we'll be smart and only
20             ## touch the things that have been touched.
21             if ($offset >= 0) {
22             for ($offset..$offset + $length) {
23             ## can't weaken non-references :-)
24             weaken( $self->element->[ $_ ] ) if ref( $self->element->[ $_ ] );
25             }
26             }
27             return $result;
28             }
29              
30              
31             sub at {
32             my $self = shift;
33             if (@_ > 1) {
34             ## its a set, so we need to weaken these things.
35             my $where = shift;
36             my $what = shift;
37             my $result = $self->SUPER::at( $where, $what );
38             weaken( $self->element->[ $where ] ) if ref($self->element->[ $where ]);
39             return $result;
40             } else {
41             ## its a get, no weakening needed
42             return $self->SUPER::at( @_ );
43             }
44             }
45              
46             1;
47              
48             =head1 NAME
49              
50             EO::WeakArray - arrays where all references contained are weak
51              
52             =head1 SYNOPSIS
53              
54             use EO::WeakArray;
55             my $thing = {};
56             my $array = EO::WeakArray->new;
57             $array->push( $obj );
58             $obj = undef;
59              
60             if ( $array->at( 0 ) == undef ) {
61             print "ok\n";
62             }
63              
64             =head1 DESCRIPTION
65              
66             A WeakArray is similar to a normal array only its contents are not reference
67             counted. Thus, if something destroys the contents from the outside world
68             then it disappears from inside the array as well.
69              
70             =head1 METHODS
71              
72             WeakArrays provide no methods beyond those provided by an array.
73              
74             =head1 AUTHOR
75              
76             James A. Duncan
77              
78             =head1 COPYRIGHT
79              
80             Copyright 2004 Fotango Ltd. All Rights Reserved.
81              
82             This module is released under the same terms as Perl itself.
83              
84             =cut