blib/lib/Math/Financial.pm

filter: Statementstmt 17.7% 40 / 226 Branchbran 0.0% 0 / 112 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 45.8% 11 / 24 Podpod 90.0% 9 / 10 totaltotal 15.7% 60 / 382 CC 61 SCAR 43.3
CC
61
Cov
12%
compound_interest
210.0
simple_interest
156.0
_get_attribute_key
72.0
CRAP 75.7
↑ Summary
1 package Math::Financial;
2  
3 # Copyright 1999 Eric Fixler <fix@fixler.com>
4 # All rights reserved. This program is free software;
5 # you can redistribute it and/or modify it under the same terms as Perl itself.
6  
7 # $Id: Financial.pm,v 1.5 1999/09/15 19:08:41 fix Exp $
8 # $Source: /www/cgi/lib/Math/RCS/Financial.pm,v $
9  
10 =pod
11  
12 =head1 NAME
13  
14 Math::Financial - Calculates figures relating to loans and annuities.
15  
16 =head1 SYNOPSIS
17  
18 $calc = new Math::Financial(fv =E<gt> 100000, pv =E<gt> 1000);
19 $calc-E<gt>set->(pmt => 500, ir => 8);
20  
21 $calc->compound_interest(find =E<gt> 'fv');
22  
23 =head1 DESCRIPTION
24  
25 This package contains solves mathematical problems relating to loans and annuities.
26  
27 The attributes that are used in the equations may be set on a per-object basis, allowing
28 you to run a set of different calculations using the same numbers, or they may be fed
29 directly to the methods.
30  
31 The attribute types, accessed through the C<get> and C<set> methods are
32  
33 =over4
34  
35 =item pv =E<gt> Present Value
36  
37 =item fv =E<gt> Future Value
38  
39 =item ir =E<gt> Yearly Interest Rate (in percent)
40  
41 =item pmt =E<gt> Payment Amount
42  
43 =item np =E<gt> Number of Payments/Loan Term
44  
45 =item tpy =E<gt> Terms Per Year (defaults to 12)
46  
47 =item pd =E<gt> Payments made so far (used only for loan/annuity balances)
48  
49 =back
50  
51 Attributes are case-insensitive. The documentation for the individual methods
52 indicates which attributes must be set for those methods.
53  
54 Calculations are based B<either> on the attributes set with the C<new> or C<set>
55 methods, B<or> with arguments fed directly to the methods. This seemed like the
56 least confusing way to make the interface flexible for people who are using the
57 module in different ways.
58  
59 Also, performing a calculation
60 does B<not> update the attribute of the solution. In other words, if
61 you solve an equation that returns fv, the solution is returned but the
62 internal fv field is unaffected.
63  
64 Any attempted calculation which cannot be completed -- due to either missing or
65 invalid attributes -- will return C<undef>.
66  
67 I am interested to hear from people using this module -- let me know what
68 you think about the interface and how it can be improved.
69  
70 =head1 METHODS
71  
72 =cut
73  
74 1 ▶sub BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
75 1 *{__PACKAGE__.'::loan_payment'} = \&monthly_payment;
76 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 1 ▶ use POSIX qw(:ctype_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
78 1 ▶ use vars qw(@ISA $VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS
Subroutine BEGIN: called CC=1 SCAR=0.0
79 1 @ATTRIBUTES $DEFAULT_OBJECT $re_object);
80 1 $VERSION = 0.76;
81 1 ▶ use constant PV => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 1 ▶ use constant FV => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 1 ▶ use constant NP => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 1 ▶ use constant PMT => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 1 ▶ use constant IR => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 1 ▶ use constant TPY => 5; # TERMS PER YEAR
Subroutine BEGIN: called CC=1 SCAR=0.0
87 1 ▶ use constant PD => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 1 @ATTRIBUTES = qw(PV FV NP PMT IR TPY PD);
89 1 $re_object = '(?i)[a-z][\w]*?::[\w]';
90 1 @ISA = qw(Exporter);
91 1 @EXPORT= ();
92 1 @EXPORT_OK = qw(loan_term loan_payment compound_interest funding_annuity
93 loan_balance loan_size simple_interest);
94 1 %EXPORT_TAGS = ( procedural => \@EXPORT_OK,
95 standard => \@EXPORT_OK);
96 }
97  
98  
99 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
100 =pod
101  
102 =head2 new
103  
104 C<$calc = new Math::Financial();
105  
106 C<$calc = new Math::Financial(pv =E<gt> 10000, ir =E<gt> 5, np => 12)>
107  
108 Object constructor. See above for a description of the available attributes.
109 You do not I<have> to set attributes here, you can also do so using C<set>,
110 or feed attributes directly to the methods.
111  
112 There are no default values for any of the attributes except C<TPY> (Terms Per Year),
113 which is 12 by default, and C<PD> which defaults to zero.
114  
115 If you don't want to use the object-oriented interface, see the L<EXPORTS> section
116 below.
117  
118 =cut
119 0 ▶ my $class = ref($_[0]) || ($_[0] =~ /(.*?::.*)/)[0];
Condition0%0/3
ref $_[0] || ($_[0] =~ /(.*?::.*)/)[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] || ($_[0] =~ /(.*?::.*)/)[0]
ref $_[0] ($_[0] =~ /(.*?::.*)/)[0]
Truth table0%0/3
A || B
A= ref $_[0]B= ($_[0] =~ /(.*?::.*)/)[0]
ABresult
1X1
011
000
120 0 ▶ my $parent = ref($class) ? $_[0] : [undef,undef,undef,undef,undef,12,0] ;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 0 0
121 0 ▶ if ($class) { shift(@_); } else { $class = __PACKAGE__ ; };
Branch: 1 branch
BranchTrueFalseTotal
if ($class) { } 0 0 0
122 0 my $params = { pv => $parent->[PV],
123 fv => $parent->[FV],
124 ir => $parent->[IR],
125 np => $parent->[NP],
126 pmt => $parent->[PMT],
127 tpy => $parent->[TPY],
128 pd => $parent->[PD],
129 @_ };
130 0 my $self = [];
131 0 bless($self,$class);
132 0 $self->set(%$params);
133 0 return $self;
134 }
135  
136  
137 0 ▶sub _get_attribute_key {
Subroutine _get_attribute_key: not called CC=8 SCAR=42.8
138 # if fed a list, will return a list
139 0 my ($self,@args) = _get_self(@_);
140 0 ▶ return undef unless scalar(@args);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @args 0 0 0
141 0 my @keys = ();
142 0 foreach (@args) {
143 0 ▶ if (isdigit($_)) { push(@keys,$_); next; };
Branch: 1 branch
BranchTrueFalseTotal
if (isdigit($_)) 0 0 0
144 0 my $attrib = quotemeta($_);
145 0 ▶ for (my $j = 0; $j <= $#ATTRIBUTES; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $#ATTRIBUTES) 0 0 0
146 0 ▶ if ($ATTRIBUTES[$j] =~ /$attrib/i) { push(@keys,$j); next; };
Branch: 1 branch
BranchTrueFalseTotal
if ($ATTRIBUTES[$j] =~ /$attrib/i) 0 0 0
147 };
148 0 push(@keys,undef); #unfound key
149 }
150 0 ▶ if (not($#args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $#args) { } 0 0 0
151 0 return $keys[0];
152 } else {
153 0 ▶ return wantarray ? @keys : \@keys;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
154 };
155 };
156  
157 0 ▶sub set {
Subroutine set: not called CC=3 SCAR=24.8
Pod documented
158 =pod
159  
160 =head2 set
161  
162 C<$calc-E<gt>set(fv =E<gt> 100000, pmt =E<gt> 500)>
163  
164 You can set any of the stored attributes using this method, which is is also
165 called by <new>. Returns the number of attributes set.
166  
167 =cut
168 0 my ($self,@args) = _get_self(@_);
169 0 my $params = { @args };
170 0 my ($field,$val,$key); my $count = 0;
171 0 ▶ while (($field, $val) = each(%$params)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$params) 0 0 0
172 0 $key = $self->_get_attribute_key($field);
173 0 ▶ if (defined($key)) { $self->[$key] = $val; $count++; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 0 0 0
174 }
175 0 return $count;
176 }
177  
178 0 ▶sub get {
Subroutine get: not called CC=7 SCAR=40.3
Pod documented
179 =pod
180  
181 =head2 get
182  
183 C<$calc-E<gt>get(field => 'ir')>
184  
185 C<$calc-E<gt>get('ir','pmt','pv')>
186  
187 C<$calc-E<gt>get([qw(ir pmt pv)])>
188  
189 You can get one or several attributes using this method. In the multiple
190 attribute formats, it accepts either a list or a list reference as input.
191  
192 In single attribute context, returns a scalar. In multiple attribute context,
193 it returns a list or a reference to a list, depending on the calling context.
194  
195 =cut
196 0 my ($self,@args) = _get_self(@_);
197 0 ▶ ($args[0] =~ /field/io) and shift(@args);
Branch: 1 branch
BranchTrueFalseTotal
$args[0] =~ /field/io and shift @args 0 0 0
198 0 my @gets = ();
199 0 foreach my $field (@args) {
200 0 ▶ if (ref($field) eq 'ARRAY') { push(@gets,map({ $self->get($_) } @$field)) ; next; }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $field eq 'ARRAY') { } 0 0 0
201 0 else { my $key = $self->_get_attribute_key($field);
202 0 ▶ push(@gets, defined($key) ? $self->[$key] : $key); next; }
Branch: 1 branch
BranchTrueFalseTotal
defined $key ? : 0 0 0
203 }
204 0 ▶ if ($#gets) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#gets) { } 0 0 0
205 0 ▶ return wantarray ? @gets : \@gets;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
206 0 } else { return $gets[0]; };
207 }
208  
209  
210 0 ▶sub compound_interest {
Subroutine compound_interest: not called CC=14 SCAR=53.5
Pod documented
211 =pod
212  
213 =head2 compound_interest
214  
215 C<$calc-E<gt>compound_interest>
216  
217 C<$calc-E<gt>compound_interest-E<gt>('fv')>
218  
219 C<$calc-E<gt>compound_interest-E<gt>(find =E<gt> 'fv')>
220  
221 Calculates compund interest for an annuity. With any 3 of pv, fv, np, and ir,
222 you can always solve the fourth.
223  
224 Without arguments, the method will attempt to figure out what you'd like to solve
225 based on what attributes of the object are defined. Usually, you'll probably want to
226 explicitly request what attribute you'd like returned, which you can do using
227 the second or third method.
228  
229 =cut
230 0 my ($self,@args) = _get_self(@_);
231 0 ▶ (scalar(@args) == 1) and unshift(@args,'find');
Branch: 1 branch
BranchTrueFalseTotal
scalar @args == 1 and unshift @args, "find" 0 0 0
232 0 ▶ if (scalar(@args) > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args > 2) 0 0 0
233 0 my $temp = __PACKAGE__->new(@args[2..$#args]);
234 0 return $temp->compound_interest(@args[0..1]);
235 };
236 0 my $solve_for = $self->_get_attribute_key($args[1]);
237 0 my (@numbers,$result);
238 0 ▶ if (not(defined($solve_for))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $solve_for) { } 0 0 0
239 0 ▶ if (@numbers = $self->_verify_fields(IR,PV,NP)) { $solve_for = FV; }
Branch: 4 branches
BranchTrueFalseTotal
if (@numbers = $self->_verify_fields(4, 0, 2)) { } 0 0 0
elsif (@numbers = $self->_verify_fields(4, 1, 2)) { } 0 0 0
elsif (@numbers = $self->_verify_fields(4, 0, 1)) { } 0 0 0
elsif (@numbers = $self->_verify_fields(0, 1, 2)) { } 0 0 0
240 0 elsif (@numbers = $self->_verify_fields(IR,FV,NP)) { $solve_for = PV; }
241 0 elsif (@numbers = $self->_verify_fields(IR,PV,FV)) { $solve_for = NP; }
242 0 elsif (@numbers = $self->_verify_fields(PV,FV,NP)) { $solve_for = IR; }
243 0 else { return undef; };
244 } else {
245 0 my @combos = ();
246 0 $combos[FV] = [IR,PV,NP]; $combos[PV] = [IR,FV,NP]; $combos[NP] = [IR,PV,FV];
247 0 $combos[IR] = [PV,FV,NP];
248 0 $set = $combos[$solve_for];
249 0 ▶ @numbers = $self->_verify_fields(@$set) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
@numbers = $self->_verify_fields(@$set) or return undef 0 0 0
250 }
251 0 ▶ eval {if ($solve_for == FV) {
Branch: 4 branches
BranchTrueFalseTotal
if ($solve_for == 1) { } 0 0 0
elsif ($solve_for == 0) { } 0 0 0
elsif ($solve_for == 2) { } 0 0 0
elsif ($solve_for == 4) { } 0 0 0
252 0 $ir = ($numbers[0]/100) / $self->[TPY];
253 0 ($pv,$np) = @numbers[1,2];
254 0 $result = abs($pv) * ( ($ir + 1) ** $np);
255 } elsif ($solve_for == PV) {
256 0 $ir = ($numbers[0]/100) / $self->[TPY];
257 0 ($fv,$np) = @numbers[1,2];
258 0 $result = abs($fv) * ( ($ir + 1) ** (0 - $np) );
259 } elsif ($solve_for == NP) {
260 0 $ir = $numbers[0]/100/$self->[TPY];
261 0 ($pv,$fv) = @numbers[1,2];
262 0 my $num = log(abs($fv)/$pv);
263 0 my $den = log( 1 + $ir);
264 0 $result = $num / $den;
265 } elsif ($solve_for == IR) {
266 0 ($pv,$fv,$np) = @numbers;
267 0 $ir = (( abs($fv)/abs($pv) ) ** (1 / $np) ) - 1;
268 0 $result = $ir * 100 * $self->[TPY];
269 };};
270
271 0 ▶ return ($@) ? undef : $result;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
272 }
273  
274 0 ▶sub funding_annuity {
Subroutine funding_annuity: not called CC=4 SCAR=30.0
Pod documented
275 =pod
276  
277 =head2 funding_annuity
278  
279 C<$calc-E<gt>funding_annuity>
280  
281 C<$calc-E<gt>funding_annuity-E<gt>(pmt =E<gt> 2000, ir =E<gt> 6.50, np =E<gt> 40, tpy => 4)>
282  
283 C<funding_annuity> calculates how much money ( C<fv> ) you will have at the end of C<np> periods
284 if you deposit C<pmt> into the account each period and the account earns C<ir> interest per year.
285  
286 You may want to set the C<tpy> attribute here to something other than 12, since, while loans
287 usually compound monthly, annuities rarely do.
288  
289 =cut
290  
291 0 my ($self,@args) = _get_self(@_);
292 0 ▶ if (scalar(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args) 0 0 0
293 0 my $temp = __PACKAGE__->new(@args);
294 0 return $temp->funding_annuity();
295 };
296 0 my @numbers = $self->_verify_fields(IR,PMT,NP);
297 0 ▶ return undef unless scalar(@numbers);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @numbers 0 0 0
298 0 my ($result); #solving for fv here
299 0 my ($pmt,$np) = @numbers[1,2];
300 0 my $ir = $numbers[0]/100/$self->[TPY];
301 0 eval { $result = ($pmt * ( ((1 + $ir) ** $np) - 1))/$ir; };
302 0 ▶ return $@ ? undef : $result;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
303 }
304  
305  
306 0 ▶sub loan_balance {
Subroutine loan_balance: not called CC=4 SCAR=30.0
Pod documented
307 =pod
308  
309 =head2 loan_balance
310  
311 C<$calc-E<gt>loan_balance>
312  
313 C<$calc-E<gt>loan_balance-E<gt>(pmt =E<gt> 2000, ir =E<gt> 6.50, np =E<gt> 360, pd =E<gt> 12)>
314  
315 C<loan_balance> calculates the balance on a loan that is being made in C<np> equal payments,
316 given that C<pd> payments have already been made. You can also use this method to determine
317 the amount of money left in an annuity that you are drawing down.
318  
319 =cut
320 0 my ($self,@args) = _get_self(@_);
321 0 ▶if (scalar(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args) 0 0 0
322 0 my $temp = __PACKAGE__->new(@args);
323 0 return $temp->loan_balance();
324 };
325 0 my @numbers = $self->_verify_fields(IR,PMT,NP);
326 0 ▶ return undef unless scalar(@numbers);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @numbers 0 0 0
327 0 my ($pmt,$np) = @numbers[1,2];
328 0 my $ir = $numbers[0]/100/$self->[TPY]; my ($result);
329 0 eval { my $a = (1 + $ir) ** ($self->[PD] - $np);
330 0 $result = $pmt/$ir * (1 - $a) ; };
331 0 ▶ return $@ ? undef : $result;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
332 }
333  
334 0 ▶sub monthly_payment {
Subroutine monthly_payment: not called CC=4 SCAR=30.0
Pod undocumented
335 =pod
336  
337 =head2 loan_payment
338  
339 C<$calc-E<gt>loan_payment>
340  
341 Return the payment amount, per period, of a loan. This is also known as amortizing.
342 The ir, np, and pv fields must be set.
343  
344 =cut
345 0 my ($self,@args) = _get_self(@_);
346 0 ▶ if (scalar(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args) 0 0 0
347 0 my $temp = __PACKAGE__->new(@args);
348 0 return $temp->monthly_payment();
349 };
350 0 my @numbers = $self->_verify_fields(IR,PV,NP);
351 0 ▶ return undef unless scalar(@numbers);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @numbers 0 0 0
352 0 my ($result,$ir);
353 0 my ($pv,$np) = @numbers[1,2];
354 0 $ir = ($numbers[0]/100) / $self->[TPY];
355 0 my $a = (1 + $ir) ** (0 - $np);
356 0 my $denominator = 1 - $a;
357 0 my $numerator = $pv * $ir;
358 0 $result = eval { $numerator / $denominator };
359 0 ▶ return $@ ? undef : $result;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
360 }
361  
362  
363 0 ▶sub loan_size {
Subroutine loan_size: not called CC=4 SCAR=30.0
Pod documented
364 =pod
365  
366 =head2 loan_size
367  
368 C<$calc-E<gt>loan_term>
369  
370 C<$calc-E<gt>loan_size-E<gt>(pmt =E<gt> 2000, ir =E<gt> 6.50, np =E<gt> 360)>
371  
372 C<loan_size> calculates the size of loan you can get based on the monthly payment
373 you can afford.
374  
375 =cut
376  
377 0 my ($self,@args) = _get_self(@_);
378 0 ▶ if (scalar(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args) 0 0 0
379 0 my $temp = __PACKAGE__->new(@args);
380 0 return $temp->loan_size();
381 };
382 0 my @numbers = $self->_verify_fields(IR,PMT,NP);
383 0 ▶ return undef unless scalar(@numbers);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @numbers 0 0 0
384 0 my ($result);
385 0 my ($pmt,$np) = @numbers[1,2];
386 0 my $ir = $numbers[0]/100/$self->[TPY];
387 0 eval { $result = ($pmt * (1 - ((1 + $ir) ** (0 - $np))))/$ir; };
388 0 ▶ return $@ ? undef : $result;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
389 };
390  
391 0 ▶sub loan_term {
Subroutine loan_term: not called CC=4 SCAR=30.0
Pod documented
392 =pod
393  
394 =head2 loan_term
395  
396 C<$calc-E<gt>loan_term>
397  
398 Return the number of payments (term) of a loan given the interest rate
399 C<ir>, payment amount C<pmt> and loan amount C<pv>. The ir, pmt, and pv fields must be set.
400  
401 =cut
402 0 my ($self,@args) = _get_self(@_);
403 0 ▶ if (scalar(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args) 0 0 0
404 0 my $temp = __PACKAGE__->new(@args);
405 0 return $temp->loan_term();
406 };
407 0 my @numbers = $self->_verify_fields(IR,PMT,PV);
408 0 ▶ return undef unless scalar(@numbers);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @numbers 0 0 0
409 0 my ($pmt, $pv) = @numbers[1,2];
410 0 $pv = abs($pv);
411 0 my $ir = $numbers[0]/100/$self->[TPY];
412 0 my ($result);
413 0 $result = eval {
414 0 my $numerator = log($pmt/($pmt - ($ir * $pv)));
415 0 my $denominator = log(1 + $ir);
416 0 return $numerator / $denominator;
417 };
418 0 ▶ return $@ ? undef : $result;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
419 }
420  
421  
422 0 ▶sub simple_interest {
Subroutine simple_interest: not called CC=12 SCAR=50.5
Pod documented
423 =pod
424  
425 =head2 simple_interest
426  
427 C<$calc-E<gt>simple_interest>
428  
429 C<$calc-E<gt>simple_interest-E<gt>('ir')>
430  
431 C<$calc-E<gt>simple_interest-E<gt>(find =E<gt> 'ir')>
432  
433 This works just like compound interest, but there is no consideration of C<np>.
434 With any 2 of pv, fv, and ir, you can always solve for the third.
435  
436 Without arguments, the method will attempt to figure out what you'd like to solve
437 based on what attributes of the object have been defined. Usually, you'll probably want to
438 explicitly request what attribute you'd like returned, which you can do using
439 the second or third method.
440  
441 =cut
442 0 my ($self,@args) = _get_self(@_);
443 0 ▶ (scalar(@args) == 1) and unshift(@args,'find');
Branch: 1 branch
BranchTrueFalseTotal
scalar @args == 1 and unshift @args, "find" 0 0 0
444 0 ▶ if (scalar(@args) > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args > 2) 0 0 0
445 0 my $temp = __PACKAGE__->new(@args[2..$#args]);
446 0 return $temp->simple_interest(@args[0..1]);
447 };
448 0 my $solve_for = $self->_get_attribute_key($args[1]);
449 0 my (@numbers,$ir,$pv,$pmt,$result);
450 0 ▶ if (not(defined($solve_for))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $solve_for) { } 0 0 0
451 0 ▶ if (@numbers = $self->_verify_fields(IR,PV)) { $solve_for = PMT; }
Branch: 3 branches
BranchTrueFalseTotal
if (@numbers = $self->_verify_fields(4, 0)) { } 0 0 0
elsif (@numbers = $self->_verify_fields(4, 3)) { } 0 0 0
elsif (@numbers = $self->_verify_fields(3, 0)) { } 0 0 0
452 0 elsif (@numbers = $self->_verify_fields(IR,PMT)) { $solve_for = PV; }
453 0 elsif (@numbers = $self->_verify_fields(PMT,PV)) { $solve_for = IR; }
454 0 else { return undef; };
455 } else {
456 0 my @combos = ();
457 0 $combos[PV] = [IR,PMT]; $combos[IR] = [PMT,PV]; $combos[PMT] = [IR,PV];
458 0 $set = $combos[$solve_for];
459 0 ▶ @numbers = $self->_verify_fields(@$set) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
@numbers = $self->_verify_fields(@$set) or return undef 0 0 0
460 }
461 # equations go here
462 0 ▶ if ($solve_for == PMT) {
Branch: 3 branches
BranchTrueFalseTotal
if ($solve_for == 3) { } 0 0 0
elsif ($solve_for == 0) { } 0 0 0
elsif ($solve_for == 4) { } 0 0 0
463 0 $result = $numbers[1] * ($numbers[0]/100);
464 } elsif ($solve_for == PV) {
465 0 eval { $result = $numbers[1]/($numbers[0]/100); };
466 } elsif ($solve_for == IR) {
467 0 eval { $result = ($numbers[0]/$numbers[1]) * 100; };
468 }
469 0 ▶ return ($@) ? undef : $result;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
470 }
471  
472 0 ▶sub _get_self {
Subroutine _get_self: not called CC=3 SCAR=24.8
473 0 ▶ my $self = (ref($_[0]) !~ /$re_object/o) ? $DEFAULT_OBJECT ||= new __PACKAGE__ : shift(@_) ;
Branch: 1 branch
BranchTrueFalseTotal
!(ref($_[0]) =~ /$re_object/o) ? : 0 0 0
Condition0%0/3
$DEFAULT_OBJECT ||= new(__PACKAGE__)
l!l&&r!l&&!r
000
MC/DC0%0/2
$DEFAULT_OBJECT ||= new(__PACKAGE__)
$DEFAULT_OBJECT new(__PACKAGE__)
Truth table0%0/3
A ||= B
A= $DEFAULT_OBJECTB= new(__PACKAGE__)
ABresult
1X1
011
000
474 0 return($self,@_);
475 }
476  
477 0 ▶sub _verify_fields {
Subroutine _verify_fields: not called CC=2 SCAR=17.9
478 0 my ($self,@args) = _get_self(@_);
479 0 my @defined = grep(/[0-9]/, @$self[@args]);
480 0 ▶ return (scalar(@defined) == scalar(@args)) ? @defined : ();
Branch: 1 branch
BranchTrueFalseTotal
scalar @defined == scalar @args ? : 0 0 0
481 }
482  
483  
484 1 1;
485  
486 __END__
↑ Summary