CLI/Hash.pm

filter: Statementstmt 14.0% 14 / 100 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 40.0% 4 / 10 Podpod 0.0% 0 / 6 totaltotal 11.0% 18 / 163 CC 27 SCAR 42.4
CC
27
Cov
10%
parse
110.0
extract
110.0
new
42.0
CRAP 69.6
« CLI/Array.pm ↑ Summary CLI/MixedArray.pm »
1 package CLI::Hash;
2 1 ▶use CLI::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 @ISA = ("CLI::Base");
4  
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use CLI qw( SSTRING parse_string hashmatch );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 0 ▶sub new {
Subroutine new: not called CC=6 SCAR=37.4
Pod undocumented
10 0 my $proto = shift;
11 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
12  
13 0 my $name = shift;
14 0 my @vars = @_;
15 0 ▶ if (@vars<1) { # We need some values to play with
Branch: 1 branch
BranchTrueFalseTotal
if (@vars < 1) 0 0 0
16 0 carp "CLI::Hash->new: No variables to add\n";
17 0 return;
18 }
19  
20 # Is the last value a hash reference?
21 0 my $hash = pop @vars;
22 0 ▶ if (!(ref($hash) eq 'HASH')) { # No then leave @vars how it was
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $hash eq 'HASH') { } 0 0 0
23 0 push @vars, $hash;
24 0 $hash = undef;
25 } else {
26 0 ▶ if (@vars<1) { # We need some values to play with
Branch: 1 branch
BranchTrueFalseTotal
if (@vars < 1) 0 0 0
27 0 carp "CLI::Hash->new: No variables to add\n";
28 0 return;
29 }
30 }
31  
32 0 my $self = {
33 NAME => $name,
34 TYPE => undef,
35 VALUE => undef,
36 FUNC => undef,
37 MIN => undef,
38 MAX => undef,
39 HASH => {},
40 VARS => [@vars]
41 };
42 0 bless ($self, $class);
43  
44 0 ▶ $self->function($hash->{function}) if (defined $hash->{function});
Branch: 1 branch
BranchTrueFalseTotal
if defined $hash->{"function"} 0 0 0
45  
46 0 return $self;
47 }
48  
49 0 ▶sub hash {
Subroutine hash: not called CC=1 SCAR=6.9
Pod undocumented
50 0 my $self = shift;
51 0 return $self->{HASH};
52 }
53  
54 0 ▶sub vars {
Subroutine vars: not called CC=2 SCAR=17.9
Pod undocumented
55 0 my $self = shift;
56 0 ▶ if (@_) { $self->{VARS} = (@_) }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
57 0 return @{$self->{VARS}};
58 }
59  
60 0 ▶sub add {
Subroutine add: not called CC=3 SCAR=24.8
Pod undocumented
61 0 my $self = shift;
62 0 my $name = shift;
63 0 my @values = @_;
64 0 ▶ if (@values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) { } 0 0 0
65 0 my @newvalues = ();
66 0 foreach ($self->vars()) {
67 0 push @newvalues, shift @values;
68 }
69 0 $self->hash->{$name} = [@newvalues];
70 } else {
71 0 carp "CLI::Hash->add: No values to add\n";
72 0 return;
73 }
74 }
75  
76 0 ▶sub parse {
Subroutine parse: not called CC=10 SCAR=47.0
Pod undocumented
77 0 my $self = shift;
78 0 my $string = shift;
79  
80 0 ▶ if (!defined $string) { # No argument, print name of last value used
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $string) { } 0 0 0
81 0 ▶ if (defined $self->value()) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->value) { } 0 0 0
82 0 print ' ', $self->value(), "\n";
83 } else {
84 0 print "No value set\n";
85 }
86 } else {
87 0 my $name = parse_string(SSTRING, $string);
88  
89 0 ▶ if (!defined $string) { # no values, extract values from the hash
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $string) { } 0 0 0
90 0 my @matches = $self->extract($name,0);
91 0 ▶ if (!defined $matches[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $matches[0]) { } 0 0 0
elsif (@matches > 1) { } 0 0 0
92 0 print "Unknown element $name\n";
93 } elsif (@matches>1) {
94 0 print "\"$name\" matches:\n\n";
95 0 foreach (@matches) {
96 0 print " $_\n";
97 }
98 0 print "\n";
99 }
100 } else { # New element
101 0 my @values = ();
102 0 foreach my $var ($self->vars()) {
103 0 ▶ if (defined $string) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $string) { } 0 0 0
104 0 my $val = parse_string($var->type(), $string);
105 0 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 0 0 0
106 0 push @values, $val;
107 } else {
108 0 carp "Bad value \"$string\"\n";
109 0 return;
110 }
111 } else {
112 0 push @values, $var->value(); # Save the current value
113 }
114 }
115 0 $self->add($name, @values);
116 }
117 }
118 }
119  
120 0 ▶sub extract {
Subroutine extract: not called CC=10 SCAR=47.0
Pod undocumented
121 0 my $self = shift;
122 0 my $name = shift;
123 0 my $quiet = shift;
124 0 ▶ $quiet=0 if (!defined $quiet);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $quiet 0 0 0
125  
126 0 my $hash = $self->hash();
127  
128 0 my @values = ();
129 0 my @matches = ();
130 0 my $hashmatch = hashmatch($name, $hash, @matches);
131 0 ▶ if (defined $hashmatch) { # Did we find a unique match
Branch: 2 branches
BranchTrueFalseTotal
if (defined $hashmatch) { } 0 0 0
elsif (defined $self->function) { } 0 0 0
132 0 $self->value($matches[0]); # Remember the name of the match
133 0 @values = @{$hashmatch}; # Get the values from the hash
134  
135 # If no uniqe matches, try an (optional) custom function
136 } elsif (defined $self->function()) {
137 0 @values = &{$self->function()}($name);
138 0 ▶ if (scalar(@values)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @values) 0 0 0
139 0 $self->value($name); # Remember the name of the match
140 }
141 }
142  
143 0 ▶ if (@values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) { } 0 0 0
144 # Got though each varable in the hash and update the value
145 0 foreach my $var ($self->vars()) {
146 0 my $value = shift @values;
147 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
148 0 $var->value($value);
149 0 ▶ print ' ', $var->name, ' = ', $var->svalue(), "\n" if (!$quiet);
Branch: 1 branch
BranchTrueFalseTotal
unless $quiet 0 0 0
150 }
151 }
152 0 return $self->value();
153 } else { # We didn't find any unique matches
154 0 ▶ if (@matches) { # There WERE multiple matches from the standard method
Branch: 1 branch
BranchTrueFalseTotal
if (@matches) { } 0 0 0
155 0 return @matches;
156 } else {
157 0 return undef;
158 }
159 }
160 }
161  
162 1 1;
163  
« CLI/Array.pm ↑ Summary CLI/MixedArray.pm »