| 1 | package Blessed::Merge; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | our $VERSION = '0.11'; | |||||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Scalar::Util qw/reftype/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Combine::Keys qw/combine_keys/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Tie::IxHash; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 6 | ▶ | my ($pkg, $args) = (shift, reftype $_[0] || "" eq 'HASH' ? $_[0] : {@_}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.5
Pod
documented
Branch: 1 branch
Condition100%2/2
builtin::reftype($_[0]) or
MC/DC100%1/1
builtin::reftype($_[0]) or
builtin::reftype($_[0])
Truth table100%2/2
A
A= builtin::reftype($_[0])
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 6 | my $self = bless $args, $pkg; | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | 6 | ▶ | $self->{$_} = $self->{$_} // 1 foreach (qw/same blessed/); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{$_} // 1
MC/DC100%1/1
$self->{$_} // 1
$self->{$_}
Truth table100%2/2
A
A= $self->{$_}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 6 | ▶ | $self->{$_} = $self->{$_} // 0 foreach(qw/unique_array unique_hash/); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{$_} // 0
MC/DC100%1/1
$self->{$_} // 0
$self->{$_}
Truth table100%2/2
A
A= $self->{$_}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 6 | $self->{itterator} = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | 6 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | sub merge { | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 10 | ▶ | my ($self, $base_bless, $new) = (shift, ref $_[0], shift); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
merge: called CC=12 SCAR=25.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 10 | tie my %isa, 'Tie::IxHash'; | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | 10 | $isa{$base_bless} = $new; | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | map { | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 10 | ▶ | if ( $self->{same} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 9 | ▶ | croak 'Attempting to merge two different *packages*' unless $base_bless eq ref $_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 6 | my $r = ref $_; | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | 6 | ▶ | $isa{$r} = $_ unless $r =~ m/HASH|ARRAY|SCALAR/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 14 | $new = $self->_merge($new, $_); | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | } @_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 9 | for my $f (keys %isa) { | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | 14 | ▶ | my $check = $isa{$f} or next; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 14 | ▶ | $_ eq $f and next or $check->isa($_) and delete $isa{$_} for keys %isa; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition62%5/8
$_ eq $f && (next) || $check->isa($_) && delete $isa{$_}
$_ eq $f and next
$check->isa($_) and delete $isa{$_}
MC/DC0%0/3
$_ eq $f && (next) || $check->isa($_) && delete $isa{$_}
$_ eq $f
$check->isa($_)
delete $isa{$_}
Truth table0%0/4
A || B and C
A= $_ eq $fB= $check->isa($_)C= delete $isa{$_}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 9 | ▶ | return $self->{blessed} ? scalar keys %isa == 1 ? bless $new, $base_bless : do { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 2 | my $class = sprintf "Blessed::Merge::__ANON__::%s", $self->{itterator}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 41 | 2 | eval sprintf('package %s; our @ISA = qw/%s/; 1;', $class, join ' ', keys %isa); | |||||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | return bless $new, $class; | |||||||||||||||||||||||||||||||||||||||||||||
| 43 | } : $new; | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | sub _merge { | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 74 | ▶ | my ($self, $new, $merger) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_merge: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 74 | ▶ | return $new unless defined $merger; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 66 | ▶ | my $new_ref = reftype($new) || ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
builtin::reftype($new) || ''
MC/DC100%1/1
builtin::reftype($new) || ''
builtin::reftype($new)
Truth table100%2/2
A
A= builtin::reftype($new)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 66 | ▶ | my $merger_ref = reftype($merger) || 'SCALAR'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
builtin::reftype($merger) || 'SCALAR'
MC/DC100%1/1
builtin::reftype($merger) || 'SCALAR'
builtin::reftype($merger)
Truth table100%2/2
A
A= builtin::reftype($merger)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | $merger_ref eq 'HASH' ? do { | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 36 | ▶ | $new = {} if ( $new_ref ne 'HASH' ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | return { | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | $self->{unique_hash} | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | ? $self->_unique_merge($merger_ref, $new, $merger) | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 36 | ▶ | : map +( $_ => $self->_merge( $new->{$_}, $merger->{$_} ) ), combine_keys($new, $merger) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 66 | ▶ | } : $merger_ref eq 'ARRAY' ? do { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | $new_ref eq 'ARRAY' ? do { | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 11 | ▶ | my $length = sub {$_[0] < $_[1] ? $_[1] : $_[0]}->(scalar @{$new}, scalar @{$merger}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | [ $self->{unique_array} | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | ? $self->_unique_merge($merger_ref, $new, $merger, $length) | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 11 | ▶ | : map { $self->_merge($new->[$_], $merger->[$_]) } 0 .. $length - 1 | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | ]; | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 13 | ▶ | } : [ map { $self->_merge('', $_ ) } @{ $merger } ]; # destroy da references | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } : $merger; | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | sub _unique_merge { | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 28 | ▶ | my ($s, $r, $n, $m, $l) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unique_merge: called CC=14 SCAR=26.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 28 | ▶ | ($r eq 'ARRAY') && do { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 9 | my (@z, %u, $x1, $x2); | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | 9 | ▶ | for (my $i = 0; $i < $l; $i++) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | my $c = grep { | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 24 | my ($x) = reftype(\$_); | |||||||||||||||||||||||||||||||||||||||||||||
| 76 | 48 | ▶ | $x eq 'SCALAR' ? !$_ || exists $u{$_} ? 1 : do { $u{$_} = 1; push @z, $_; } : 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
not $_ or exists $u{$_}
MC/DC100%2/2
not $_ or exists $u{$_}
not $_
exists $u{$_}
Truth table100%3/3
not(A) or B
A= not $_B= exists $u{$_}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } ($n->[$i], $m->[$i]); | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 24 | ▶ | do { ($x1, $x2) = (reftype($n->[$i]), reftype($m->[$i])); $c == 0 } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | ? $x1 eq $x2 | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | ? push @z, $s->_merge($n->[$i], $m->[$i]) | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | : push @z, $n->[$i], $m->[$i] | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | : $x1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | ? push @z, $n->[$i] | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | : push @z, $m->[$i] if $c != 2; | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 9 | return @z; | |||||||||||||||||||||||||||||||||||||||||||||
| 87 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 19 | my %z = %{ $n }; | |||||||||||||||||||||||||||||||||||||||||||||
| 89 | map { | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 31 | ▶ | my $x = reftype($m->{$_}) || 'SCALAR'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
builtin::reftype($m->{$_}) || 'SCALAR'
MC/DC100%1/1
builtin::reftype($m->{$_}) || 'SCALAR'
builtin::reftype($m->{$_})
Truth table100%2/2
A
A= builtin::reftype($m->{$_})
| |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | exists $z{$_} ? $x ne 'SCALAR' && $x eq reftype($z{$_}) | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 31 | ▶ | ? do { $z{$_} = $s->_merge($z{$_}, $m->{$_}) } : '*\o/*' : do { $z{$_} = $m->{$_} } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$x ne "SCALAR" and $x eq builtin::reftype($z{$_})
MC/DC100%2/2
$x ne "SCALAR" and $x eq builtin::reftype($z{$_})
$x ne "SCALAR"
$x eq builtin::reftype($z{$_})
Truth table100%3/3
A and B
A= $x ne "SCALAR"B= $x eq builtin::reftype($z{$_})
| |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 19 | } keys %{ $m }; | |||||||||||||||||||||||||||||||||||||||||||||
| 94 | 19 | return %z; | |||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||