blib/lib/Number/Format/Calc.pm

filter: Statementstmt 96.8% 123 / 127 Branchbran 77.6% 59 / 76 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 95.3% 41 / 43 Podpod 100% 3 / 3 totaltotal 90.7% 226 / 249 CC 42 SCAR 15.9
CC
42
Cov
90%
_operatew
11.1
_compare
9.1
_operate
9.0
CRAP 4.9
↑ Summary
1 package Number::Format::Calc;
2
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
6 4 ▶use Number::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 4 our $VERSION = 0.21;
9
10 4 my %defaults =
11 (
12 -thousands_sep => ",",
13 -decimal_point => ".",
14 );
15
16 sub import
17 4 ▶{
Subroutine import: called CC=2 SCAR=6.9
18 4 shift;
19 4 my %def = @_;
20 4 $defaults{$_} = $def{$_} for keys %def;
21 }
22
23 use overload
24
25 106 ▶ '""' => sub { $_[0]->{formatter}->format_number( $_[0]->{number} ) },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
26
27 #arithmetic operations
28 16 ▶ '+' => sub { _operate ("+", @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 13 ▶ '-' => sub { _operate ("-", @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 21 ▶ '*' => sub { _operate ("*", @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 6 ▶ '/' => sub { _operate ("/", @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 3 ▶ '%' => sub { _operate ("%", @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 9 ▶ '**' => sub { _operate ("**", @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
34
35 #arithmetic operations with assign
36 2 ▶ '+=' => sub { _operatew ("+", @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 1 ▶ '-=' => sub { _operatew ("-", @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 3 ▶ '*=' => sub { _operatew ("*", @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
39 1 ▶ '/=' => sub { _operatew ("/", @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
40 0 ▶ '%=' => sub { _operatew ("%", @_); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 0 ▶ '**=' => sub { _operatew ("**", @_); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
42
43 #arithmetic functions
44 1 ▶ 'abs' => sub { _function ("abs", @_);},
Subroutine __ANON__: called CC=1 SCAR=0.0
45 1 ▶ 'sqrt' => sub { _function ("sqrt",@_);},
Subroutine __ANON__: called CC=1 SCAR=0.0
46 1 ▶ 'cos' => sub { _function ("cos", @_);},
Subroutine __ANON__: called CC=1 SCAR=0.0
47 1 ▶ 'sin' => sub { _function ("sin", @_);},
Subroutine __ANON__: called CC=1 SCAR=0.0
48 1 ▶ 'exp' => sub { _function ("exp", @_);},
Subroutine __ANON__: called CC=1 SCAR=0.0
49 1 ▶ 'log' => sub { _function ("log", @_);},
Subroutine __ANON__: called CC=1 SCAR=0.0
50
51 #mutations
52 6 ▶ '++' => sub { _mutate ("++", @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
53 6 ▶ '--' => sub { _mutate ("--", @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
54
55 #numeric comparisons
56 1 ▶ '<' => sub { _compare ("<", @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
57 2 ▶ '<=' => sub { _compare ("<=", @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
58 1 ▶ '>' => sub { _compare (">", @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 2 ▶ '>=' => sub { _compare (">=", @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
60 1 ▶ '==' => sub { _compare ("==", @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
61 1 ▶ '!=' => sub { _compare ("!=", @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
62
63 #numeric sorting
64 10 ▶ '<=>' => sub { _numsort (@_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
65
66 #fallback
67 4 fallback => 1;
68
69 #/use overload
70
71 sub new
72 18 ▶{
Subroutine new: called CC=4 SCAR=13.9
Pod documented
73 18 my $class = shift;
74 18 my $number = shift;
75 18 my %args = @_;
76
77 18 my %realargs = ();
78
79
80 18 for ( keys %args )
81 {
82 56 my $value = $args{$_};
83
84 56 $_ = lc($_); s/^(?!-)/-/;
85
86 56 $realargs{$_} = $value;
87 }
88
89 18 for ( keys %defaults )
90 {
91 38 my $value = $defaults{$_};
92
93 38 $_ = lc($_);
94 38 s/^(?!-)/-/;
95
96 38 ▶ $realargs{$_} = $value unless exists $realargs{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $realargs{$_} 5 33 38
97 }
98
99 18 my $self = bless {}, 'Number::Format::Calc';
100
101 18 $self->{formatter} = new Number::Format (%realargs);
102 18 $self->{number} = $self->{formatter}->unformat_number( $number );
103
104 18 return $self;
105 }
106
107 sub _operate
108 68 ▶{
Subroutine _operate: called CC=9 SCAR=22.0
109 68 my $op = shift;
110
111 68 ▶ my $op1 = ref( $_[0] ) ? $_[0]->{number} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 68 0 68
112 68 ▶ my $op2 = ref( $_[1] ) ? $_[1]->{number} : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 28 40 68
113
114 68 my $number = { %{$_[0]} };
115
116 68 ▶ if ( $op eq "+" ) { $number->{number} = $op1 + $op2; }
Branch: 6 branches
BranchTrueFalseTotal
if ($op eq '+') { } 16 52 68
elsif ($op eq '-') { } 13 39 52
elsif ($op eq '*') { } 21 18 39
elsif ($op eq '/') { } 6 12 18
elsif ($op eq '%') { } 3 9 12
elsif ($op eq '**') { } 9 0 9
117 13 elsif ( $op eq "-" ) { $number->{number} = $op1 - $op2; }
118 21 elsif ( $op eq "*" ) { $number->{number} = $op1 * $op2; }
119 6 elsif ( $op eq "/" ) { $number->{number} = $op1 / $op2; }
120 3 elsif ( $op eq "%" ) { $number->{number} = $op1 % $op2; }
121 9 elsif ( $op eq "**" ) { $number->{number} = $op1 ** $op2; }
122
123 68 return bless $number, 'Number::Format::Calc';
124 }
125
126 sub _operatew
127 7 ▶{
Subroutine _operatew: called CC=9 SCAR=24.1
128 7 my $op = shift;
129
130 7 ▶ my $op1 = ref( $_[0] ) ? $_[0]->{number} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 7 0 7
131 7 ▶ my $op2 = ref( $_[1] ) ? $_[1]->{number} : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 1 6 7
132
133 7 ▶ if ( $op eq "+" ) { $_[0]->{number} = $op1 + $op2; }
Branch: 6 branches
BranchTrueFalseTotal
if ($op eq '+') { } 2 5 7
elsif ($op eq '-') { } 1 4 5
elsif ($op eq '*') { } 3 1 4
elsif ($op eq '/') { } 1 0 1
elsif ($op eq '%') { } 0 0 0
elsif ($op eq '**') { } 0 0 0
134 1 elsif ( $op eq "-" ) { $_[0]->{number} = $op1 - $op2; }
135 3 elsif ( $op eq "*" ) { $_[0]->{number} = $op1 * $op2; }
136 1 elsif ( $op eq "/" ) { $_[0]->{number} = $op1 / $op2; }
137 0 elsif ( $op eq "%" ) { $_[0]->{number} = $op1 % $op2; }
138 0 elsif ( $op eq "**" ) { $_[0]->{number} = $op1 ** $op2; }
139
140 7 return $_[0];
141 }
142
143
144 sub _mutate
145 12 ▶{
Subroutine _mutate: called CC=3 SCAR=11.0
146 12 my $op = shift;
147
148 12 ▶ if ( $op eq "++" ) { ++ $_[0]->{number} }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq '++') { } 6 6 12
elsif ($op eq '--') { } 6 0 6
149 6 elsif ( $op eq "--" ) { -- $_[0]->{number} }
150 }
151
152 sub _compare
153 8 ▶{
Subroutine _compare: called CC=9 SCAR=22.1
154 8 my $op = shift;
155
156 8 ▶ my $op1 = ref( $_[0] ) ? $_[0]->{number} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 8 0 8
157 8 ▶ my $op2 = ref( $_[1] ) ? $_[1]->{number} : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 8 0 8
158
159 8 ▶ if ( $op eq "<" ) { return $op1 < $op2; }
Branch: 6 branches
BranchTrueFalseTotal
if ($op eq '<') { } 1 7 8
elsif ($op eq '>') { } 1 6 7
elsif ($op eq '<=') { } 2 4 6
elsif ($op eq '>=') { } 2 2 4
elsif ($op eq '==') { } 1 1 2
elsif ($op eq '!=') { } 1 0 1
160 1 elsif ( $op eq ">" ) { return $op1 > $op2; }
161 2 elsif ( $op eq "<=" ) { return $op1 <= $op2; }
162 2 elsif ( $op eq ">=" ) { return $op1 >= $op2; }
163 1 elsif ( $op eq "==" ) { return $op1 == $op2; }
164 1 elsif ( $op eq "!=" ) { return $op1 != $op2; }
165 }
166
167 sub _numsort
168 10 ▶{
Subroutine _numsort: called CC=3 SCAR=11.7
169 10 ▶ my $op1 = ref( $_[0] ) ? $_[0]->{number} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 10 0 10
170 10 ▶ my $op2 = ref( $_[1] ) ? $_[1]->{number} : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 10 0 10
171
172 10 return $op1 <=> $op2;
173 }
174
175 sub _function
176 6 ▶{
Subroutine _function: called CC=8 SCAR=20.8
177 6 my $op = shift;
178
179 6 ▶ my $op1 = ref( $_[0] ) ? $_[0]->{number} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 6 0 6
180
181 6 my $number = { %{$_[0]} };
182
183 6 ▶ if ( $op eq "sqrt" ) { $number->{number} = sqrt($op1); }
Branch: 6 branches
BranchTrueFalseTotal
if ($op eq 'sqrt') { } 1 5 6
elsif ($op eq 'abs') { } 1 4 5
elsif ($op eq 'cos') { } 1 3 4
elsif ($op eq 'sin') { } 1 2 3
elsif ($op eq 'exp') { } 1 1 2
elsif ($op eq 'log') { } 1 0 1
184 1 elsif ( $op eq "abs" ) { $number->{number} = abs($op1); }
185 1 elsif ( $op eq "cos" ) { $number->{number} = cos($op1); }
186 1 elsif ( $op eq "sin" ) { $number->{number} = sin($op1); }
187 1 elsif ( $op eq "exp" ) { $number->{number} = exp($op1); }
188 1 elsif ( $op eq "log" ) { $number->{number} = log($op1); }
189
190 6 return bless $number, 'Number::Format::Calc';
191 }
192
193 sub number
194 1 ▶{
Subroutine number: called CC=1 SCAR=0.0
Pod documented
195 1 return $_[0]->{number};
196 }
197
198 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
199 sub fmod
200 2 ▶{
Subroutine fmod: called CC=3 SCAR=11.0
Pod documented
201 2 ▶ my $op1 = ref( $_[0] ) ? $_[0]->{number} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 0 2
202 2 ▶ my $op2 = ref( $_[1] ) ? $_[1]->{number} : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 1 1 2
203
204 2 my $number = { %{$_[0]} };
205
206 2 $number->{number} = $op1-(int($op1/$op2)*$op2);
207
208 2 return bless $number, 'Number::Format::Calc';
209 }
210
211 4 1;
212
213
214 =head1 NAME
215
216 Number::Format::Calc
217
218 =head1 SYNOPSIS
219
220 use Number::Format::Calc (%args);
221 $n = new Number::Format::Calc ('1.234,5', %args );
222
223 =head1 DESCRIPTION
224
225 This module makes calculations with formatted numbers transparent.
226
227 All arithmetric operators and and some arithmetric functions (I<sqrt, abs, cos, sin, exp, log>) are overloaded.
228
229 =head1 METHODS
230
231 =head2 new ($self, $number, %args)
232
233 The constructor awaits the formatted number-string as the first argument,
234 and a hash with the same formatting-options as in Number::Format.
235
236 The same arguments can be passed via the C<use Number::Format::Calc (%args);>-statement and will then serve as defaults
237 for all instances of Number::Format::Calc-objects.
238
239 =head2 number ($self)
240
241 This method returns the number without formats.
242
243 =head2 fmod ($self, $foo)
244
245 This method returns the result of a floating-point modulo operation from $self->number modulo $foo.
246
247 =head1 Examples
248
249 use Number::Format::Calc;
250
251 my $n = new Number::Format::Calc ( '1.111,5' , -thousands_sep=>".", -decimal_point=>",", decimal_digits=>1 );
252 my $m = new Number::Format::Calc ( '2.222,35' , -thousands_sep=>".", -decimal_point=>",", decimal_digits=>2 );
253
254 #add 10 to the object
255 print $n + 10, "\n"; #1.121,5;
256
257 #When two objects are involved, the settings of the left object win:
258 print $n + $m, "\n"; #3.333,9;
259 print $m + $n, "\n"; #3.333,85;
260
261 #modulo operation
262 print $n % 9, "\n"; #4
263
264 #floating-point modulo operation
265 print $n->fmod(9), "\n"; #4.5
266
267 #Get plain number
268 print $n->number; #1111.5
269
270 More examples can be found in the test-files (*.t) that come with this module.
271
272 ########################################################################
273
274 #using defaults
275 use Number::Format::Calc ( -thousands_sep=>".", -decimal_point=>",", -decimal_digits=>2, -decimal_fill => 1 );
276
277 my $n = new Number::Format::Calc ('1.111,5');
278 print $n; #1.111,50
279
280
281 =head1 PREREQUISITIES
282
283 Number::Format
284 Test::Simple
285
286 =head1 BUGS
287
288 None that I know of. If you find one, or a missing test-case, let me know.
289
290 =head1 AUTHOR
291
292 Markus Holzer
293 CPAN ID: HOLLIHO
294 HOLLIHO@gmx.de
295 http://holli.perlmonk.org
296
297 You can also reach me via the chatterbox at L<http://www.perlmonks.org>
298
299 =head1 COPYRIGHT
300
301 This program is free software licensed under the...
302
303 The General Public License (GPL)
304 Version 2, June 1991
305
306 The full text of the license can be found in the
307 LICENSE file included with this module.
308
309
310 =head1 SEE ALSO
311
312 perl(1).
313
314 =cut
315
316 ############################################# main pod documentation end ##
↑ Summary