blib/lib/Acme/Numbers.pm

filter: Statementstmt 96.3% 80 / 83 Branchbran 91.1% 31 / 34 Conditioncond 90.6% 29 / 32 MC/DCmcdc 64.7% 11 / 17 Subroutinesub 80.0% 12 / 15 Podpod 100% 5 / 5 totaltotal 90.3% 168 / 186 CC 30 SCAR 18.6
CC
30
Cov
94%
handle
12.0
AUTOLOAD
8.1
value
6.0
CRAP 6.4
↑ Summary
1 package Acme::Numbers;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use Lingua::EN::Words2Nums qw(words2nums);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 our $AUTOLOAD;
5 6 our $VERSION = '1.2';
6  
7  
8 =head1 NAME
9  
10 Acme::Numbers - a fluent numeric interface
11  
12 =head1 SYNOPSIS
13  
14 use Acme::Numbers;
15  
16 print one."\n"; # prints 1
17 print two.hundred."\n"; # prints 200
18 print forty.two."\n"; # prints 42
19 print six.hundred.and.sixty.six."\n"; # prints 666
20 print one.million."\n"; # prints 1000000
21  
22 print three.point.one.four."\n"; # prints 3.14
23 print one.point.zero.two."\n"; # prints 1.02
24 print zero.point.zero.five."\n"; # prints 0.05
25  
26 print four.pounds."\n"; # prints "4.00"
27 print four.pounds.five."\n"; # prints "4.05"
28 print four.pounds.fifty."\n"; # prints "4.50"
29 print four.pounds.fifty.five."\n"; # prints "4.55"
30  
31 print fifty.pence."\n"; # prints "0.50"
32 print fifty.five.pence."\n"; # prints "0.55"
33 print four.pounds.fifty.pence."\n"; # prints "4.50"
34 print four.pounds.and.fifty.p."\n"; # prints "4.50"
35  
36 print fifty.cents."\n"; # prints "0.50"
37 print fifty.five.cents."\n"; # prints "0.55"
38 print four.dollars.fifty.cents."\n"; # prints "4.55"
39  
40
41  
42 =head1 DESCRIPTION
43  
44 Inspired by this post
45  
46 http://beautifulcode.oreillynet.com/2007/12/the_cardinality_of_a_fluent_in.php
47  
48 and a burning curiosity. At leats, I hope the burning
49 was curiosity.
50  
51 =head1 ONE BIIIIIIIIIIIILLION
52  
53 By default billion is 10**12 because, dammit, that's right.
54  
55 If you want it to be an American billion then do
56  
57 use Acme::Numbers billion => 10**9;
58  
59 Setting this automatically changes all the larger numbers
60 (trillion, quadrillion, etc) to match.
61  
62 =head1 METHODS
63  
64 You should never really use these methods on the class directly.
65  
66 All numbers handled by C<Lingua::EN::Words2Nums> are handled by this module.
67  
68 In addition ...
69  
70 =cut
71  
72 5 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
73 5 my $class = shift;
74 5 my %opts = @_;
75  
76 5 ▶ $opts{billion} = 10**12 unless defined $opts{billion};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"billion"} 4 1 5
77 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
78 6 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
79 5 my ($pkg, $file) = caller;
80 5 $Lingua::EN::Words2Nums::billion = $opts{billion};
81 5 foreach my $num ((keys %Lingua::EN::Words2Nums::nametosub,
82 'and', 'point', 'zero',
83 'pound', 'pounds', 'pence', 'p',
84 'dollars', 'cents'))
85 {
86 600 ▶ *{"$pkg\::$num"} = sub { $class->$num };
Subroutine __ANON__: called CC=1 SCAR=0.0
87 }
88 };
89  
90  
91  
92 =head2 new <value> <operator>
93  
94 C<operator> can be 'num', 'and' or 'point'
95  
96 =cut
97  
98 191 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
99 191 my $class = shift;
100 191 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 62 129 191
101 191 my $val = shift;
102 191 my $op = shift;
103 191 ▶ my $name = shift || $op;
Condition66%2/3
shift() || $op
l!l&&r!l&&!r
102890
MC/DC0%0/2
shift() || $op
shift() $op
Truth table66%2/3
A || B
A= shift()B= $op
ABresult
1X1
011
000
104 191 bless { value => $val, operator => $op, name => $name }, $class;
105 }
106  
107 =head2 name
108  
109 The name of this object (i.e the method that was originally called).
110  
111 =cut
112  
113 0 ▶sub name {
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
114 0 return $_[0]->{name};
115 }
116  
117 =head2 value
118  
119 The current numeric value
120  
121 =cut
122  
123 38 ▶sub value {
Subroutine value: called CC=6 SCAR=17.9
Pod documented
124 38 my $self = shift;
125 38 my $val = $self->{value};
126 # if we're 'pence' then divide by 100 and then pretend we're pounds
127 38 ▶ if ($self->{operator} =~ m!^p(ence)?$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"operator"} =~ /^p(ence)?$/) 6 32 38
128 # this fixes something where there's 0
129 # pounds and a trailing zero like 0.50
130 6 $self->{last_added} = $val;
131 6 $val = $val/100;
132 6 $self->{operator} = 'pounds';
133 }
134 38 ▶ if ($self->{operator} =~ m!^pounds?$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"operator"} =~ /^pounds?$/) 22 16 38
135 22 my ($num, $frac) = split /\./, $val;
136 22 ▶ $frac ||= 0;
Condition100%2/2
$frac ||= 0
l!l
202
MC/DC100%1/1
$frac ||= 0
$frac
Truth table100%2/2
A
A= $frac
Aresult
11
00
137 # this also fixes 0 pounds trailing zero
138 22 ▶ $frac = $self->{last_added} if defined $self->{last_added} && $self->{last_added}>$frac;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"last_added"} and $self->{"last_added"} > $frac 3 19 22
Condition100%3/3
defined $self->{"last_added"} and $self->{"last_added"} > $frac
!ll&&!rl&&r
2173
MC/DC100%2/2
defined $self->{"last_added"} and $self->{"last_added"} > $frac
defined $self->{"last_added"} $self->{"last_added"} > $frac
Truth table100%3/3
A and B
A= defined $self->{"last_added"}B= $self->{"last_added"} > $frac
ABresult
0X0
100
111
139 # we substr to fix one.pound.fifty.pence which
140 # leaves $frac as '500'
141 22 $val = sprintf("%d.%02d",$num,substr($frac,0,2));
142 }
143  
144 38 return $val;
145 }
146  
147 129 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=8 SCAR=20.9
148 129 my $self = shift;
149 129 my $method = $AUTOLOAD;
150 129 $method =~ s/.*://; # strip fully-qualified portion
151 129 my $val;
152 # nasty override - we should probably have a
153 # generic major or minor currency indicator
154 # if we could store and propogate the currency
155 # then we could also throw errors at mismatched
156 # units e.g five.pounds.and.fifty.cents
157 # but maybe also print out the correct sigil
158 # e.g $5.50
159 129 ▶ $method = 'pounds' if $method eq 'dollars';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "dollars" 2 127 129
160 129 ▶ $method = 'pence' if $method eq 'cents';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "cents" 4 125 129
161  
162 # dummy methods
163 129 ▶ if ($method eq 'and' || $method =~ m!^p!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'and' or $method =~ /^p/) { } 43 86 129
Condition100%3/3
$method eq "and" or $method =~ /^p/
l!l&&r!l&&!r
53886
MC/DC100%2/2
$method eq "and" or $method =~ /^p/
$method eq "and" $method =~ /^p/
Truth table100%3/3
A or B
A= $method eq "and"B= $method =~ /^p/
ABresult
1X1
011
000
164 43 $val = $self->new(0, $method)
165 } else {
166 # bit of a hack here
167 86 ▶ my $tmp = ($method eq 'zero')? 0 : words2nums($method);
Branch: 1 branch
BranchTrueFalseTotal
$method eq 'zero' ? : 4 82 86
168 # maybe this should die
169 86 ▶ return unless defined $tmp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tmp 0 86 86
170 86 $val = $self->new($tmp, 'num', $method);
171 }
172  
173 # If we're the first number in the chain
174 # then just return ourselves
175 129 ▶ if (!ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $self) { } 129 0 129
176 129 return $val;
177 } else {
178 # Otherwise do the magic
179 0 return $self->handle($val);
180 }
181 }
182  
183 =head2 handle <Acme::Numbers>
184  
185 Handle putting these two objects together
186  
187 =cut
188  
189 91 ▶sub handle {
Subroutine handle: called CC=12 SCAR=24.9
Pod documented
190 91 my ($self, $val) = @_;
191 # If we haven't passed a pounds, pence or point marker
192 91 ▶ if ($self->{operator} !~ m!^p!) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'operator'} =~ /^p/) { } 45 46 91
193 # If the new object is marker ...
194 45 ▶ if ($val->{operator} =~ m!^p!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val->{'operator'} =~ /^p/) { } 29 16 45
195 # ... Just propogate along but make a note
196 # A pound should not be overidden by a pence
197 29 ▶ $self->{operator} = $val->{operator} unless $self->{operator} =~ m!^pounds?$!;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"operator"} =~ /^pounds?$/ 29 0 29
198 29 return $self;
199  
200 # Otherwise ...
201 } else {
202 16 my $val = $val->{value};
203 # If we're not currently adding and the new more than the old
204 # e.g two.hundred then multiply
205 16 ▶ if ($self->{value} < $val && $self->{operator} ne 'add') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'value'} < $val and $self->{'operator'} ne 'add') { } 7 9 16
Condition66%2/3
$self->{"value"} < $val and $self->{"operator"} ne "add"
!ll&&!rl&&r
907
MC/DC50%1/2
$self->{"value"} < $val and $self->{"operator"} ne "add"
$self->{"value"} < $val $self->{"operator"} ne "add"
Truth table66%2/3
A and B
A= $self->{"value"} < $valB= $self->{"operator"} ne "add"
ABresult
0X0
100
111
206 7 $val *= $self->{value};
207 # Otherwise add
208 } else {
209 9 $val += $self->{value};
210 }
211 16 return $self->new($val, 'num', $self->{operator});
212 }
213 } else { # point, pound, pence
214 # first get the fractional part
215 46 my ($num, $frac) = split /\./, $self->{value};
216 #$frac ||= 0;
217 # Cope with four.point.zero.four
218 46 ▶ if ((defined $frac && $frac>0 && $frac<10) || $val->{value} == 0 || (defined $self->{last_added} and $self->{last_added} eq '0')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $frac and $frac > 0 and $frac < 10 or $val->{'value'} == 0 or defined $self->{'last_added'} and $self->{'last_added'} eq '0') { } 21 25 46
Condition100%15/15
defined $frac and $frac > 0 and $frac < 10 or $val->{"value"} == 0 or defined $self->{"last_added"} and $self->{"last_added"} eq 0
l!l&&r!l&&!r
15625
defined $frac and $frac > 0 and $frac < 10 or $val->{"value"} == 0
l!l&&r!l&&!r
21331
defined $frac and $frac > 0 and $frac < 10
!ll&&!rl&&r
26182
defined $frac and $frac > 0
!ll&&!rl&&r
21520
defined $self->{"last_added"} and $self->{"last_added"} eq 0
!ll&&!rl&&r
1696
MC/DC66%4/6
defined $frac and $frac > 0 and $frac < 10 or $val->{"value"} == 0 or defined $self->{"last_added"} and $self->{"last_added"} eq 0
defined $frac $frac > 0 $frac < 10 $val->{"value"} == 0 defined $self->{"last_added"} $self->{"last_added"} eq 0
Truth table53%7/13
A and B and C or D or E and F
A= defined $fracB= $frac > 0C= $frac < 10D= $val->{"value"} == 0E= defined $self->{"last_added"}F= $self->{"last_added"} eq 0
ABCDEFresult
111XXX1
0XX1XX1
10X1XX1
1101XX1
0XX0111
10X0111
1100111
0XX00X0
0XX0100
10X00X0
10X0100
11000X0
1100100
219 21 $frac .= $val->{value};
220 } else {
221 25 $frac += $val->{value};
222 }
223 # Create the new object
224 46 my $new = $self->new("${num}.${frac}", $self->{operator});
225 # We use this to be able to do point.fifty.five and point.five.five
226 46 $new->{last_added} = $val->{value};
227 46 return $new;
228 }
229 }
230  
231 =head2 concat <value>
232  
233 Concatenate two things.
234  
235 =cut
236  
237 129 ▶sub concat {
Subroutine concat: called CC=3 SCAR=11.0
Pod documented
238 129 my ($self, $new) = @_;
239 129 my $class = shift;
240 # If both objects are special numbers handle them
241 129 ▶ if (ref($new) && $new->isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new and $new->isa(__PACKAGE__)) { } 91 38 129
Condition66%2/3
ref $new and $new->isa(__PACKAGE__)
!ll&&!rl&&r
38091
MC/DC50%1/2
ref $new and $new->isa(__PACKAGE__)
ref $new $new->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= ref $newB= $new->isa(__PACKAGE__)
ABresult
0X0
100
111
242 91 return $self->handle($new);
243 # Otherwise stringify both and concat
244 } else {
245 38 return $self->value.$new;
246 }
247 }
248  
249 0 ▶sub _bool {
Subroutine _bool: not called CC=1 SCAR=6.9
250 0 my ($self, $new, $op) = @_;
251 }
252  
253 6 ▶use overload '""' => 'value',
Subroutine BEGIN: called CC=1 SCAR=0.0
254 '+0' => 'value',
255 6 '.' => 'concat';
256 # 'bool' => 'bool';
257  
258  
259 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
260  
261 6 1;
↑ Summary