Template/Context.pm

filter: Statementstmt 10.6% 16 / 150 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 23.8% 5 / 21 Podpod 73.3% 11 / 15 totaltotal 11.2% 32 / 284 CC 48 SCAR 43.9
CC
48
Cov
7%
resolve
240.0
get
110.0
_find_param_in_map
56.0
CRAP 80.7
« Template/Container/Conditional.pm ↑ Summary Template/Format.pm »
1 package Excel::Template::Context;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 BEGIN {
6 1 ▶ use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶ @ISA = qw(Excel::Template::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶ use Excel::Template::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 }
11  
12 1 ▶use Excel::Template::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # This is a helper object. It is not instantiated by the user, nor does it
15 # represent an XML node. Rather, every container will use this object to
16 # maintain the context for its children.
17  
18 1 my %isAbsolute = map { $_ => ~~1 } qw(
19 ROW
20 COL
21 );
22  
23 sub new
24 {
25 0 ▶ my $class = shift;
Subroutine new: not called CC=5 SCAR=34.0
Pod undocumented
26 0 my $self = $class->SUPER::new(@_);
27  
28 0 $self->{ACTIVE_WORKSHEET} = undef;
29 0 $self->{FORMAT_OBJECT} = Excel::Template::Format->new;
30 0 $self->{ACTIVE_FORMAT} = $self->{FORMAT_OBJECT}->blank_format($self);
31 0 $self->{WORKSHEET_NAMES} = undef;
32  
33 0 $self->{__MARKS} = {};
34  
35 # Removed NAME_MAP until I figure out what the heck it's for
36 0 for (qw( STACK PARAM_MAP ))
37 {
38 0 ▶ next if defined $self->{$_} && ref $self->{$_} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$_} and ref $self->{$_} eq "ARRAY" 0 0 0
Condition0%0/3
defined $self->{$_} and ref $self->{$_} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{$_} and ref $self->{$_} eq "ARRAY"
defined $self->{$_} ref $self->{$_} eq "ARRAY"
Truth table0%0/3
A and B
A= defined $self->{$_}B= ref $self->{$_} eq "ARRAY"
ABresult
0X0
100
111
39 0 $self->{$_} = [];
40 }
41  
42 0 $self->{$_} = 0 for keys %isAbsolute;
43  
44 0 return $self;
45 }
46  
47 0 ▶sub use_unicode { $_[0]->{UNICODE} && 1 }
Subroutine use_unicode: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"UNICODE"} and 1 0 0 0
48  
49 sub _find_param_in_map
50 {
51 0 ▶ my $self = shift;
Subroutine _find_param_in_map: not called CC=7 SCAR=40.3
52 0 my ($map, $param, $depth) = @_;
53 0 $param = uc $param;
54 0 ▶ $depth ||= 0;
Condition0%0/2
$depth ||= 0
l!l
00
MC/DC0%0/1
$depth ||= 0
$depth
Truth table0%0/2
A
A= $depth
Aresult
11
00
55  
56 0 my ($val, $found);
57 0 for my $map (reverse @{$self->{$map}})
58 {
59 0 ▶ next unless exists $map->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $map->{$param} 0 0 0
60 0 ▶ $depth--, next if $depth;
Branch: 1 branch
BranchTrueFalseTotal
if $depth 0 0 0
61  
62 0 $found = ~~1;
63 0 $val = $map->{$param};
64 0 last;
65 }
66  
67 die "Parameter '$param' not found\n"
68 0 ▶ if !$found && $self->{DIE_ON_NO_PARAM};
Branch: 1 branch
BranchTrueFalseTotal
if not $found and $self->{"DIE_ON_NO_PARAM"} 0 0 0
Condition0%0/3
not $found and $self->{"DIE_ON_NO_PARAM"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $found and $self->{"DIE_ON_NO_PARAM"}
not $found $self->{"DIE_ON_NO_PARAM"}
Truth table0%0/3
not(A) and B
A= not $foundB= $self->{"DIE_ON_NO_PARAM"}
ABresult
0X0
100
111
69  
70 0 return $val;
71 }
72  
73 sub param
74 {
75 0 ▶ my $self = shift;
Subroutine param: not called CC=1 SCAR=6.9
Pod documented
76 0 $self->_find_param_in_map(
77 'PARAM_MAP',
78 @_,
79 );
80 }
81  
82 #sub named_param
83 #{
84 # my $self = shift;
85 # $self->_find_param_in_map(
86 # 'NAME_MAP',
87 # @_,
88 # );
89 #}
90  
91 sub resolve
92 {
93 0 ▶ my $self = shift;
Subroutine resolve: not called CC=15 SCAR=54.8
Pod undocumented
94 0 my ($obj, $key, $depth) = @_;
95 0 $key = uc $key;
96 0 ▶ $depth ||= 0;
Condition0%0/2
$depth ||= 0
l!l
00
MC/DC0%0/1
$depth ||= 0
$depth
Truth table0%0/2
A
A= $depth
Aresult
11
00
97  
98 0 my $obj_val = $obj->{$key};
99  
100 0 ▶ $obj_val = $self->param($1)
Branch: 1 branch
BranchTrueFalseTotal
if $obj_val =~ /^\$(\S+)$/o 0 0 0
101 if $obj_val =~ /^\$(\S+)$/o;
102  
103 #GGG Remove this once NAME_MAP is working
104 # $obj_val = $self->named_param($1)
105 # if $obj_val =~ /^\\(\S+)$/o;
106  
107 #GGG Does this adequately test values to make sure they're legal??
108 # A value is defined as:
109 # 1) An optional operator (+, -, *, or /)
110 # 2) A decimal number
111  
112 #GGG Convert this to use //x
113 0 my ($op, $val) = $obj_val =~ m/^\s*([\+\*\/\-])?\s*([\d.]*\d)\s*$/oi;
114  
115 # Unless it's a relative value, we have what we came for.
116 0 ▶ return $obj_val unless $op;
Branch: 1 branch
BranchTrueFalseTotal
unless $op 0 0 0
117  
118 my $prev_val = $isAbsolute{$key}
119 0 ▶ ? $self->{$key}
Branch: 1 branch
BranchTrueFalseTotal
$isAbsolute{$key} ? : 0 0 0
120 : $self->get($obj, $key, $depth + 1);
121  
122 0 ▶ return $obj_val unless defined $prev_val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prev_val 0 0 0
123 0 ▶ return $prev_val unless defined $obj_val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj_val 0 0 0
124  
125 # Prevent divide-by-zero issues.
126 0 ▶ return $prev_val if $op eq '/' and $val == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "/" and $val == 0 0 0 0
Condition0%0/3
$op eq "/" and $val == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$op eq "/" and $val == 0
$op eq "/" $val == 0
Truth table0%0/3
A and B
A= $op eq "/"B= $val == 0
ABresult
0X0
100
111
127  
128 0 my $new_val;
129 0 for ($op)
130 {
131 0 ▶ /^\+$/ && do { $new_val = ($prev_val + $val); last; };
Branch: 1 branch
BranchTrueFalseTotal
/^\+$/ and do { $new_val = $prev_val + $val; last } 0 0 0
132 0 ▶ /^\-$/ && do { $new_val = ($prev_val - $val); last; };
Branch: 1 branch
BranchTrueFalseTotal
/^\-$/ and do { $new_val = $prev_val - $val; last } 0 0 0
133 0 ▶ /^\*$/ && do { $new_val = ($prev_val * $val); last; };
Branch: 1 branch
BranchTrueFalseTotal
/^\*$/ and do { $new_val = $prev_val * $val; last } 0 0 0
134 0 ▶ /^\/$/ && do { $new_val = ($prev_val / $val); last; };
Branch: 1 branch
BranchTrueFalseTotal
m[^/$] and do { $new_val = $prev_val / $val; last } 0 0 0
135  
136 0 die "Unknown operator '$op' in arithmetic resolve\n";
137 }
138  
139 0 ▶ return $new_val if defined $new_val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_val 0 0 0
140 0 return;
141 }
142  
143 sub enter_scope
144 {
145 0 ▶ my $self = shift;
Subroutine enter_scope: not called CC=3 SCAR=24.8
Pod undocumented
146 0 my ($obj) = @_;
147  
148 0 push @{$self->{STACK}}, $obj;
149  
150 0 for my $key (keys %isAbsolute)
151 {
152 0 ▶ next unless exists $obj->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $obj->{$key} 0 0 0
153 0 $self->{$key} = $self->resolve($obj, $key);
154 }
155  
156 0 return ~~1;
157 }
158  
159 sub exit_scope
160 {
161 0 ▶ my $self = shift;
Subroutine exit_scope: not called CC=3 SCAR=24.8
Pod undocumented
162 0 my ($obj, $no_delta) = @_;
163  
164 0 ▶ unless ($no_delta)
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_delta) 0 0 0
165 {
166 0 my $deltas = $obj->deltas($self);
167 0 $self->{$_} += $deltas->{$_} for keys %$deltas;
168 }
169  
170 0 pop @{$self->{STACK}};
171  
172 0 return ~~1;
173 }
174  
175 sub get
176 {
177 0 ▶ my $self = shift;
Subroutine get: not called CC=10 SCAR=47.0
Pod documented
178 0 my ($dummy, $key, $depth) = @_;
179 0 ▶ $depth ||= 0;
Condition0%0/2
$depth ||= 0
l!l
00
MC/DC0%0/1
$depth ||= 0
$depth
Truth table0%0/2
A
A= $depth
Aresult
11
00
180 0 $key = uc $key;
181  
182 0 ▶ return unless @{$self->{STACK}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"STACK"};} 0 0 0
183  
184 0 my $obj = $self->{STACK}[-1];
185  
186 0 ▶ return $self->{$key} if $isAbsolute{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $isAbsolute{$key} 0 0 0
187  
188 0 my $val = undef;
189 0 my $this_depth = $depth;
190 0 foreach my $e (reverse @{$self->{STACK}})
191 {
192 0 ▶ next unless exists $e->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $e->{$key} 0 0 0
193 0 ▶ next if $this_depth-- > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $this_depth-- > 0 0 0 0
194  
195 0 $val = $self->resolve($e, $key, $depth);
196 0 last;
197 }
198  
199 0 ▶ $val = $self->{$key} unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
200 0 ▶ return $val unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
201  
202 0 ▶ return $self->param($1, $depth) if $val =~ /^\$(\S+)$/o;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^\$(\S+)$/o 0 0 0
203  
204 0 return $val;
205 }
206  
207 sub active_format
208 {
209 0 ▶ my $self = shift;
Subroutine active_format: not called CC=2 SCAR=17.9
Pod documented
210
211 0 ▶ $self->{ACTIVE_FORMAT} = $_[0]
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
212 if @_;
213  
214 0 $self->{ACTIVE_FORMAT};
215 }
216  
217 sub new_worksheet
218 {
219 0 ▶ my $self = shift;
Subroutine new_worksheet: not called CC=4 SCAR=30.0
Pod documented
220 0 my ($worksheet) = @_;
221  
222 0 $self->{ROW} = $self->{COL} = 0;
223 0 $self->{REFERENCES} = {};
224  
225 0 my $name = $self->get( $worksheet, 'NAME' );
226  
227 0 ▶ if ( defined $name && length $name )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name and length $name) { } 0 0 0
Condition0%0/3
defined $name and length $name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table0%0/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
228 {
229 0 ▶ if ( exists $self->{WORKSHEET_NAMES}{$name} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'WORKSHEET_NAMES'}{$name}) { } 0 0 0
230 {
231 0 $name = '';
232 }
233 else
234 {
235 0 $self->{WORKSHEET_NAMES}{$name} = undef;
236 }
237 }
238 else
239 {
240 0 $name = '';
241 }
242  
243 return $self->active_worksheet(
244 0 $self->{XLS}->add_worksheet( $name ),
245 );
246 }
247  
248 sub mark
249 {
250 0 ▶ my $self = shift;
Subroutine mark: not called CC=2 SCAR=17.9
Pod documented
251  
252 0 ▶ if ( @_ > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
253 {
254 0 my %args = @_;
255  
256 0 @{$self->{__MARKS}}{keys %args} = values %args;
257 }
258  
259 0 return $self->{__MARKS}{$_[0]}
260 }
261  
262 sub active_worksheet
263 {
264 0 ▶ my $self = shift;
Subroutine active_worksheet: not called CC=2 SCAR=17.9
Pod documented
265
266 0 ▶ $self->{ACTIVE_WORKSHEET} = $_[0]
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
267 if @_;
268  
269 0 $self->{ACTIVE_WORKSHEET};
270 }
271  
272 sub add_reference
273 {
274 0 ▶ my $self = shift;
Subroutine add_reference: not called CC=2 SCAR=17.9
Pod documented
275 0 my ($ref, $row, $col) = @_;
276  
277 0 ▶ $self->{REFERENCES}{$ref} ||= [];
Condition0%0/2
$self->{"REFERENCES"}{$ref} ||= []
l!l
00
MC/DC0%0/1
$self->{"REFERENCES"}{$ref} ||= []
$self->{"REFERENCES"}{$ref}
Truth table0%0/2
A
A= $self->{"REFERENCES"}{$ref}
Aresult
11
00
278  
279 0 push @{$self->{REFERENCES}{$ref}}, [ $row, $col ];
280  
281 0 return ~~1;
282 }
283  
284 sub get_all_references
285 {
286 0 ▶ my $self = shift;
Subroutine get_all_references: not called CC=2 SCAR=17.9
Pod documented
287 0 my $ref = uc shift;
288  
289 0 ▶ $self->{REFERENCES}{$ref} ||= [];
Condition0%0/2
$self->{"REFERENCES"}{$ref} ||= []
l!l
00
MC/DC0%0/1
$self->{"REFERENCES"}{$ref} ||= []
$self->{"REFERENCES"}{$ref}
Truth table0%0/2
A
A= $self->{"REFERENCES"}{$ref}
Aresult
11
00
290  
291 0 return @{ $self->{REFERENCES}{$ref} };
292 }
293  
294 sub get_last_reference
295 {
296 0 ▶ my $self = shift;
Subroutine get_last_reference: not called CC=2 SCAR=17.9
Pod documented
297 0 my $ref = uc shift;
298  
299 0 ▶ $self->{REFERENCES}{$ref} ||= [];
Condition0%0/2
$self->{"REFERENCES"}{$ref} ||= []
l!l
00
MC/DC0%0/1
$self->{"REFERENCES"}{$ref} ||= []
$self->{"REFERENCES"}{$ref}
Truth table0%0/2
A
A= $self->{"REFERENCES"}{$ref}
Aresult
11
00
300  
301 0 return @{ $self->{REFERENCES}{$ref}[-1] };
302 }
303  
304 0 ▶sub format_object { $_[0]{FORMAT_OBJECT} }
Subroutine format_object: not called CC=1 SCAR=6.9
Pod documented
305  
306 1 1;
307 __END__
« Template/Container/Conditional.pm ↑ Summary Template/Format.pm »