File Coverage

blib/lib/Data/Dataset/Classic/Titanic.pm
Criterion Covered Total %
statement 52 52 100.0
branch 11 18 61.1
condition 1 3 33.3
subroutine 8 8 100.0
pod 4 4 100.0
total 76 85 89.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.0101';
7              
8 1     1   732 use strict;
  1         2  
  1         33  
9 1     1   5 use warnings;
  1         2  
  1         28  
10              
11 1     1   904 use Text::CSV_XS;
  1         19844  
  1         54  
12 1     1   564 use File::ShareDir qw(dist_dir);
  1         24952  
  1         489  
13              
14              
15              
16             sub as_file {
17 4     4 1 584 my $file = eval { dist_dir('Data-Dataset-Classic-Titanic') . '/titanic.csv' };
  4         17  
18 4 50 33     586 $file = 'share/titanic.csv'
19             unless $file && -e $file;
20 4         17 return $file;
21             }
22              
23              
24             sub as_list {
25 1     1 1 1005 my $file = as_file();
26              
27 1         3 my @data;
28              
29 1 50       8 my $csv = Text::CSV_XS->new({ binary => 1 })
30             or die "Can't read CSV: ", Text::CSV_XS->error_diag;
31              
32 1 50       152 open my $fh, '<', $file
33             or die "Can't read $file: $!";
34              
35 1         3 my $counter = 0;
36              
37 1         40 while (my $row = $csv->getline($fh)) {
38 892         26871 $counter++;
39 892 100       1807 next if $counter == 1; # Skip the header
40 891         17784 push @data, $row;
41             }
42              
43 1         75 close $fh;
44              
45 1         133 return @data;
46             }
47              
48              
49             sub as_hash {
50 1     1 1 1087 my $file = as_file();
51              
52 1         4 my %data;
53             my @headers;
54              
55 1 50       10 my $csv = Text::CSV_XS->new({ binary => 1 })
56             or die "Can't read CSV: ", Text::CSV->error_diag;
57              
58 1 50       242 open my $fh, '<', $file
59             or die "Can't read $file: $!";
60              
61 1         3 my $counter = 0;
62              
63 1         43 while (my $row = $csv->getline($fh)) {
64 892         25161 $counter++;
65              
66             # If we are on the first row, grab the headers and skip the row
67 892 100       1697 if ($counter == 1) {
68 1         6 push @headers, @$row;
69 1         10 @data{@headers} = undef;
70 1         26 next;
71             }
72              
73             # Add each row item to the growing header lists
74 891         1976 for my $i (0 .. @headers - 1) {
75 10692         15344 push @{ $data{ $headers[$i] } }, $row->[$i];
  10692         40027  
76             }
77             }
78              
79 1         58 close $fh;
80              
81 1         38 return %data;
82             }
83              
84              
85             sub headers {
86 1     1 1 318 my $file = as_file();
87              
88 1         4 my @data;
89              
90 1 50       9 my $csv = Text::CSV_XS->new({ binary => 1 })
91             or die "Can't read CSV: ", Text::CSV_XS->error_diag;
92              
93 1 50       201 open my $fh, '<', $file
94             or die "Can't read $file: $!";
95              
96 1         54 while (my $row = $csv->getline($fh)) {
97 1         84 push @data, @$row;
98 1         3 last;
99             }
100              
101 1         17 close $fh;
102              
103 1         20 return @data;
104             }
105              
106             1;
107              
108             __END__