blib/lib/Class/Measure.pm

filter: Statementstmt 96.3% 211 / 219 Branchbran 81.6% 80 / 98 Conditioncond 60.7% 34 / 56 MC/DCmcdc 22.8% 8 / 35 Subroutinesub 100% 28 / 28 Podpod 100% 9 / 9 totaltotal 83.1% 370 / 445 SCAR 45.0
CC
77
Cov
87%
reg_convs
11.8
_find_path
10.0
reg_aliases
9.4
CRAP 89.6
↑ Summary Build »
1 package Class::Measure;
2 4 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 our $VERSION = '0.08';
6  
7 =encoding utf8
8  
9 =head1 NAME
10  
11 Class::Measure - Create, compare, and convert units of measurement.
12  
13 =head1 SYNOPSIS
14  
15 See L<Class::Measure::Length> for some examples.
16  
17 =head1 DESCRIPTION
18  
19 This is a base class that is inherited by the Class::Measure
20 classes. This distribution comes with the class L<Class::Measure::Length>.
21  
22 The classes L<Class::Measure::Area>, L<Class::Measure::Mass>,
23 L<Class::Measure::Space>, L<Class::Measure::Temperature>,
24 and L<Class::Measure::Volume> are planned and will be added soon.
25  
26 The methods described here are available in all Class::Measure classes.
27  
28 =cut
29  
30 4 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
31 4 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 use overload
34 4 '+'=>\&_ol_add, '-'=>\&_ol_sub,
35 '*'=>\&_ol_mult, '/'=>\&_ol_div,
36 4 ▶ '""'=>\&_ol_str;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 4 our $type_convs = {};
39 4 our $type_paths = {};
40 4 our $type_aliases = {};
41  
42 =head1 METHODS
43  
44 =head2 new
45  
46 my $m = new Class::Measure::Length( 1, 'inch' );
47  
48 Creates a new measurement object. You must pass an initial
49 measurement and default unit.
50  
51 In most cases the measurement class that you are using
52 will export a method to create new measurements. For
53 example L<Class::Measure::Length> exports the
54 C<length()> method.
55  
56 =cut
57  
58 sub new {
59 14 ▶ my $class = shift;
Subroutine new: called CC=5 SCAR=16.1
Pod documented
60  
61 14 my $unit = pop;
62 14 ▶ $unit = $type_aliases->{$class}->{$unit} || $unit if $unit;
Branch: 1 branch
BranchTrueFalseTotal
if $unit 12 2 14
Condition66%2/3
$Class::Measure::type_aliases->{$class}{$unit} || $unit
l!l&&r!l&&!r
1110
MC/DC0%0/2
$Class::Measure::type_aliases->{$class}{$unit} || $unit
$Class::Measure::type_aliases->{$class}{$unit} $unit
Truth table66%2/3
A || B
A= $Class::Measure::type_aliases->{$class}{$unit}B= $unit
ABresult
1X1
011
000
63  
64 croak 'Unknown Class::Measure unit'
65 14 ▶ unless $unit and $type_convs->{$class}->{$unit};
Branch: 1 branch
BranchTrueFalseTotal
unless $unit and $Class::Measure::type_convs->{$class}{$unit} 3 11 14
Condition100%3/3
$unit and $Class::Measure::type_convs->{$class}{$unit}
!ll&&!rl&&r
2111
MC/DC100%2/2
$unit and $Class::Measure::type_convs->{$class}{$unit}
$unit $Class::Measure::type_convs->{$class}{$unit}
Truth table100%3/3
A and B
A= $unitB= $Class::Measure::type_convs->{$class}{$unit}
ABresult
0X0
100
111
66  
67 11 return bless {
68 unit => $unit,
69 values => { $unit => shift },
70 }, $class;
71 }
72  
73 =head2 unit
74  
75 my $unit = $m->unit();
76  
77 Returns the object's default unit.
78  
79 =cut
80  
81 sub unit {
82 24 ▶ my $self = shift;
Subroutine unit: called CC=1 SCAR=0.0
Pod documented
83 24 return $self->{unit};
84 }
85  
86 =head2 set_unit
87  
88 $m->set_unit( 'feet' );
89  
90 Sets the default unit of the measurement.
91  
92 =cut
93  
94 sub set_unit {
95 21 ▶ my $self = shift;
Subroutine set_unit: called CC=1 SCAR=0.0
Pod documented
96 21 my $unit = $self->_unalias( shift );
97 21 $self->_conv( $unit );
98 21 $self->{unit} = $unit;
99 21 return;
100 }
101  
102 =head2 value
103  
104 my $yards = $m->value('yards');
105 my $val = $m->value();
106 print "$m is the same as $val when in a string\n";
107  
108 Retrieves the value of the measurement in the
109 default unit. You may specify a unit in which
110 case the value is converted to the unit and returned.
111  
112 This method is also used to handle overloading of
113 stringifying the object.
114  
115 =cut
116  
117 sub value {
118 54 ▶ my $self = shift;
Subroutine value: called CC=2 SCAR=6.9
Pod documented
119 54 ▶ return $self->_conv(shift) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 52 54
120 52 return $self->{values}->{$self->{unit}};
121 }
122  
123 =head2 set_value
124  
125 my $m = length( 0, 'inches' );
126 $m->set_value( 12 ); # 12 inches.
127 $m->set_value( 1, 'foot' ); # 1 foot.
128  
129 Sets the measurement in the default unit. You may
130 specify a new default unit as well.
131  
132 =cut
133  
134 sub set_value {
135 18 ▶ my $self = shift;
Subroutine set_value: called CC=2 SCAR=6.9
Pod documented
136 18 ▶ $self->{unit} = $self->_unalias(pop @_) if( @_>1 );
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 3 15 18
137 18 $self->{values} = { $self->{unit} => shift };
138 18 return;
139 }
140  
141 =head2 reg_units
142  
143 Class::Measure::Length->reg_units(
144 'inch', 'foot', 'yard',
145 );
146  
147 Registers one or more units for use in the specified
148 class. Units should be in the singular, most common,
149 form.
150  
151 =cut
152  
153 sub reg_units {
154 8 ▶ my $self = shift;
Subroutine reg_units: called CC=5 SCAR=16.3
Pod documented
155 8 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
080
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
156 8 ▶ my $convs = $type_convs->{$class} ||= {};
Condition100%2/2
$Class::Measure::type_convs->{$class} ||= {}
l!l
53
MC/DC100%1/1
$Class::Measure::type_convs->{$class} ||= {}
$Class::Measure::type_convs->{$class}
Truth table100%2/2
A
A= $Class::Measure::type_convs->{$class}
Aresult
11
00
157 8 foreach my $unit (@_){
158 30 ▶ croak('This unit has already been defined') if $convs->{$unit};
Branch: 1 branch
BranchTrueFalseTotal
if $convs->{$unit} 0 30 30
159 30 $convs->{$unit} = {};
160  
161 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
162 30 *{"${class}::${unit}"} = _build_unit_sub( $unit );
163 }
164 8 return;
165 }
166  
167 sub _build_unit_sub {
168 30 ▶ my ($unit) = @_;
Subroutine _build_unit_sub: called CC=1 SCAR=0.0
169  
170 return sub{
171 8 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.1
172 8 ▶ return $self->set_value( shift(), $unit ) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 8 8
173 8 return $self->_conv( $unit );
174 30 };
175 }
176  
177 =head2 units
178  
179 my @units = Class::Measure::Length->units();
180  
181 Returns a list of all registered units.
182  
183 =cut
184  
185 sub units {
186 1 ▶ my $self = shift;
Subroutine units: called CC=2 SCAR=7.4
Pod documented
187 1 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
188 1 return keys(%{$type_convs->{$class}});
189 }
190  
191 =head2 reg_aliases
192  
193 Class::Measure::Length->reg_aliases(
194 ['feet','ft'] => 'foot',
195 ['in','inches'] => 'inch',
196 'yards' => 'yard'
197 );
198  
199 Register alternate names for units. Expects two
200 arguments per unit to alias. The first argument
201 being the alias (scalar) or aliases (array ref), and
202 the second argument being the unit to alias them to.
203  
204 =cut
205  
206 sub reg_aliases {
207 6 ▶ my $self = shift;
Subroutine reg_aliases: called CC=9 SCAR=22.4
Pod documented
208 6 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
209 6 ▶ croak('Wrong number of arguments (must be a multiple of two)') if( (@_+0) % 2 );
Branch: 1 branch
BranchTrueFalseTotal
if (@_ + 0) % 2 0 6 6
210 6 ▶ my $aliases = $type_aliases->{$class} ||= {};
Condition100%2/2
$Class::Measure::type_aliases->{$class} ||= {}
l!l
51
MC/DC100%1/1
$Class::Measure::type_aliases->{$class} ||= {}
$Class::Measure::type_aliases->{$class}
Truth table100%2/2
A
A= $Class::Measure::type_aliases->{$class}
Aresult
11
00
211 6 ▶ while( @_ ){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 26 6 32
212 26 ▶ my @aliases = ( ref($_[0]) ? @{shift()} : shift );
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 10 16 26
213 26 my $unit = shift;
214 26 ▶ croak('Unknown unit "'.$unit.'" to alias to') unless( defined $type_convs->{$class}->{$unit} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Class::Measure::type_convs->{$class}{$unit} 0 26 26
215 26 foreach my $alias (@aliases){
216 54 ▶ if( defined $aliases->{$alias} ){ croak('Alias already in use'); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $aliases->{$alias}) 0 54 54
217 54 $aliases->{$alias} = $unit;
218  
219 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
220 54 *{"${class}::${alias}"} = *{"${class}::${unit}"};
221 }
222 }
223 6 return;
224 }
225  
226 =head2 reg_convs
227  
228 Class::Measure::Length->reg_convs(
229 12, 'inches' => 'foot',
230 'yard' => '3', 'feet'
231 );
232  
233 Registers a unit conversion. There are three distinct
234 ways to specify a new conversion. Each requires three
235 arguments.
236  
237 $count1, $unit1 => $unit2
238 $unit1 => $count2, $unit2
239  
240 These first two syntaxes create automatic reverse conversions
241 as well. So, saying there are 12 inches in a foot implies
242 that there are 1/12 feet in an inch.
243  
244 $unit1 => $unit2, $sub
245  
246 The third syntax accepts a subroutine as the last argument
247 the subroutine will be called with the value of $unit1 and
248 it's return value will be assigned to $unit2. This
249 third syntax does not create a reverse conversion automatically.
250  
251 =cut
252  
253 sub reg_convs {
254 9 ▶ my $self = shift;
Subroutine reg_convs: called CC=11 SCAR=24.7
Pod documented
255 9 ▶ croak('Wrong number of arguments (must be a multiple of three)') if( (@_+0) % 3 );
Branch: 1 branch
BranchTrueFalseTotal
if (@_ + 0) % 3 0 9 9
256 9 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
090
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
257 9 ▶ while(@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 29 9 38
258 29 my($from,$to,$conv);
259 # First check for coderef to avoid seeing units as number in that case:
260 29 ▶ if( ref($_[2]) eq 'CODE' ){
Branch: 3 branches
BranchTrueFalseTotal
if (ref $_[2] eq 'CODE') { } 1 28 29
elsif (&looks_like_number($_[0])) { } 8 20 28
elsif (&looks_like_number($_[1])) { } 20 0 20
261 1 ($from,$to,$conv) = splice(@_,0,3);
262 }elsif( looks_like_number($_[0]) ){
263 8 ($conv,$from,$to) = splice(@_,0,3);
264 8 $conv = 1 / $conv;
265 }elsif( looks_like_number($_[1]) ){
266 20 ($from,$conv,$to) = splice(@_,0,3);
267 }else{
268 0 croak('Invalid arguments');
269 }
270 29 $from = $self->_unalias($from);
271 29 $to = $self->_unalias($to);
272 29 ▶ my $units = $type_convs->{$class} ||= {};
Condition50%1/2
$Class::Measure::type_convs->{$class} ||= {}
l!l
290
MC/DC0%0/1
$Class::Measure::type_convs->{$class} ||= {}
$Class::Measure::type_convs->{$class}
Truth table50%1/2
A
A= $Class::Measure::type_convs->{$class}
Aresult
11
00
273 29 ▶ $units->{$from} ||= {};
Condition50%1/2
$units->{$from} ||= {}
l!l
290
MC/DC0%0/1
$units->{$from} ||= {}
$units->{$from}
Truth table50%1/2
A
A= $units->{$from}
Aresult
11
00
274 29 $units->{$from}->{$to} = $conv;
275 29 ▶ unless( ref $conv ){
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $conv) 28 1 29
276 28 ▶ $units->{$to} ||= {};
Condition50%1/2
$units->{$to} ||= {}
l!l
280
MC/DC0%0/1
$units->{$to} ||= {}
$units->{$to}
Truth table50%1/2
A
A= $units->{$to}
Aresult
11
00
277 28 $units->{$to}->{$from} = 1/$conv;
278 }
279 }
280 9 $type_paths->{$class} = {};
281 9 return;
282 }
283  
284 sub _unalias {
285 163 ▶ my $self = shift;
Subroutine _unalias: called CC=4 SCAR=14.3
286 163 ▶ my $class = ref($self) || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
99640
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
287 163 my $unit = shift;
288 163 ▶ return $unit if( defined $type_convs->{$class}->{$unit} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $Class::Measure::type_convs->{$class}{$unit} 132 31 163
289 31 ▶ return $type_aliases->{$class}->{$unit} || croak('Unknown unit or alias "'.$unit.'"');
Condition33%1/3
$Class::Measure::type_aliases->{$class}{$unit} || croak('Unknown unit or alias "' . $unit . '"')
l!l&&r!l&&!r
3100
MC/DC0%0/2
$Class::Measure::type_aliases->{$class}{$unit} || croak('Unknown unit or alias "' . $unit . '"')
$Class::Measure::type_aliases->{$class}{$unit} croak('Unknown unit or alias "' . $unit . '"')
Truth table33%1/3
A || B
A= $Class::Measure::type_aliases->{$class}{$unit}B= croak('Unknown unit or alias "' . $unit . '"')
ABresult
1X1
011
000
290 }
291  
292 sub _conv {
293 31 ▶ my $self = shift;
Subroutine _conv: called CC=6 SCAR=18.0
294 31 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
3100
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
295 31 my $unit = $self->_unalias( shift );
296 31 ▶ return $self->{values}->{$unit} if( defined $self->{values}->{$unit} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"values"}{$unit} 9 22 31
297 22 my $path = $self->_path( $self->unit, $unit );
298 22 ▶ croak('Unable to find an appropriate conversion path') unless( $path );
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 22 22
299 22 my $units = $type_convs->{$class};
300 22 my $prev_unit = shift( @$path );
301 22 my $value = $self->value;
302 22 foreach $unit (@$path){
303 60 my $conv = $units->{$prev_unit}->{$unit};
304 60 ▶ if( ref($conv) ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $conv) { } 1 59 60
305 1 $value = &{$conv}( $value, $prev_unit, $unit );
306 }else{
307 59 $value = $value * $units->{$prev_unit}->{$unit};
308 }
309 60 $self->{values}->{$unit} = $value;
310 60 $prev_unit = $unit;
311 }
312 22 return $value;
313 }
314  
315 sub _path {
316 25 ▶ my $self = shift;
Subroutine _path: called CC=8 SCAR=21.0
317 25 my $from = $self->_unalias(shift);
318 25 my $to = $self->_unalias(shift);
319 25 ▶ my $class = ref($self) || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
2230
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
320 25 my $key = "$from-$to";
321 25 ▶ my $paths = $type_paths->{$class} ||= {};
Condition50%1/2
$Class::Measure::type_paths->{$class} ||= {}
l!l
250
MC/DC0%0/1
$Class::Measure::type_paths->{$class} ||= {}
$Class::Measure::type_paths->{$class}
Truth table50%1/2
A
A= $Class::Measure::type_paths->{$class}
Aresult
11
00
322 25 ▶ if( defined $paths->{$key} ){ return [@{$paths->{$key}}]; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $paths->{$key}) 3 22 25
323  
324 22 ▶ my $units = $type_convs->{$class} ||= {};
Condition50%1/2
$Class::Measure::type_convs->{$class} ||= {}
l!l
220
MC/DC0%0/1
$Class::Measure::type_convs->{$class} ||= {}
$Class::Measure::type_convs->{$class}
Truth table50%1/2
A
A= $Class::Measure::type_convs->{$class}
Aresult
11
00
325 22 my $path;
326 22 foreach (1..10){
327 87 $path = _find_path( $from, $to, $units, $_ );
328 87 ▶ last if( $path );
Branch: 1 branch
BranchTrueFalseTotal
if $path 22 65 87
329 }
330 22 ▶ return 0 if(!$path);
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 22 22
331 22 $paths->{$key} = $path;
332 22 return [@$path];
333 }
334  
335 sub _find_path {
336 569 ▶ my($level,$to,$units) = splice(@_,0,3);
Subroutine _find_path: called CC=10 SCAR=23.0
337 569 ▶ unless( ref $level ){ $level=[$level]; }
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $level) 87 482 569
338 569 ▶ my $max_depth = ( @_ ? shift : 12 );
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 569 0 569
339 569 ▶ my $depth = ( @_ ? shift : 0 );
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 482 87 569
340 569 ▶ my $path = ( @_ ? shift : [] );
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 482 87 569
341 569 my $next_level = {};
342  
343 569 foreach my $unit (@$level){
344 1322 ▶ if($unit eq $to){
Branch: 1 branch
BranchTrueFalseTotal
if ($unit eq $to) 22 1300 1322
345 22 push @$path, $unit;
346 22 return $path;
347 }
348 }
349  
350 547 ▶ return 0 if( $depth+1 == $max_depth );
Branch: 1 branch
BranchTrueFalseTotal
if $depth + 1 == $max_depth 298 249 547
351 249 $depth ++;
352  
353 249 foreach my $unit (@$level){
354 482 push @$path, $unit;
355 482 ▶ if(_find_path( [keys %{$units->{$unit}}], $to, $units, $max_depth, $depth, $path )){
Branch: 1 branch
BranchTrueFalseTotal
if (_find_path([keys %{$$units{$unit};}], $to, $units, $max_depth, $depth, $path)) 65 417 482
356 65 $depth --;
357 65 return $path;
358 }
359 417 pop @$path;
360 }
361  
362 184 $depth --;
363 184 return 0;
364 }
365  
366 sub _ol_add {
367 4 ▶ my($one,$two,$opt) = @_;
Subroutine _ol_add: called CC=7 SCAR=19.6
368 4 ▶ if($opt){ my $tmp=$one; $one=$two; $two=$tmp; }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt) 1 3 4
369 4 ▶ if( ref($two) and ref($one) ){
Branch: 3 branches
BranchTrueFalseTotal
if (ref $two and ref $one) { } 1 3 4
elsif (ref $one) { } 2 1 3
elsif (ref $two) { } 1 0 1
Condition100%3/3
ref $two and ref $one
!ll&&!rl&&r
211
MC/DC100%2/2
ref $two and ref $one
ref $two ref $one
Truth table100%3/3
A and B
A= ref $twoB= ref $one
ABresult
0X0
100
111
370 1 ▶ croak('You may only add numbers or measurements of the same class') if( ref($one) ne ref($two) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $one ne ref $two 0 1 1
371 1 $one->set_value( $one->value + $two->value($one->unit) );
372 1 return $one;
373 }elsif( ref $one ){
374 2 $one->set_value( $one->value + $two );
375 2 return $one;
376 }elsif( ref $two ){
377 1 $two->set_value( $one + $two->value );
378 1 return $two;
379 }
380 0 return;
381 }
382  
383 sub _ol_sub {
384 4 ▶ my($one,$two,$opt) = @_;
Subroutine _ol_sub: called CC=7 SCAR=19.6
385 4 ▶ if($opt){ my $tmp=$one; $one=$two; $two=$tmp; }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt) 1 3 4
386 4 ▶ if( ref($two) and ref($one) ){
Branch: 3 branches
BranchTrueFalseTotal
if (ref $two and ref $one) { } 1 3 4
elsif (ref $one) { } 2 1 3
elsif (ref $two) { } 1 0 1
Condition100%3/3
ref $two and ref $one
!ll&&!rl&&r
211
MC/DC100%2/2
ref $two and ref $one
ref $two ref $one
Truth table100%3/3
A and B
A= ref $twoB= ref $one
ABresult
0X0
100
111
387 1 ▶ croak('You may only subtract numbers or measurements of the same class') if( ref($one) ne ref($two) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $one ne ref $two 0 1 1
388 1 $one->set_value( $one->value - $two->value($one->unit) );
389 1 return $one;
390 }elsif( ref $one ){
391 2 $one->set_value( $one->value - $two );
392 2 return $one;
393 }elsif( ref $two ){
394 1 $two->set_value( $one - $two->value );
395 1 return $two;
396 }
397 0 return;
398 }
399  
400 sub _ol_mult {
401 4 ▶ my($one,$two,$opt) = @_;
Subroutine _ol_mult: called CC=6 SCAR=18.5
402 4 ▶ if($opt){ my $tmp=$one; $one=$two; $two=$tmp; }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt) 1 3 4
403 4 ▶ if( ref($two) and ref($one) ){
Branch: 3 branches
BranchTrueFalseTotal
if (ref $two and ref $one) { } 0 4 4
elsif (ref $one) { } 3 1 4
elsif (ref $two) { } 1 0 1
Condition66%2/3
ref $two and ref $one
!ll&&!rl&&r
310
MC/DC0%0/2
ref $two and ref $one
ref $two ref $one
Truth table66%2/3
A and B
A= ref $twoB= ref $one
ABresult
0X0
100
111
404 0 croak('You cannot multiply two measure classes');
405 }elsif( ref $one ){
406 3 $one->set_value( $one->value * $two );
407 3 return $one;
408 }elsif( ref $two ){
409 1 $two->set_value( $one * $two->value );
410 1 return $two;
411 }
412 0 return;
413 }
414  
415 sub _ol_div {
416 3 ▶ my($one,$two,$opt) = @_;
Subroutine _ol_div: called CC=6 SCAR=18.5
417 3 ▶ if($opt){ my $tmp=$one; $one=$two; $two=$tmp; }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt) 1 2 3
418 3 ▶ if( ref($two) and ref($one) ){
Branch: 3 branches
BranchTrueFalseTotal
if (ref $two and ref $one) { } 0 3 3
elsif (ref $one) { } 2 1 3
elsif (ref $two) { } 1 0 1
Condition66%2/3
ref $two and ref $one
!ll&&!rl&&r
210
MC/DC0%0/2
ref $two and ref $one
ref $two ref $one
Truth table66%2/3
A and B
A= ref $twoB= ref $one
ABresult
0X0
100
111
419 0 croak('You cannot divide one measure class by another');
420 }elsif( ref $one ){
421 2 $one->set_value( $one->value / $two );
422 2 return $one;
423 }elsif( ref $two ){
424 1 $two->set_value( $one / $two->value );
425 1 return $two;
426 }
427 0 return;
428 }
429  
430 sub _ol_str {
431 1 ▶ my $self = shift;
Subroutine _ol_str: called CC=1 SCAR=0.0
432 1 return $self->value;
433 }
434  
435 4 1;
436 __END__
↑ Summary Build »