| 1 | package Math::Business::SMA; | ||||||||||
| 2 | |||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 6 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 6 | 1; | |||||||||
| 8 | |||||||||||
| 9 | 0 | ▶ | sub tag { (shift)->{tag} } | ||||||||
|
Subroutine
tag: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 10 | |||||||||||
| 11 | 0 | ▶ | sub recommended { croak "no recommendation" } | ||||||||
|
Subroutine
recommended: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 12 | |||||||||||
| 13 | sub new { | ||||||||||
| 14 | 14 | ▶ | my $class = shift; | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 15 | 14 | my $this = bless { | |||||||||
| 16 | val => [], | ||||||||||
| 17 | cur => undef, | ||||||||||
| 18 | rec => 1, | ||||||||||
| 19 | }, $class; | ||||||||||
| 20 | |||||||||||
| 21 | 14 | my $days = shift; | |||||||||
| 22 | 14 | ▶ | if( defined $days ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 11 | $this->set_days( $days ); | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | 14 | return $this; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | sub set_days { | ||||||||||
| 30 | 15 | ▶ | my $this = shift; | ||||||||
|
Subroutine
set_days: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 31 | 15 | my $arg = int(shift); | |||||||||
| 32 | |||||||||||
| 33 | 15 | ▶ | croak "days must be a positive non-zero integer" if $arg <= 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | |||||||||||
| 35 | 15 | $this->{val} = []; | |||||||||
| 36 | 15 | delete $this->{SMA}; | |||||||||
| 37 | 15 | $this->{days} = $arg; | |||||||||
| 38 | 15 | $this->{tag} = "SMA($this->{days})"; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | sub insert { | ||||||||||
| 42 | 226 | ▶ | my $this = shift; | ||||||||
|
Subroutine
insert: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||
| 43 | 226 | my $val = $this->{val}; | |||||||||
| 44 | 226 | my $N = $this->{days}; | |||||||||
| 45 | |||||||||||
| 46 | 226 | ▶ | croak "You must set the number of days before you try to insert" if not defined $N; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | |||||||||||
| 48 | 226 | ▶ | while( defined(my $PM = shift) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 227 | push @$val, $PM; | |||||||||
| 50 | |||||||||||
| 51 | 227 | ▶ | if( @$val >= $N ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 147 | ▶ | if( defined( my $s = $this->{SMA} ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 133 | my $old = shift @$val; | |||||||||
| 54 | 133 | $this->{SMA} = $s - $old/$N + $PM/$N; | |||||||||
| 55 | |||||||||||
| 56 | } else { | ||||||||||
| 57 | 14 | my $sum = 0; | |||||||||
| 58 | 14 | $sum += $_ for @$val; | |||||||||
| 59 | |||||||||||
| 60 | 14 | $this->{SMA} = $sum/$N; | |||||||||
| 61 | } | ||||||||||
| 62 | } | ||||||||||
| 63 | } | ||||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | sub query { | ||||||||||
| 67 | 272 | ▶ | my $this = shift; | ||||||||
|
Subroutine
query: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 68 | |||||||||||
| 69 | 272 | return $this->{SMA}; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | __END__ | ||||||||||