Sabermetrics/abstract.pm

filter: Statementstmt 71.6% 58 / 81 Branchbran 57.8% 22 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 62.9% 17 / 27 Podpod 0.0% 0 / 7 totaltotal 63.4% 97 / 153 CC 21 SCAR 18.7
CC
21
Cov
67%
AUTOLOAD
16.1
print
6.3
formula
6.0
CRAP 6.5
« Sabermetrics/League/CPB2.pm ↑ Summary Sabermetrics/Team.pm »
1 package Baseball::Sabermetrics::abstract;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 our $AUTOLOAD;
5 2 our %formula;
6  
7 #my $DEBUG = 0;
8  
9 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.6
10 # formulas are weird, can we improve it ?
11 %formula = (
12 0 ▶ pa => sub { $_->ab + $_->bb + $_->hbp + $_->sf },
Subroutine __ANON__: not called CC=1 SCAR=6.9
13 0 ▶ ta => sub { $_->h + $_->{'2b'} + $_->{'3b'} * 2 + $_->hr * 3 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
14 6 ▶ ba => sub { $_->h / $_->ab },
Subroutine __ANON__: called CC=1 SCAR=0.0
15 6 ▶ obp => sub { ($_->h + $_->bb + $_->hbp) / $_->pa },
Subroutine __ANON__: called CC=1 SCAR=0.0
16 6 ▶ slg => sub { $_->tb / $_->ab },
Subroutine __ANON__: called CC=1 SCAR=0.0
17 1 ▶ ops => sub { $_->obp + $_->slg },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 1 ▶ k_9 => sub { $_->p_so / $_->ip * 9 },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 1 ▶ bb_9 => sub { $_->p_bb / $_->ip * 9 },
Subroutine __ANON__: called CC=1 SCAR=0.0
20 1 ▶ k_bb => sub { $_->p_so / $_->p_bb },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 5 ▶ isop => sub { $_->slg - $_->ba },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 0 ▶ isod => sub { $_->obp - $_->ba },
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 0 ▶ rc => sub { $_->ab * $_->obp },
Subroutine __ANON__: not called CC=1 SCAR=6.9
24  
25 1 ▶ era => sub { $_->er / $_->ip * 9 },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 1 ▶ whip => sub { ($_->p_bb + $_->h_allowed) / $_->ip },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 0 ▶ babip => sub { ($_->h_allowed - $_->hr_allowed) / ($_->p_pa - $_->h_allowed - $_->p_so - $_->p_bb - $_->hr_allowed) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 0 ▶ g_f => sub { $_->go / $_->ao },
Subroutine __ANON__: not called CC=1 SCAR=6.9
29  
30 # rf => sub { ($_->a + $_->po) / $_->f_inn * 9 },
31 0 ▶ fpct => sub { ($_->po + $_->a) / ($_->po + $_->a + $_->e) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 2 );
33 }
34  
35 sub new
36 54 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
37 54 my ($class, $hash) = @_;
38 54 return bless \%$hash, $class;
39 }
40  
41 183 ▶sub AUTOLOAD : lvalue
Subroutine AUTOLOAD: called CC=13 SCAR=27.8
42 {
43 183 my $self = shift;
44 183 ▶ my $type = ref($self) or die;
Branch: 1 branch
BranchTrueFalseTotal
my $type = ref $self or die 183 0 183
45 183 my $name = $AUTOLOAD;
46 183 $name =~ s/.*:://;
47 183 my $ref;
48 183 my $cachename = '!'.$name . join '!', @_;
49  
50 183 ▶ if ($name eq 'DESTROY') {
Branch: 4 branches
BranchTrueFalseTotal
if ($name eq 'DESTROY') { } 0 183 183
elsif (exists $self->{$name}) { } 71 112 183
elsif (exists $self->{$cachename}) { } 82 30 112
elsif (exists $Baseball::Sabermetrics::abstract::formula{$name}) { } 30 0 30
51 # is there a better way?
52 0 $ref = \$name;
53 }
54 elsif (exists $self->{$name}) {
55 71 $ref = \$self->{$name};
56 }
57 elsif (exists $self->{$cachename}) {
58 82 $ref = \$self->{$cachename};
59 }
60 elsif (exists $formula{$name}) {
61 # no strict;
62 # use vars qw/ $team $league /;
63  
64  
65 30 my $caller = caller;
66 30 local $_ = $self;
67 # local *league = exists $self->{league} ? \$self->{league} : undef;
68 # local *team = exists $self->{team} ? \$self->{team} : undef;
69 # $DEBUG && print STDERR "[",__PACKAGE__,"] calculating $self->{name}'s $name, league: $league, team: $team\n";
70  
71 30 ▶ unless (ref $formula{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $Baseball::Sabermetrics::abstract::formula{$name}) 1 29 30
72 1 $formula{$name} =~ s[(\$?)(?<!->)("?)(\b\w(?:\w|->)*)][
73 3 my ($d, $q, $n) = ($1, $2, $3);
74 3 ▶ if ($q) {
Branch: 3 branches
BranchTrueFalseTotal
if ($q) { } 0 3 3
elsif ($n =~ /^\d+$/) { } 0 3 3
elsif ($n =~ /^\d/) { } 0 3 3
75 0 "\"$n";
76 }
77 elsif ($n =~ /^\d+$/) {
78 0 $n;
79 }
80 # This is for 2b, 3b. We assume that no formula has name with a digital initial.
81 elsif ($n =~ /^\d/) {
82 0 "\$_->{'$n'}";
83 }
84 else {
85 3 ▶ $d ? "\$$n" : "\$_->$n"
Branch: 1 branch
BranchTrueFalseTotal
$d ? : 0 3 3
86 }
87 ]eg;
88 1 $formula{$name} =~ s/\$team/\$_->team/g;
89 1 $formula{$name} =~ s/\$league/\$_->league/g;
90 # print "## $name ##\n$formula{$name}\n";
91 1 ▶ $formula{$name} = eval "sub { $formula{$name} }" or die $@;
Branch: 1 branch
BranchTrueFalseTotal
$Baseball::Sabermetrics::abstract::formula{$name} = eval "sub { $Baseball::Sabermetrics::abstract::formula{$name} }" or die $@ 1 0 1
92 }
93  
94 30 eval { $self->{$cachename} = $formula{$name}->(@_); };
95 30 ▶ die "$@ when eval [ $name ] of $_->{name}\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 30 30
96  
97 30 $ref = \$self->{$cachename};
98 }
99 else {
100 0 $ref = \$self->{$name};
101 }
102  
103 183 $$ref;
104 }
105  
106 sub print
107 5 ▶{
Subroutine print: called CC=6 SCAR=18.5
Pod undocumented
108 5 my $self = shift;
109 5 ▶ if (grep /^all$/, @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /^all$/, @_) 0 5 5
110 0 @_ = keys %$self;
111 }
112 5 for (@_) {
113 25 ▶ if ($_ eq 'team') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq 'team') { } 0 25 25
114 0 print $self->team->name, "\t";
115 }
116 else {
117 25 my $val = $self->$_;
118 25 ▶ if ($val =~ s/(\d+\.\d\d\d)(\d)\d*/$1/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ s/(\d+\.\d\d\d)(\d)\d*/$1/) 13 12 25
119 13 ▶ $val += 0.001 if $2 >= 5;
Branch: 1 branch
BranchTrueFalseTotal
if $2 >= 5 8 5 13
120 }
121  
122 25 print "$val\t";
123 }
124 }
125 5 print "\n";
126 }
127  
128 sub define
129 1 ▶{
Subroutine define: called CC=1 SCAR=0.0
Pod undocumented
130 1 my ($self, %funcs) = @_;
131 1 %formula = (%formula, %funcs);
132 }
133  
134 sub formula
135 0 ▶{
Subroutine formula: not called CC=2 SCAR=17.9
Pod undocumented
136 0 ▶ die "undefined formula" unless exists $formula{$_[1]};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Baseball::Sabermetrics::abstract::formula{$_[1]} 0 0 0
137 0 return $formula{$_[1]};
138 }
139  
140 sub formula_list
141 0 ▶{
Subroutine formula_list: not called CC=1 SCAR=6.9
Pod undocumented
142 0 return keys %formula;
143 }
144  
145 sub top
146 4 ▶{
Subroutine top: called CC=2 SCAR=7.4
Pod undocumented
147 4 my ($self, $what, $num, $func) = @_;
148 4 ▶ if (! ref $func) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $func) 4 0 4
149 4 return (sort { $b->$func <=> $a->$func } $self->$what)[0..$num-1];
150 }
151 0 return (sort $func $self->what)[0..$num-1];
152 }
153  
154 sub bottom
155 0 ▶{
Subroutine bottom: not called CC=2 SCAR=17.9
Pod undocumented
156 0 my ($self, $what, $num, $func) = @_;
157 0 ▶ if (! ref $func) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $func) 0 0 0
158 0 return (sort { $a->$func <=> $b->$func } $self->$what)[0..$num-1];
159 }
160 0 return (sort $func $self->what)[0..$num-1];
161 }
162  
163 #sub declare
164 #{
165 # my $self = shift;
166 # $self->{$_} for (@_);
167 #}
168  
169 2 1;
« Sabermetrics/League/CPB2.pm ↑ Summary Sabermetrics/Team.pm »