File Coverage

blib/lib/Sport/Analytics/NHL/Util.pm
Criterion Covered Total %
statement 62 75 82.6
branch 12 22 54.5
condition 3 7 42.8
subroutine 14 15 93.3
pod 8 8 100.0
total 99 127 77.9


line stmt bran cond sub pod time code
1             package Sport::Analytics::NHL::Util;
2              
3 55     55   61098 use strict;
  55         116  
  55         1507  
4 55     55   243 use warnings FATAL => 'all';
  55         87  
  55         1606  
5              
6 55     55   249 use Carp;
  55         99  
  55         2416  
7 55     55   277 use File::Basename;
  55         102  
  55         3674  
8 55     55   336 use File::Path qw(mkpath);
  55         143  
  55         2885  
9 55     55   26808 use Data::Dumper;
  55         269574  
  55         2882  
10              
11 55     55   778 use parent 'Exporter';
  55         408  
  55         353  
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 310739 my $message = shift;
96              
97 225215 50       543071 print STDERR "$message\n" if $ENV{HOCKEYDB_DEBUG};
98             }
99              
100             sub verbose ($) {
101              
102 5     5 1 12 my $message = shift;
103              
104 5 50 33     53 print STDERR "$message\n" if $ENV{HOCKEYDB_VERBOSE} || $ENV{HOCKEYDB_DEBUG};
105             }
106              
107             sub read_file ($;$) {
108              
109 48     48 1 4749 my $filename = shift;
110 48   50     240 my $no_strip = shift || 0;
111 48         100 my $content;
112              
113 48         283 debug "Reading $filename ...";
114 48 50       2379 open(my $fh, '<', $filename) or die "Couldn't read file $filename: $!";
115             {
116 48         154 local $/ = undef;
  48         277  
117 48         29614 $content = <$fh>;
118             }
119 48         588 close $fh;
120 48 50       1188 $content =~ s/\xC2\xA0/ /g unless $no_strip;
121 48         15474 $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 4778 my $content = shift;
143 28         73 my $filename = shift;
144 28   50     183 my $no_strip = shift || 1;
145              
146 28         185 debug "Writing $filename ...";
147 28 100       5363 mkpath(dirname($filename)) unless -d dirname($filename);
148 28 50       160 $content =~ s/\xC2\xA0/ /g unless $no_strip;
149 28 50       3224 open(my $fh, '>', $filename) or die "Couldn't write file $filename: $!";
150 28         318 binmode $fh, ':utf8';
151 28         38544 print $fh $content;
152 28         751 close $fh;
153 28         276 $filename;
154             }
155              
156             sub fill_broken($$;$) {
157              
158 641     641 1 852 my $item = shift;
159 641         849 my $broken = shift;
160              
161 641 100       1344 return unless $broken;
162 1         1 for my $field (keys %{$broken}) {
  1         4  
163 2         4 $item->{$field} = $broken->{$field};
164             }
165             }
166              
167             sub get_seconds ($) {
168              
169 4286     4286 1 7893 my $time = shift;
170              
171 4286 50       6546 unless (defined $time) {
172 0         0 print "No time supplied\n";
173 0         0 die Dumper [caller];
174             }
175 4286 50       10433 return $time if $time =~ /^\d+$/;
176 4286         10460 $time =~ /^\-?(\d+)\:(\d+)$/;
177 4286         15938 $1*60 + $2;
178             }
179              
180             sub my_uniq (&@) {
181              
182 17     17 1 35 my $func = shift;
183 17         39 my %seen = ();
184 17         42 grep {! $seen{$func->($_)}++} @_;
  334         527  
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;