File Coverage

blib/lib/Data/Dataset/Classic/Titanic.pm
Criterion Covered Total %
statement 52 52 100.0
branch 8 12 66.6
condition 1 3 33.3
subroutine 8 8 100.0
pod 4 4 100.0
total 73 79 92.4


line stmt bran cond sub pod time code
1             package Data::Dataset::Classic::Titanic;
2             our $AUTHORITY = 'cpan:GENE';
3              
4             # ABSTRACT: Provide the classic Titanic survivor dataset
5              
6             our $VERSION = '0.0102';
7              
8 1     1   731 use strict;
  1         3  
  1         29  
9 1     1   5 use warnings;
  1         2  
  1         25  
10              
11 1     1   1016 use Text::CSV_XS;
  1         18758  
  1         49  
12 1     1   505 use File::ShareDir qw(dist_dir);
  1         25125  
  1         547  
13              
14              
15              
16             sub as_file {
17 4     4 1 571 my $file = eval { dist_dir('Data-Dataset-Classic-Titanic') . '/titanic.csv' };
  4         16  
18 4 50 33     577 $file = 'share/titanic.csv'
19             unless $file && -e $file;
20 4         18 return $file;
21             }
22              
23              
24             sub as_list {
25 1     1 1 1024 my $file = as_file();
26              
27 1         3 my @data;
28              
29 1         8 my $csv = Text::CSV_XS->new({ binary => 1 });
30              
31 1 50       156 open my $fh, '<', $file
32             or die "Can't read $file: $!";
33              
34 1         4 my $counter = 0;
35              
36 1         39 while (my $row = $csv->getline($fh)) {
37 892         27128 $counter++;
38 892 100       1683 next if $counter == 1; # Skip the header
39 891         17841 push @data, $row;
40             }
41              
42 1         78 close $fh;
43              
44 1         163 return @data;
45             }
46              
47              
48             sub as_hash {
49 1     1 1 1093 my $file = as_file();
50              
51 1         4 my %data;
52             my @headers;
53              
54 1         10 my $csv = Text::CSV_XS->new({ binary => 1 });
55              
56 1 50       207 open my $fh, '<', $file
57             or die "Can't read $file: $!";
58              
59 1         6 my $counter = 0;
60              
61 1         41 while (my $row = $csv->getline($fh)) {
62 892         25604 $counter++;
63              
64             # If we are on the first row, grab the headers and skip the row
65 892 100       1728 if ($counter == 1) {
66 1         6 push @headers, @$row;
67 1         9 @data{@headers} = undef;
68 1         26 next;
69             }
70              
71             # Add each row item to the growing header lists
72 891         1967 for my $i (0 .. @headers - 1) {
73 10692         14802 push @{ $data{ $headers[$i] } }, $row->[$i];
  10692         41016  
74             }
75             }
76              
77 1         56 close $fh;
78              
79 1         39 return %data;
80             }
81              
82              
83             sub headers {
84 1     1 1 327 my $file = as_file();
85              
86 1         3 my @data;
87              
88 1         9 my $csv = Text::CSV_XS->new({ binary => 1 });
89              
90 1 50       188 open my $fh, '<', $file
91             or die "Can't read $file: $!";
92              
93 1         53 while (my $row = $csv->getline($fh)) {
94 1         104 push @data, @$row;
95 1         3 last;
96             }
97              
98 1         15 close $fh;
99              
100 1         21 return @data;
101             }
102              
103             1;
104              
105             __END__