blib/lib/Array/Assign.pm

filter: Statementstmt 92.7% 51 / 55 Branchbran 68.7% 11 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 9 / 9 totaltotal 90.3% 84 / 93 CC 9 SCAR 7.4
CC
9
Cov
87%
new
3.1
arry_extract_s
3.1
arry_assign_s
3.0
CRAP 2.1
↑ Summary
1 package Array::Assign;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our @EXPORT = qw(
6 arry_assign_i arry_assign_s
7 arry_extract_i arry_extract_s
8 );
9  
10 2 our $IDX_MAX = 1000;
11 2 our $VERSION = 0.01;
12  
13 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod documented
14 2 my ($cls,$idx_map) = @_;
15 2 my $self = {};
16 2 ▶ if(ref $idx_map eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $idx_map eq 'HASH') { } 0 2 2
17 0 %$self = %$idx_map;
18 } else {
19 2 my @idx_arry;
20 2 ▶ if(ref $idx_map eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $idx_map eq 'ARRAY') { } 0 2 2
21 0 @idx_arry = @$idx_map;
22 } else {
23 2 @idx_arry = @_[1..$#_];
24 }
25 2 @{$self}{@idx_arry} = (0..$#idx_arry);
26 }
27 2 bless $self, $cls;
28 2 return $self;
29 }
30  
31 3 ▶sub assign_s {
Subroutine assign_s: called CC=1 SCAR=0.0
Pod documented
32 3 my ($self,$target,%fields) = @_;
33 3 &arry_assign_s($target, $self, %fields);
34 }
35  
36 2 ▶sub assign_i {
Subroutine assign_i: called CC=1 SCAR=0.0
Pod documented
37 2 shift;
38 2 goto &arry_assign_i;
39 }
40  
41 2 ▶sub extract_s {
Subroutine extract_s: called CC=1 SCAR=0.0
Pod documented
42 2 my ($self,$source,%fields) = @_;
43 2 &arry_extract_s($source, $self, %fields);
44 }
45  
46 2 ▶sub extract_i {
Subroutine extract_i: called CC=1 SCAR=0.0
Pod documented
47 2 shift;
48 2 goto &arry_extract_i;
49 }
50  
51  
52 5 ▶sub arry_assign_s(\@$%) {
Subroutine arry_assign_s: called CC=3 SCAR=11.0
Pod documented
53 5 my ($target,$mapping,%assignments) = @_;
54 5 ▶ while (my ($name,$value) = each %assignments) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %assignments) 11 5 16
55 11 my $idx = $mapping->{$name};
56 11 ▶ die("Unknown name '$name'") unless defined $idx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $idx 0 11 11
57 11 $target->[$idx] = $value;
58 }
59 5 $target;
60 }
61  
62 sub arry_assign_i(\@%)
63 4 ▶{
Subroutine arry_assign_i: called CC=1 SCAR=0.0
Pod documented
64 4 my ($target,%mappings) = @_;
65 4 _idx_sanity_check(%mappings);
66
67 4 @{$target}[keys %mappings] = values %mappings;
68 }
69  
70  
71 sub arry_extract_i(\@%)
72 4 ▶{
Subroutine arry_extract_i: called CC=2 SCAR=6.9
Pod documented
73 4 my ($source,%targets) = @_;
74 4 _idx_sanity_check(%targets);
75 4 ▶ while ( my ($idx,$ref) = each %targets) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %targets) 8 4 12
76 8 $$ref = $source->[$idx];
77 }
78 }
79  
80 4 ▶sub arry_extract_s(\@$%) {
Subroutine arry_extract_s: called CC=3 SCAR=11.2
Pod documented
81 4 my ($source,$mappings,%targets) = @_;
82 4 ▶ while (my ($name,$ref) = each %targets) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %targets) 9 4 13
83 9 my $idx = $mappings->{$name};
84 9 ▶ if(!defined $idx) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $idx) 0 9 9
85 0 die("unknown parameter '$name'");
86 }
87 9 $$ref = $source->[$idx];
88 }
89 }
90  
91 8 ▶sub _idx_sanity_check {
Subroutine _idx_sanity_check: called CC=2 SCAR=8.1
92 8 my %mappings = @_;
93 8 ▶ if(scalar (grep $_ > $IDX_MAX, keys %mappings) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep(($_ > $Array::Assign::IDX_MAX), keys %mappings) > 0) 0 8 8
94 0 die("Abnormally large index found. ".
95 "Bump up \$IDX_MAX if this is not a mistake");
96 }
97 }
98  
99 __END__
↑ Summary