line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CLI::Hash; |
2
|
1
|
|
|
1
|
|
5
|
use CLI::Base; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
3
|
|
|
|
|
|
|
@ISA = ("CLI::Base"); |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
69
|
|
6
|
1
|
|
|
1
|
|
5
|
use CLI qw( SSTRING parse_string hashmatch ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
57
|
|
7
|
1
|
|
|
1
|
|
42
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
865
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new { |
10
|
0
|
|
|
0
|
0
|
|
my $proto = shift; |
11
|
0
|
|
0
|
|
|
|
my $class = ref($proto) || $proto; |
12
|
|
|
|
|
|
|
|
13
|
0
|
|
|
|
|
|
my $name = shift; |
14
|
0
|
|
|
|
|
|
my @vars = @_; |
15
|
0
|
0
|
|
|
|
|
if (@vars<1) { # We need some values to play with |
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
|
0
|
|
|
|
|
if (!(ref($hash) eq 'HASH')) { # No then leave @vars how it was |
23
|
0
|
|
|
|
|
|
push @vars, $hash; |
24
|
0
|
|
|
|
|
|
$hash = undef; |
25
|
|
|
|
|
|
|
} else { |
26
|
0
|
0
|
|
|
|
|
if (@vars<1) { # We need some values to play with |
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
|
0
|
|
|
|
|
$self->function($hash->{function}) if (defined $hash->{function}); |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
return $self; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub hash { |
50
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
51
|
0
|
|
|
|
|
|
return $self->{HASH}; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub vars { |
55
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
56
|
0
|
0
|
|
|
|
|
if (@_) { $self->{VARS} = (@_) } |
|
0
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
return @{$self->{VARS}}; |
|
0
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub add { |
61
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
62
|
0
|
|
|
|
|
|
my $name = shift; |
63
|
0
|
|
|
|
|
|
my @values = @_; |
64
|
0
|
0
|
|
|
|
|
if (@values) { |
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
|
|
|
|
|
|
|
sub parse { |
77
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
78
|
0
|
|
|
|
|
|
my $string = shift; |
79
|
|
|
|
|
|
|
|
80
|
0
|
0
|
|
|
|
|
if (!defined $string) { # No argument, print name of last value used |
81
|
0
|
0
|
|
|
|
|
if (defined $self->value()) { |
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
|
0
|
|
|
|
|
if (!defined $string) { # no values, extract values from the hash |
90
|
0
|
|
|
|
|
|
my @matches = $self->extract($name,0); |
91
|
0
|
0
|
|
|
|
|
if (!defined $matches[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
|
0
|
|
|
|
|
if (defined $string) { |
104
|
0
|
|
|
|
|
|
my $val = parse_string($var->type(), $string); |
105
|
0
|
0
|
|
|
|
|
if (defined $val) { |
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
|
|
|
|
|
|
|
sub extract { |
121
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
122
|
0
|
|
|
|
|
|
my $name = shift; |
123
|
0
|
|
|
|
|
|
my $quiet = shift; |
124
|
0
|
0
|
|
|
|
|
$quiet=0 if (!defined $quiet); |
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
|
0
|
|
|
|
|
if (defined $hashmatch) { # Did we find a unique match |
|
|
0
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
$self->value($matches[0]); # Remember the name of the match |
133
|
0
|
|
|
|
|
|
@values = @{$hashmatch}; # Get the values from the hash |
|
0
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
# If no uniqe matches, try an (optional) custom function |
136
|
|
|
|
|
|
|
} elsif (defined $self->function()) { |
137
|
0
|
|
|
|
|
|
@values = &{$self->function()}($name); |
|
0
|
|
|
|
|
|
|
138
|
0
|
0
|
|
|
|
|
if (scalar(@values)) { |
139
|
0
|
|
|
|
|
|
$self->value($name); # Remember the name of the match |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
0
|
0
|
|
|
|
|
if (@values) { |
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
|
0
|
|
|
|
|
if (defined $value) { |
148
|
0
|
|
|
|
|
|
$var->value($value); |
149
|
0
|
0
|
|
|
|
|
print ' ', $var->name, ' = ', $var->svalue(), "\n" if (!$quiet); |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
} |
152
|
0
|
|
|
|
|
|
return $self->value(); |
153
|
|
|
|
|
|
|
} else { # We didn't find any unique matches |
154
|
0
|
0
|
|
|
|
|
if (@matches) { # There WERE multiple matches from the standard method |
155
|
0
|
|
|
|
|
|
return @matches; |
156
|
|
|
|
|
|
|
} else { |
157
|
0
|
|
|
|
|
|
return undef; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
1; |
163
|
|
|
|
|
|
|
|