| 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 | BEGIN { | ||||||||||||||||||
| 10 | %formula = ( | ||||||||||||||||||
| 11 | 0 | ▶ | pa => sub { $_->ab + $_->bb + $_->hbp + $_->sf }, | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 12 | 1 | ▶ | ba => sub { $_->h / $_->ab }, | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 13 | 1 | ▶ | obp => sub { ($_->h + $_->bb + $_->hbp) / $_->pa }, | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 14 | 1 | ▶ | slg => sub { $_->tb / $_->ab }, | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 15 | 1 | ▶ | ops => sub { $_->obp + $_->slg }, | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 16 | 1 | ▶ | k_9 => sub { $_->p_so / $_->ip * 9 }, | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 17 | 1 | ▶ | bb_9 => sub { $_->p_bb / $_->ip * 9 }, | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 18 | 1 | ▶ | k_bb => sub { $_->p_so / $_->p_bb }, | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 19 | 0 | ▶ | isop => sub { $_->slg - $_->ba }, | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 20 | 0 | ▶ | rc => sub { $_->ab * $_->obp }, | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | 1 | ▶ | era => sub { $_->er / $_->ip * 9 }, | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 23 | 1 | ▶ | whip => sub { ($_->p_bb + $_->h_allowed) / $_->ip }, | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 24 | 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
| |||||||||||||||||||
| 25 | 0 | ▶ | go_ao => sub { $_->go / $_->ao }, | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 26 | |||||||||||||||||||
| 27 | 0 | ▶ | rf => sub { ($_->a + $_->po) / $_->finn * 9 }, | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 28 | 2 | ▶ | ); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 29 | } | ||||||||||||||||||
| 30 | |||||||||||||||||||
| 31 | sub new | ||||||||||||||||||
| 32 | { | ||||||||||||||||||
| 33 | 4 | ▶ | my ($class, $hash) = @_; | ||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 34 | 4 | return bless \%$hash, $class; | |||||||||||||||||
| 35 | } | ||||||||||||||||||
| 36 | |||||||||||||||||||
| 37 | sub AUTOLOAD : lvalue | ||||||||||||||||||
| 38 | { | ||||||||||||||||||
| 39 | 34 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=8 SCAR=21.5
| |||||||||||||||||||
| 40 | 34 | ▶ | my $type = ref($self) or die; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 41 | 34 | my $name = $AUTOLOAD; | |||||||||||||||||
| 42 | 34 | $name =~ s/.*:://; | |||||||||||||||||
| 43 | 34 | my $ref; | |||||||||||||||||
| 44 | |||||||||||||||||||
| 45 | 34 | ▶ | if ($name eq 'DESTROY') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 46 | # is there a better way? | ||||||||||||||||||
| 47 | 0 | $ref = \$name; | |||||||||||||||||
| 48 | } | ||||||||||||||||||
| 49 | elsif (exists $self->{$name}) { | ||||||||||||||||||
| 50 | 24 | $ref = \$self->{$name}; | |||||||||||||||||
| 51 | } | ||||||||||||||||||
| 52 | elsif (exists $formula{$name}) { | ||||||||||||||||||
| 53 | # no strict; | ||||||||||||||||||
| 54 | # use vars qw/ $team $league /; | ||||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | 10 | my $caller = caller; | |||||||||||||||||
| 58 | 10 | 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 | ▶ | unless (ref $formula{$name}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 64 | 1 | $formula{$name} =~ s{(\$?)([a-zA-Z_](?:\w|->)*)}{ | |||||||||||||||||
| 65 | 3 | ▶ | $1 ? "\$$2" : "\$_->$2" | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 66 | }eg; | ||||||||||||||||||
| 67 | 1 | $formula{$name} =~ s/\$team/\$_->team/g; | |||||||||||||||||
| 68 | 1 | $formula{$name} =~ s/\$league/\$_->league/g; | |||||||||||||||||
| 69 | 1 | ▶ | $formula{$name} = eval "sub { $formula{$name} }" or die $@; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 70 | } | ||||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | 10 | $self->{$name} = $formula{$name}->(); | |||||||||||||||||
| 73 | 10 | $ref = \$self->{$name}; | |||||||||||||||||
| 74 | } | ||||||||||||||||||
| 75 | else { | ||||||||||||||||||
| 76 | 0 | $ref = \$self->{$name}; | |||||||||||||||||
| 77 | } | ||||||||||||||||||
| 78 | |||||||||||||||||||
| 79 | 34 | $$ref; | |||||||||||||||||
| 80 | } | ||||||||||||||||||
| 81 | |||||||||||||||||||
| 82 | sub print | ||||||||||||||||||
| 83 | { | ||||||||||||||||||
| 84 | 0 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
print: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||
| 85 | 0 | ▶ | if (grep /^all$/, @_) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 86 | 0 | @_ = keys %$self; | |||||||||||||||||
| 87 | } | ||||||||||||||||||
| 88 | 0 | for (@_) { | |||||||||||||||||
| 89 | 0 | ▶ | if ($_ eq 'team') { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 90 | 0 | print $self->team->name, "\t"; | |||||||||||||||||
| 91 | } | ||||||||||||||||||
| 92 | else { | ||||||||||||||||||
| 93 | 0 | my $val = $self->$_; | |||||||||||||||||
| 94 | 0 | ▶ | if ($val =~ s/(\d+\.\d\d\d)(\d)\d*/$1/) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 95 | 0 | ▶ | $val += 0.001 if $2 >= 5; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 96 | } | ||||||||||||||||||
| 97 | |||||||||||||||||||
| 98 | 0 | print "$val\t"; | |||||||||||||||||
| 99 | } | ||||||||||||||||||
| 100 | } | ||||||||||||||||||
| 101 | 0 | print "\n"; | |||||||||||||||||
| 102 | } | ||||||||||||||||||
| 103 | |||||||||||||||||||
| 104 | sub define | ||||||||||||||||||
| 105 | { | ||||||||||||||||||
| 106 | 1 | ▶ | my ($self, %funcs) = @_; | ||||||||||||||||
|
Subroutine
define: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 107 | 1 | %formula = (%formula, %funcs); | |||||||||||||||||
| 108 | } | ||||||||||||||||||
| 109 | |||||||||||||||||||
| 110 | sub formula | ||||||||||||||||||
| 111 | { | ||||||||||||||||||
| 112 | 0 | ▶ | die "undefined formula" unless exists $formula{$_[1]}; | ||||||||||||||||
|
Subroutine
formula: not called CC=2 SCAR=17.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||
| 113 | 0 | return $formula{$_[1]}; | |||||||||||||||||
| 114 | } | ||||||||||||||||||
| 115 | |||||||||||||||||||
| 116 | sub formula_list | ||||||||||||||||||
| 117 | { | ||||||||||||||||||
| 118 | 0 | ▶ | return keys %formula; | ||||||||||||||||
|
Subroutine
formula_list: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 119 | } | ||||||||||||||||||
| 120 | |||||||||||||||||||
| 121 | sub top | ||||||||||||||||||
| 122 | { | ||||||||||||||||||
| 123 | 0 | ▶ | my ($self, $what, $num, $func) = @_; | ||||||||||||||||
|
Subroutine
top: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||
| 124 | 0 | ▶ | if (! ref $func) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 125 | 0 | return (sort { $b->$func <=> $a->$func } $self->$what)[0..$num-1]; | |||||||||||||||||
| 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 | 2 | 1; | |||||||||||||||||