File Coverage

blib/lib/Baseball/Sabermetrics/abstract.pm
Criterion Covered Total %
statement 35 62 56.4
branch 9 26 34.6
condition n/a
subroutine 5 9 55.5
pod 0 6 0.0
total 49 103 47.5


line stmt bran cond sub pod time code
1             package Baseball::Sabermetrics::abstract;
2 2     2   6 use strict;
  2         2  
  2         561  
3              
4             our $AUTOLOAD;
5             our %formula;
6              
7             #my $DEBUG = 0;
8              
9             BEGIN {
10             %formula = (
11 0         0 pa => sub { $_->ab + $_->bb + $_->hbp + $_->sf },
12 1         7 ba => sub { $_->h / $_->ab },
13 1         4 obp => sub { ($_->h + $_->bb + $_->hbp) / $_->pa },
14 1         6 slg => sub { $_->tb / $_->ab },
15 1         3 ops => sub { $_->obp + $_->slg },
16 1         4 k_9 => sub { $_->p_so / $_->ip * 9 },
17 1         3 bb_9 => sub { $_->p_bb / $_->ip * 9 },
18 1         6 k_bb => sub { $_->p_so / $_->p_bb },
19 0         0 isop => sub { $_->slg - $_->ba },
20 0         0 rc => sub { $_->ab * $_->obp },
21              
22 1         6 era => sub { $_->er / $_->ip * 9 },
23 1         5 whip => sub { ($_->p_bb + $_->h_allowed) / $_->ip },
24 0         0 babip => sub { ($_->h_allowed - $_->hr_allowed) / ($_->p_pa - $_->h_allowed - $_->p_so - $_->p_bb - $_->hr_allowed) },
25 0         0 go_ao => sub { $_->go / $_->ao },
26              
27 0         0 rf => sub { ($_->a + $_->po) / $_->finn * 9 },
28 2     2   1163 );
29             }
30              
31             sub new
32             {
33 4     4 0 3 my ($class, $hash) = @_;
34 4         6 return bless \%$hash, $class;
35             }
36              
37             sub AUTOLOAD : lvalue
38             {
39 34     34   3763 my $self = shift;
40 34 50       48 my $type = ref($self) or die;
41 34         22 my $name = $AUTOLOAD;
42 34         84 $name =~ s/.*:://;
43 34         19 my $ref;
44              
45 34 50       61 if ($name eq 'DESTROY') {
    100          
    50          
46             # is there a better way?
47 0         0 $ref = \$name;
48             }
49             elsif (exists $self->{$name}) {
50 24         20 $ref = \$self->{$name};
51             }
52             elsif (exists $formula{$name}) {
53             # no strict;
54             # use vars qw/ $team $league /;
55              
56              
57 10         12 my $caller = caller;
58 10         8 local $_ = $self;
59             # local *league = exists $self->{league} ? \$self->{league} : undef;
60             # local *team = exists $self->{team} ? \$self->{team} : undef;
61             # $DEBUG && print STDERR "[",__PACKAGE__,"] calculating $self->{name}'s $name, league: $league, team: $team\n";
62              
63 10 100       18 unless (ref $formula{$name}) {
64 1         5 $formula{$name} =~ s{(\$?)([a-zA-Z_](?:\w|->)*)}{
65 3 50       10 $1 ? "\$$2" : "\$_->$2"
66             }eg;
67 1         2 $formula{$name} =~ s/\$team/\$_->team/g;
68 1         2 $formula{$name} =~ s/\$league/\$_->league/g;
69 1 50       62 $formula{$name} = eval "sub { $formula{$name} }" or die $@;
70             }
71              
72 10         32 $self->{$name} = $formula{$name}->();
73 10         11 $ref = \$self->{$name};
74             }
75             else {
76 0         0 $ref = \$self->{$name};
77             }
78              
79 34         107 $$ref;
80             }
81              
82             sub print
83             {
84 0     0 0 0 my $self = shift;
85 0 0       0 if (grep /^all$/, @_) {
86 0         0 @_ = keys %$self;
87             }
88 0         0 for (@_) {
89 0 0       0 if ($_ eq 'team') {
90 0         0 print $self->team->name, "\t";
91             }
92             else {
93 0         0 my $val = $self->$_;
94 0 0       0 if ($val =~ s/(\d+\.\d\d\d)(\d)\d*/$1/) {
95 0 0       0 $val += 0.001 if $2 >= 5;
96             }
97              
98 0         0 print "$val\t";
99             }
100             }
101 0         0 print "\n";
102             }
103              
104             sub define
105             {
106 1     1 0 460 my ($self, %funcs) = @_;
107 1         12 %formula = (%formula, %funcs);
108             }
109              
110             sub formula
111             {
112 0 0   0 0   die "undefined formula" unless exists $formula{$_[1]};
113 0           return $formula{$_[1]};
114             }
115              
116             sub formula_list
117             {
118 0     0 0   return keys %formula;
119             }
120              
121             sub top
122             {
123 0     0 0   my ($self, $what, $num, $func) = @_;
124 0 0         if (! ref $func) {
125 0           return (sort { $b->$func <=> $a->$func } $self->$what)[0..$num-1];
  0            
126             }
127 0           return (sort $func $self->what)[0..$num-1];
128             }
129              
130             #sub declare
131             #{
132             # my $self = shift;
133             # $self->{$_} for (@_);
134             #}
135              
136             1;