line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Sport::Analytics::NHL::Util; |
2
|
|
|
|
|
|
|
|
3
|
55
|
|
|
55
|
|
49843
|
use strict; |
|
55
|
|
|
|
|
100
|
|
|
55
|
|
|
|
|
1363
|
|
4
|
55
|
|
|
55
|
|
214
|
use warnings FATAL => 'all'; |
|
55
|
|
|
|
|
91
|
|
|
55
|
|
|
|
|
1416
|
|
5
|
|
|
|
|
|
|
|
6
|
55
|
|
|
55
|
|
215
|
use Carp; |
|
55
|
|
|
|
|
82
|
|
|
55
|
|
|
|
|
2164
|
|
7
|
55
|
|
|
55
|
|
246
|
use File::Basename; |
|
55
|
|
|
|
|
105
|
|
|
55
|
|
|
|
|
3234
|
|
8
|
55
|
|
|
55
|
|
313
|
use File::Path qw(mkpath); |
|
55
|
|
|
|
|
142
|
|
|
55
|
|
|
|
|
2547
|
|
9
|
55
|
|
|
55
|
|
23719
|
use Data::Dumper; |
|
55
|
|
|
|
|
241308
|
|
|
55
|
|
|
|
|
2632
|
|
10
|
|
|
|
|
|
|
|
11
|
55
|
|
|
55
|
|
703
|
use parent 'Exporter'; |
|
55
|
|
|
|
|
298
|
|
|
55
|
|
|
|
|
269
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
$SIG{__DIE__} = sub { Carp::confess( @_ ) }; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 NAME |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Sport::Analytics::NHL::Util - Simple system-independent utilities |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SYNOPSIS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
Provides simple system-independent utilities. For system-dependent utilities see Sports::Analytics::NHL::Tools . |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
use Sport::Analytics::NHL::Util |
24
|
|
|
|
|
|
|
debug "This is a debug message"; |
25
|
|
|
|
|
|
|
verbose "This is a verbose message"; |
26
|
|
|
|
|
|
|
my $content = read_file('some.file'); |
27
|
|
|
|
|
|
|
write_file($content, 'some.file'); |
28
|
|
|
|
|
|
|
$table = read_tab_file('some.tab.file'); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 FUNCTIONS |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=over 2 |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=item C |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
Produces message to the STDERR if the DEBUG level is set ($ENV{HOCKEYDB_DEBUG}) |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=item C |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
Produces message to the STDERR if the VERBOSE ($ENV{HOCKEYDB_VERBOSE})or the DEBUG level are set. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=item C |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Reads a file into a scalar |
45
|
|
|
|
|
|
|
Arguments: the filename |
46
|
|
|
|
|
|
|
Returns: the scalar with the filename contents |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=item C |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
Writes a file from a scalar, usually replacing the non-breaking space with regular space |
51
|
|
|
|
|
|
|
Arguments: the content scalar |
52
|
|
|
|
|
|
|
the filename |
53
|
|
|
|
|
|
|
Returns: the filename written |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=item C |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
Reads a tabulated file into an array of arrays |
58
|
|
|
|
|
|
|
Arguments: the tabulated file |
59
|
|
|
|
|
|
|
Returns: array of arrays with the data |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=item C |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
Fills a hash (player, event, etc.) with preset values. Usually happens with broken items. |
64
|
|
|
|
|
|
|
Arguments: |
65
|
|
|
|
|
|
|
* the item to fill |
66
|
|
|
|
|
|
|
* the hash with the preset values to use |
67
|
|
|
|
|
|
|
Returns: void. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=item C |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Get the number of seconds in MM:SS string |
72
|
|
|
|
|
|
|
Arguments: the MM:SS string |
73
|
|
|
|
|
|
|
Returns: the number of seconds |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=item C |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
An expansion of List::MoreUtils::uniq function that filters the items not only by their value, but by applying a function to that value. Effectively: |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
uniq @list == my_uniq { $_ } @list |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=back |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=cut |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
our @EXPORT = qw( |
86
|
|
|
|
|
|
|
debug verbose |
87
|
|
|
|
|
|
|
read_file write_file |
88
|
|
|
|
|
|
|
fill_broken |
89
|
|
|
|
|
|
|
get_seconds |
90
|
|
|
|
|
|
|
my_uniq |
91
|
|
|
|
|
|
|
); |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub debug ($) { |
94
|
|
|
|
|
|
|
|
95
|
225215
|
|
|
225215
|
1
|
290801
|
my $message = shift; |
96
|
|
|
|
|
|
|
|
97
|
225215
|
50
|
|
|
|
509141
|
print STDERR "$message\n" if $ENV{HOCKEYDB_DEBUG}; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub verbose ($) { |
101
|
|
|
|
|
|
|
|
102
|
5
|
|
|
5
|
1
|
10
|
my $message = shift; |
103
|
|
|
|
|
|
|
|
104
|
5
|
50
|
33
|
|
|
52
|
print STDERR "$message\n" if $ENV{HOCKEYDB_VERBOSE} || $ENV{HOCKEYDB_DEBUG}; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub read_file ($;$) { |
108
|
|
|
|
|
|
|
|
109
|
48
|
|
|
48
|
1
|
6633
|
my $filename = shift; |
110
|
48
|
|
50
|
|
|
206
|
my $no_strip = shift || 0; |
111
|
48
|
|
|
|
|
80
|
my $content; |
112
|
|
|
|
|
|
|
|
113
|
48
|
|
|
|
|
240
|
debug "Reading $filename ..."; |
114
|
48
|
50
|
|
|
|
2032
|
open(my $fh, '<', $filename) or die "Couldn't read file $filename: $!"; |
115
|
|
|
|
|
|
|
{ |
116
|
48
|
|
|
|
|
132
|
local $/ = undef; |
|
48
|
|
|
|
|
238
|
|
117
|
48
|
|
|
|
|
27943
|
$content = <$fh>; |
118
|
|
|
|
|
|
|
} |
119
|
48
|
|
|
|
|
544
|
close $fh; |
120
|
48
|
50
|
|
|
|
986
|
$content =~ s/\xC2\xA0/ /g unless $no_strip; |
121
|
48
|
|
|
|
|
14016
|
$content; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub read_tab_file ($) { |
125
|
|
|
|
|
|
|
|
126
|
0
|
|
|
0
|
1
|
0
|
my $filename = shift; |
127
|
0
|
|
|
|
|
0
|
my $table = []; |
128
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
0
|
debug "Reading tabulated $filename ..."; |
130
|
0
|
0
|
|
|
|
0
|
open(my $fh, '<', $filename) or die "Couldn't read file $filename: $!"; |
131
|
0
|
|
|
|
|
0
|
while (<$fh>) { |
132
|
0
|
|
|
|
|
0
|
chomp; |
133
|
0
|
|
|
|
|
0
|
my @row = split(/\t/); |
134
|
0
|
|
|
|
|
0
|
push(@{$table}, [@row]); |
|
0
|
|
|
|
|
0
|
|
135
|
|
|
|
|
|
|
} |
136
|
0
|
|
|
|
|
0
|
close $fh; |
137
|
0
|
|
|
|
|
0
|
$table; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub write_file ($$;$) { |
141
|
|
|
|
|
|
|
|
142
|
28
|
|
|
28
|
1
|
3910
|
my $content = shift; |
143
|
28
|
|
|
|
|
67
|
my $filename = shift; |
144
|
28
|
|
50
|
|
|
162
|
my $no_strip = shift || 1; |
145
|
|
|
|
|
|
|
|
146
|
28
|
|
|
|
|
131
|
debug "Writing $filename ..."; |
147
|
28
|
100
|
|
|
|
3460
|
mkpath(dirname($filename)) unless -d dirname($filename); |
148
|
28
|
50
|
|
|
|
134
|
$content =~ s/\xC2\xA0/ /g unless $no_strip; |
149
|
28
|
50
|
|
|
|
3079
|
open(my $fh, '>', $filename) or die "Couldn't write file $filename: $!"; |
150
|
28
|
|
|
|
|
281
|
binmode $fh, ':utf8'; |
151
|
28
|
|
|
|
|
37734
|
print $fh $content; |
152
|
28
|
|
|
|
|
661
|
close $fh; |
153
|
28
|
|
|
|
|
236
|
$filename; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub fill_broken($$;$) { |
157
|
|
|
|
|
|
|
|
158
|
641
|
|
|
641
|
1
|
914
|
my $item = shift; |
159
|
641
|
|
|
|
|
708
|
my $broken = shift; |
160
|
|
|
|
|
|
|
|
161
|
641
|
100
|
|
|
|
1260
|
return unless $broken; |
162
|
1
|
|
|
|
|
1
|
for my $field (keys %{$broken}) { |
|
1
|
|
|
|
|
3
|
|
163
|
2
|
|
|
|
|
3
|
$item->{$field} = $broken->{$field}; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub get_seconds ($) { |
168
|
|
|
|
|
|
|
|
169
|
4286
|
|
|
4286
|
1
|
7096
|
my $time = shift; |
170
|
|
|
|
|
|
|
|
171
|
4286
|
50
|
|
|
|
6416
|
unless (defined $time) { |
172
|
0
|
|
|
|
|
0
|
print "No time supplied\n"; |
173
|
0
|
|
|
|
|
0
|
die Dumper [caller]; |
174
|
|
|
|
|
|
|
} |
175
|
4286
|
50
|
|
|
|
10999
|
return $time if $time =~ /^\d+$/; |
176
|
4286
|
|
|
|
|
10236
|
$time =~ /^\-?(\d+)\:(\d+)$/; |
177
|
4286
|
|
|
|
|
15220
|
$1*60 + $2; |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
sub my_uniq (&@) { |
181
|
|
|
|
|
|
|
|
182
|
17
|
|
|
17
|
1
|
30
|
my $func = shift; |
183
|
17
|
|
|
|
|
35
|
my %seen = (); |
184
|
17
|
|
|
|
|
32
|
grep {! $seen{$func->($_)}++} @_; |
|
334
|
|
|
|
|
469
|
|
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head1 AUTHOR |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
More Hockey Stats, C<< >> |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head1 BUGS |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
194
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
195
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=head1 SUPPORT |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
perldoc Sport::Analytics::NHL::Util |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
You can also look for information at: |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=over 4 |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
L |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
L |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=item * CPAN Ratings |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
L |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=item * Search CPAN |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
L |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=back |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=cut |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
1; |