| 1 | package Hash::AutoHash::Record; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 13 | our $VERSION='1.17_01'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 13 | $VERSION=eval $VERSION; # I think this is the accepted idiom.. | |||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | ################################################################################# | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # Author: Nat Goodman | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # Created: 09-03-05 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # $Id: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # Flat and hierarchical record structures of the type encountered in Data::Pipeline | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | ################################################################################# | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 13 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 13 | ▶ | use Hash::AutoHash; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 13 | ▶ | use base qw(Hash::AutoHash); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 13 | our @NORMAL_EXPORT_OK=@Hash::AutoHash::EXPORT_OK; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 13 | my $helper_class=__PACKAGE__.'::helper'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 13 | our @EXPORT_OK=$helper_class->EXPORT_OK; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 13 | our @SUBCLASS_EXPORT_OK=$helper_class->SUBCLASS_EXPORT_OK; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | ################################################################################# | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # helper package exists to avoid polluting Hash::AutoHash::Args namespace with | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # subs that would mask accessor/mutator AUTOLOADs | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # functions herein (except _new) are exportable by Hash::AutoHash::Args | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | ################################################################################# | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | package Hash::AutoHash::Record::helper; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 13 | our $VERSION=$Hash::AutoHash::Record::VERSION; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 13 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 13 | ▶ | our @ISA=qw(Hash::AutoHash::helper); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 13 | ▶ | use Hash::AutoHash qw(autohash_tie); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | sub _new { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 800 | ▶ | my($helper_class,$class,@args)=@_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 800 | my $self=autohash_tie Hash::AutoHash::Record::tie,@args; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 800 | bless $self,$class; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # Override autohash_clear to allow clearing of specific keys | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | sub autohash_clear { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 5 | ▶ | my $record=shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
autohash_clear: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 5 | tied(%$record)->CLEAR(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | ################################################################################# | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # Tied hash which implements Hash::AutoHash::Record | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | ################################################################################# | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | package Hash::AutoHash::Record::tie; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 13 | our $VERSION=$Hash::AutoHash::Record::VERSION; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 13 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 13 | ▶ | use Tie::Hash; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 13 | ▶ | use Scalar::Util qw(reftype); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 13 | ▶ | use List::MoreUtils qw(uniq); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 13 | ▶ | use Storable qw(dclone); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 13 | ▶ | use Hash::AutoHash qw(autohash_alias); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 13 | ▶ | use Hash::AutoHash::AVPairsSingle; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 13 | ▶ | use Hash::AutoHash::AVPairsMulti; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 13 | our @ISA=qw(Tie::ExtraHash); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 13 | my $i=0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 13 | ▶ | use constant STORAGE=>$i++; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 13 | ▶ | use constant DEFAULTS=>$i++; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 13 | ▶ | use constant DEFAULT_TYPE_SCALAR=>$i++; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 13 | ▶ | use constant DEFAULT_TYPE_ARRAY=>$i++; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 13 | ▶ | use constant DEFAULT_TYPE_HASH=>$i++; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 13 | ▶ | use constant UNIQUE=>$i++; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 13 | ▶ | use constant FILTER=>$i++; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | # use constant FIELDS=>$i++; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # use constant TYPES=>$i++; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | # # undef means no type conversion | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # our $default_type_scalar; # no type conversion | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | # our $default_type_array; # no type conversion | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | # our $default_type_hash='Hash::AutoHash'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | # our $default_type_refhash='Hash::AutoHash::AVPairsMulti'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | sub TIEHASH { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 800 | ▶ | my($class,@hash)=@_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
TIEHASH: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 800 | my $self=bless [],$class; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | # use initial values (possibly flattened) as defaults | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 800 | my $defaults=$self->defaults(_flatten(@hash)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 800 | ▶ | $self->[STORAGE]=$defaults? dclone($defaults): {}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 800 | $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | sub FETCH { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 24996 | ▶ | my($self,$key)=@_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 24996 | my $storage=$self->[STORAGE]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 24996 | my $value=$storage->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 24996 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | # NG 09-10-12: line below was holdover from MultiValued. Not correct here | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | # return () unless defined $value; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 4529 | ▶ | return @$value if 'ARRAY' eq reftype($value); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 2117 | ▶ | return %$value if 'HASH' eq reftype($value); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 571 | return ($value); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 20467 | $value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | # FUTURE possibility: check whether hash locked & key exists | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | sub STORE { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 1691 | ▶ | my($self,$key,@values)=@_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 1691 | my $storage=$self->[STORAGE]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 1691 | $self->_store($storage,$key,@values); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 1687 | $self->FETCH($key); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | sub CLEAR { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 214 | ▶ | my($self,@keys)=@_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
CLEAR: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 214 | my $defaults=$self->defaults; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 214 | ▶ | unless (@keys) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 211 | ▶ | $self->[STORAGE]=$defaults? dclone($defaults): {} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | } else { # clear specific keys | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 3 | my $storage=$self->[STORAGE]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 3 | my $defaults=$self->[DEFAULTS]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 3 | for my $key (@keys) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 4 | my $default=$defaults->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 4 | my $new=$self->_convert_initial_value($default); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 4 | $storage->{$key}=$default; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | }} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 214 | my $unique=$self->unique; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 214 | ▶ | $self->_unique($unique) if $unique; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 13 | *clear=\&CLEAR; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | # default values. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | # can be set from initial values suppled to TIEHASH, or explicitly | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | sub defaults { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 1120 | ▶ | my $self=shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 1120 | my $defaults; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 1120 | ▶ | if (@_) { # set new value | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 553 | ▶ | my @hash=(@_==1 && 'ARRAY' eq ref $_[0])? @{$_[0]}: (@_==1 && 'HASH' eq ref $_[0])? %{$_[0]}: | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition83%5/6
@_ == 1 and "ARRAY" eq ref $_[0]
@_ == 1 and "HASH" eq ref $_[0]
MC/DC100%2/2
@_ == 1 and "ARRAY" eq ref $_[0]
@_ == 1
"ARRAY" eq ref $_[0]
MC/DC50%1/2
@_ == 1 and "HASH" eq ref $_[0]
@_ == 1
"HASH" eq ref $_[0]
Truth table100%3/3
A and B
A= @_ == 1B= "ARRAY" eq ref $_[0]
Truth table66%2/3
A and B
A= @_ == 1B= "HASH" eq ref $_[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 553 | $defaults={}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 553 | ▶ | while (@hash>1) { # store initial values | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 1622 | my($key,$value)=splice @hash,0,2; # shift 1st two elements | |||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 1622 | $self->_store($defaults,$key,$value); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 553 | $self->[DEFAULTS]=$defaults; # set object attribute | |||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | } else { # get defaults from object | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 567 | $defaults=$self->[DEFAULTS]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 1120 | ▶ | wantarray? %{$defaults || {}}: $defaults; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | # forcibly assign value or set to undef | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | sub force { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 250 | ▶ | my($self,$key)=splice @_,0,2; # shift 1st 2 elements | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
force: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 250 | my $storage=$self->[STORAGE]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 250 | $storage->{$key}=undef; # once field is undef, _store can do the rest | |||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 250 | ▶ | $self->_store($storage,$key,@_) if @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 250 | $self->FETCH($key); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | # code adapted from Hash::AutoHash::MultiValued | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | sub unique { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 3098 | ▶ | my $self=shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unique: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 3098 | ▶ | return $self->[UNIQUE] unless @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 586 | my $unique=$self->[UNIQUE]=shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 586 | ▶ | $unique=$self->[UNIQUE]=sub {$_[0] eq $_[1]} if $unique && 'CODE' ne ref $unique; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
Condition100%3/3
$unique and "CODE" ne ref $unique
MC/DC100%2/2
$unique and "CODE" ne ref $unique
$unique
"CODE" ne ref $unique
Truth table100%3/3
A and B
A= $uniqueB= "CODE" ne ref $unique
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 586 | ▶ | $self->_unique($unique) if $unique; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 586 | $unique; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | sub _unique { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 495 | ▶ | my($self,$unique)=@_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unique: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 495 | my $storage=$self->[STORAGE]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 495 | ▶ | my @values=grep {defined $_ && 'ARRAY' eq reftype($_)} values %$storage; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 495 | for my $values (@values) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 805 | ▶ | next unless @$values; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | # leave 1st value in @$values. put rest in @new_values | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 225 | my @new_values=splice(@$values,1); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 225 | my($a,$b); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 225 | for $a (@new_values) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 79 | ▶ | push(@$values,$a) unless grep {$b=$_; &$unique($a,$b)} @$values; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | }} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | # code adapted from Hash::AutoHash::MultiValued | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | sub filter { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 76 | ▶ | my $self=shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
filter: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 76 | ▶ | my $filter=@_? $self->[FILTER]=shift: $self->[FILTER]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 76 | ▶ | if ($filter) { # apply to existing values -- ARRAYs only | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 52 | ▶ | $filter=$self->[FILTER]=\&uniq unless 'CODE' eq ref $filter; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 52 | my $storage=$self->[STORAGE]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 52 | ▶ | my @values=grep {defined $_ && 'ARRAY' eq reftype($_)} values %$storage; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 52 | map {@$_=&$filter(@$_)} @values; # updates each list in-place | |||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 76 | $filter; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | # sub _default_type_scalar {shift->_default_type('scalar',@_);} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | # sub _default_type_array {shift->_default_type('array',@_);} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | # sub _default_type_hash {shift->_default_type('hash',@_);} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | # sub _default_type_refhash {shift->_default_type('refhash',@_);} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | # sub _default_type { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | # my($self,$type)=splice @_,0,2; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | # my $default; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | # if (@_) { # set new value in object | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | # $default=$self->[uc "default_type_$type"]=$_[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | # } else { # get defaults from object if possible, else from class | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | # $default=$self->[uc "default_type_$type"]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | # unless (defined $default) { # now look in class | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | # my $class=ref $self; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | # no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | # my $class_var=$class."::default_type_$type"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | # $default=${$class_var}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | # }} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | # $default; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | # } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | # sub _convert_initial_value { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | # my($self,$value)=@_; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | # my $type= | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | # (!ref $value)? 'scalar': | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | # ('ARRAY' eq ref $value)? 'array': | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | # ('HASH' eq ref $value)? 'hash': | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | # ('REF' eq ref $value && 'HASH' eq ref $$value)? 'refhash': | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | # undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | # my $class=$self->_default_type($type) if $type; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | # $value=$class? new $class $value: $value; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | # $value; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | # } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | sub _convert_initial_value { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 1224 | ▶ | my($self,$value)=@_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_convert_initial_value: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 1224 | ▶ | if ('HASH' eq ref $value) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | # attribute-single-value pair if no refs | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | # else attribute-multi-value pair if only refs are ARRAY | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | # else use as is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 311 | my @values=values %$value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | # CAUTION: doing grep below w/o map seems to stringify refs to things like ARRAY(0x1163510) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 311 | my @refs=grep {$_} map {ref $_} @values; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 311 | ▶ | if (!@refs) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 263 | $value=new Hash::AutoHash::AVPairsSingle $value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | } elsif (!grep !/^ARRAY$/,@refs) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 21 | $value=new Hash::AutoHash::AVPairsMulti $value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | ▶ | } elsif ('REF' eq ref $value && 'HASH' eq ref $$value) { | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
"REF" eq ref $value and "HASH" eq ref $$value
MC/DC100%2/2
"REF" eq ref $value and "HASH" eq ref $$value
"REF" eq ref $value
"HASH" eq ref $$value
Truth table100%3/3
A and B
A= "REF" eq ref $valueB= "HASH" eq ref $$value
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 247 | my @values=values %$$value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | # CAUTION: doing grep below w/o map seems to stringify refs to things like ARRAY(0x1163510) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 247 | my @refs=grep {$_} map {ref $_} @values; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 247 | ▶ | if (!grep !/^ARRAY$/,@refs) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 247 | $value=new Hash::AutoHash::AVPairsMulti $$value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | }} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 1224 | $value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | # logic: check type of old value and new value | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | # old undef. anything goes. new value replaces old w/ initial value conversion | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | # old scalar && new scalar. new value replaces old | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | # old ARRAY && new any value. multi-valued field. new pushed onto old & possibly uniqued | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | # old Hash::AutoHash. new must be HASH or ARRAY or list of key=>value pairs. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | # new elements set in old using method notation | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | # old anything else. new value replaces old | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | sub _store { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 6056 | ▶ | my($self,$storage,$key,@new)=@_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_store: called CC=17 SCAR=28.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 6056 | ▶ | return unless @new; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 4853 | my $old=$storage->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 4853 | ▶ | if (!defined $old) { # old undef. anything goes. new replaces old. | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 2495 | ▶ | if (@new==1 && 'ARRAY' eq ref $new[0]) { # new multi-valued field | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@new == 1 and "ARRAY" eq ref $new[0]
MC/DC50%1/2
@new == 1 and "ARRAY" eq ref $new[0]
@new == 1
"ARRAY" eq ref $new[0]
Truth table66%2/3
A and B
A= @new == 1B= "ARRAY" eq ref $new[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 1292 | $storage->{$key}=[]; # initialize to empty ARRAY. recursion will do the rest | |||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 1203 | my $new1=shift @new; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 1203 | $new1=$self->_convert_initial_value($new1); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 1203 | $storage->{$key}=$new1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 2495 | $self->_store($storage,$key,@new); # recurse | |||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | } elsif (!ref $old) { # old scalar. new replaces old. must be scalar | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 17 | my $new=shift @new; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 17 | $new=$self->_convert_initial_value($new); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 17 | ▶ | confess "Trying to store multiple values in single-valued field $key" if @new; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 16 | ▶ | confess "Trying to store reference in single-valued field $key" if ref $new; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 16 | $storage->{$key}=$new; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | } elsif ('ARRAY' eq ref $old) { # old ARRAY. push new onto old. must be scalar | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | # $self->_store_multi($old,@new); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | # code adapted from Hash::AutoHash::MultiValued | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 2296 | ▶ | @new=@{$new[0]} if @new==1 && 'ARRAY' eq ref $new[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@new == 1 and "ARRAY" eq ref $new[0]
MC/DC100%2/2
@new == 1 and "ARRAY" eq ref $new[0]
@new == 1
"ARRAY" eq ref $new[0]
Truth table100%3/3
A and B
A= @new == 1B= "ARRAY" eq ref $new[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 2296 | ▶ | confess "Trying to store reference in multi-valued field $key" if grep {ref($_)} @new; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 2295 | ▶ | if (my $unique=$self->unique) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 586 | my($a,$b); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 586 | for $a (@new) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 251 | ▶ | push(@$old,$a) unless grep {$b=$_; &$unique($a,$b)} @$old; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | }} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 1709 | push(@$old,@new); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | } elsif (UNIVERSAL::isa($old,'Hash::AutoHash')) { # old Hash::AutoHash | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 42 | @new=_flatten(@new); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 42 | ▶ | while (@new>1) { # store initial values | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 52 | my($key,$value)=splice @new,0,2; # shift 1st two elements | |||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 52 | $old->$key($value); # store using hash notation | |||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | } else { # old anything else. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 3 | $storage->{$key}=$new[0]; # new replaces old | |||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | # # store into multi-valued field, or store multi-value into undef field | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | # sub _store_multi { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | # my($self,$old,@new)=@_; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | # # code adapted from Hash::AutoHash::MultiValued | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | # @new=@{$new[0]} if @new==1 && 'ARRAY' eq ref $new[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | # if (my $unique=$self->unique) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | # my($a,$b); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | # for $a (@new) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | # push(@$old,$a) unless grep {$b=$_; &$unique($a,$b)} @$old; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | # }} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | # push(@$old,@new); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | # } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | # } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | sub _flatten { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 842 | ▶ | if (@_==1) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_flatten: called CC=4 SCAR=13.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 595 | ▶ | return ('ARRAY' eq ref $_[0])? @{$_[0]}: ('HASH' eq ref $_[0])? %{$_[0]}: @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 247 | @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 13 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||