File Coverage

blib/lib/Sport/Analytics/NHL/Util.pm
Criterion Covered Total %
statement 68 81 83.9
branch 12 22 54.5
condition 3 7 42.8
subroutine 15 16 93.7
pod 9 9 100.0
total 107 135 79.2


line stmt bran cond sub pod time code
1             package Sport::Analytics::NHL::Util;
2              
3 67     67   59422 use strict;
  67         155  
  67         1856  
4 67     67   318 use warnings FATAL => 'all';
  67         134  
  67         2356  
5              
6 67     67   365 use Carp;
  67         171  
  67         3274  
7 67     67   382 use File::Basename;
  67         149  
  67         4772  
8 67     67   402 use File::Path qw(mkpath);
  67         158  
  67         3728  
9 67     67   30320 use Data::Dumper;
  67         342173  
  67         4092  
10              
11 67     67   942 use parent 'Exporter';
  67         455  
  67         491  
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             =item C
82              
83             Performs a string cleanup: replaces multiple whitespaces with one, trims edge whitespaces and converts the string to upper-case.
84              
85             Argument: the string
86             Returns: the normalize string
87              
88             =back
89              
90             =cut
91              
92             our @EXPORT = qw(
93             debug verbose
94             read_file write_file
95             fill_broken
96             get_seconds
97             my_uniq
98             normalize_string
99             );
100              
101             sub debug ($) {
102              
103 316     316 1 745 my $message = shift;
104              
105 316 50       1514 print STDERR "$message\n" if $ENV{HOCKEYDB_DEBUG};
106             }
107              
108             sub verbose ($) {
109              
110 5     5 1 37 my $message = shift;
111              
112 5 50 33     51 print STDERR "$message\n" if $ENV{HOCKEYDB_VERBOSE} || $ENV{HOCKEYDB_DEBUG};
113             }
114              
115             sub read_file ($;$) {
116              
117 70     70 1 9961 my $filename = shift;
118 70   50     405 my $no_strip = shift || 0;
119 70         147 my $content;
120              
121 70         395 debug "Reading $filename ...";
122 70 50       2868 open(my $fh, '<', $filename) or die "Couldn't read file $filename: $!";
123             {
124 70         215 local $/ = undef;
  70         443  
125 70         27881 $content = <$fh>;
126             }
127 70         712 close $fh;
128 70 50       1536 $content =~ s/\xC2\xA0/ /g unless $no_strip;
129 70         22838 $content;
130             }
131              
132             sub read_tab_file ($) {
133              
134 0     0 1 0 my $filename = shift;
135 0         0 my $table = [];
136              
137 0         0 debug "Reading tabulated $filename ...";
138 0 0       0 open(my $fh, '<', $filename) or die "Couldn't read file $filename: $!";
139 0         0 while (<$fh>) {
140 0         0 chomp;
141 0         0 my @row = split(/\t/);
142 0         0 push(@{$table}, [@row]);
  0         0  
143             }
144 0         0 close $fh;
145 0         0 $table;
146             }
147              
148             sub write_file ($$;$) {
149              
150 32     32 1 5015 my $content = shift;
151 32         81 my $filename = shift;
152 32   50     211 my $no_strip = shift || 1;
153              
154 32         186 debug "Writing $filename ...";
155 32 100       3692 mkpath(dirname($filename)) unless -d dirname($filename);
156 32 50       181 $content =~ s/\xC2\xA0/ /g unless $no_strip;
157 32 50       3085 open(my $fh, '>', $filename) or die "Couldn't write file $filename: $!";
158 32         308 binmode $fh, ':utf8';
159 32         38285 print $fh $content;
160 32         855 close $fh;
161 32         328 $filename;
162             }
163              
164             sub fill_broken($$;$) {
165              
166 1281     1281 1 1654 my $item = shift;
167 1281         1554 my $broken = shift;
168              
169 1281 100       2642 return unless $broken;
170 1         2 for my $field (keys %{$broken}) {
  1         5  
171 2         7 $item->{$field} = $broken->{$field};
172             }
173             }
174              
175             sub get_seconds ($) {
176              
177 4947     4947 1 8457 my $time = shift;
178              
179 4947 50       7411 unless (defined $time) {
180 0         0 print "No time supplied\n";
181 0         0 die Dumper [caller];
182             }
183 4947 50       12802 return $time if $time =~ /^\d+$/;
184 4947         12192 $time =~ /^\-?(\d+)\:(\d+)$/;
185 4947         18901 $1*60 + $2;
186             }
187              
188             sub my_uniq (&@) {
189              
190 21     21 1 37 my $func = shift;
191 21         41 my %seen = ();
192 21         38 grep {! $seen{$func->($_)}++} @_;
  407         610  
193             }
194              
195             sub normalize_string ($) {
196              
197 4     4 1 471 my $string = shift;
198              
199 4         15 $string =~ s/^\s+//;
200 4         17 $string =~ s/\s+$//;
201 4         16 $string =~ s/\s+/ /g;
202 4         11 $string = uc $string;
203              
204 4         17 $string;
205             }
206              
207             =head1 AUTHOR
208              
209             More Hockey Stats, C<< >>
210              
211             =head1 BUGS
212              
213             Please report any bugs or feature requests to C, or through
214             the web interface at L. I will be notified, and then you'll
215             automatically be notified of progress on your bug as I make changes.
216              
217              
218             =head1 SUPPORT
219              
220             You can find documentation for this module with the perldoc command.
221              
222             perldoc Sport::Analytics::NHL::Util
223              
224             You can also look for information at:
225              
226             =over 4
227              
228             =item * RT: CPAN's request tracker (report bugs here)
229              
230             L
231              
232             =item * AnnoCPAN: Annotated CPAN documentation
233              
234             L
235              
236             =item * CPAN Ratings
237              
238             L
239              
240             =item * Search CPAN
241              
242             L
243              
244             =back
245              
246             =cut
247              
248             1;