| 1 | package EO::WeakArray; | ||||||||||||
| 2 | |||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 2 | ▶ | use EO::Array; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use base qw( EO::Array ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use Scalar::Util qw( weaken ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 2 | our $VERSION = 0.96; | |||||||||||
| 11 | |||||||||||||
| 12 | 20 | ▶ | sub splice { | ||||||||||
|
Subroutine
splice: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||
| 13 | 20 | my $self = shift; | |||||||||||
| 14 | 20 | my $result = $self->SUPER::splice(@_); | |||||||||||
| 15 | 20 | my $offset = shift; | |||||||||||
| 16 | 20 | ▶ | my $length = shift || 0; | ||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 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 | 20 | ▶ | if ($offset >= 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 22 | 19 | for ($offset..$offset + $length) { | |||||||||||
| 23 | ## can't weaken non-references :-) | ||||||||||||
| 24 | 22 | ▶ | weaken( $self->element->[ $_ ] ) if ref( $self->element->[ $_ ] ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 25 | } | ||||||||||||
| 26 | } | ||||||||||||
| 27 | 20 | return $result; | |||||||||||
| 28 | } | ||||||||||||
| 29 | |||||||||||||
| 30 | |||||||||||||
| 31 | 2 | ▶ | sub at { | ||||||||||
|
Subroutine
at: called CC=3 SCAR=17.3
Pod
documented
| |||||||||||||
| 32 | 2 | my $self = shift; | |||||||||||
| 33 | 2 | ▶ | if (@_ > 1) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 34 | ## its a set, so we need to weaken these things. | ||||||||||||
| 35 | 0 | my $where = shift; | |||||||||||
| 36 | 0 | my $what = shift; | |||||||||||
| 37 | 0 | my $result = $self->SUPER::at( $where, $what ); | |||||||||||
| 38 | 0 | ▶ | weaken( $self->element->[ $where ] ) if ref($self->element->[ $where ]); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 39 | 0 | return $result; | |||||||||||
| 40 | } else { | ||||||||||||
| 41 | ## its a get, no weakening needed | ||||||||||||
| 42 | 2 | return $self->SUPER::at( @_ ); | |||||||||||
| 43 | } | ||||||||||||
| 44 | } | ||||||||||||
| 45 | |||||||||||||
| 46 | 2 | 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 <jduncan@fotango.com> | ||||||||||||
| 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 | ||||||||||||