| 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.0103'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 682 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 29 |  | 
| 9 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 25 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 1 |  |  | 1 |  | 895 | use Text::CSV_XS (); | 
|  | 1 |  |  |  |  | 19231 |  | 
|  | 1 |  |  |  |  | 32 |  | 
| 12 | 1 |  |  | 1 |  | 483 | use File::ShareDir qw(dist_dir); | 
|  | 1 |  |  |  |  | 26347 |  | 
|  | 1 |  |  |  |  | 478 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | sub as_file { | 
| 17 | 4 |  |  | 4 | 1 | 578 | my $file = eval { dist_dir('Data-Dataset-Classic-Titanic') . '/titanic.csv' }; | 
|  | 4 |  |  |  |  | 15 |  | 
| 18 | 4 | 50 | 33 |  |  | 576 | $file = 'share/titanic.csv' | 
| 19 |  |  |  |  |  |  | unless $file && -e $file; | 
| 20 | 4 |  |  |  |  | 16 | return $file; | 
| 21 |  |  |  |  |  |  | } | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | sub as_list { | 
| 25 | 1 |  |  | 1 | 1 | 1048 | my $file = as_file(); | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 1 |  |  |  |  | 5 | my @data; | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 1 |  |  |  |  | 7 | my $csv = Text::CSV_XS->new({ binary => 1 }); | 
| 30 |  |  |  |  |  |  |  | 
| 31 | 1 | 50 |  |  |  | 149 | open my $fh, '<', $file | 
| 32 |  |  |  |  |  |  | or die "Can't read $file: $!"; | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 1 |  |  |  |  | 4 | my $counter = 0; | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 1 |  |  |  |  | 41 | while (my $row = $csv->getline($fh)) { | 
| 37 | 892 |  |  |  |  | 26098 | $counter++; | 
| 38 | 892 | 100 |  |  |  | 1636 | next if $counter == 1; # Skip the header | 
| 39 | 891 |  |  |  |  | 17542 | push @data, $row; | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 1 |  |  |  |  | 86 | close $fh; | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 1 |  |  |  |  | 137 | return @data; | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | sub as_hash { | 
| 49 | 1 |  |  | 1 | 1 | 1076 | my $file = as_file(); | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 1 |  |  |  |  | 4 | my %data; | 
| 52 |  |  |  |  |  |  | my @headers; | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 1 |  |  |  |  | 11 | my $csv = Text::CSV_XS->new({ binary => 1 }); | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 1 | 50 |  |  |  | 211 | open my $fh, '<', $file | 
| 57 |  |  |  |  |  |  | or die "Can't read $file: $!"; | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 1 |  |  |  |  | 4 | my $counter = 0; | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 1 |  |  |  |  | 43 | while (my $row = $csv->getline($fh)) { | 
| 62 | 892 |  |  |  |  | 25294 | $counter++; | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | # If we are on the first row, grab the headers and skip the row | 
| 65 | 892 | 100 |  |  |  | 1758 | if ($counter == 1) { | 
| 66 | 1 |  |  |  |  | 7 | push @headers, @$row; | 
| 67 | 1 |  |  |  |  | 12 | @data{@headers} = undef; | 
| 68 | 1 |  |  |  |  | 29 | next; | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | # Add each row item to the growing header lists | 
| 72 | 891 |  |  |  |  | 2014 | for my $i (0 .. @headers - 1) { | 
| 73 | 10692 |  |  |  |  | 14806 | push @{ $data{ $headers[$i] } }, $row->[$i]; | 
|  | 10692 |  |  |  |  | 40285 |  | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 1 |  |  |  |  | 56 | close $fh; | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 1 |  |  |  |  | 37 | return %data; | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | sub headers { | 
| 84 | 1 |  |  | 1 | 1 | 310 | my $file = as_file(); | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 1 |  |  |  |  | 3 | my @data; | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 1 |  |  |  |  | 10 | my $csv = Text::CSV_XS->new({ binary => 1 }); | 
| 89 |  |  |  |  |  |  |  | 
| 90 | 1 | 50 |  |  |  | 211 | open my $fh, '<', $file | 
| 91 |  |  |  |  |  |  | or die "Can't read $file: $!"; | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 1 |  |  |  |  | 55 | while (my $row = $csv->getline($fh)) { | 
| 94 | 1 |  |  |  |  | 82 | push @data, @$row; | 
| 95 | 1 |  |  |  |  | 3 | last; | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 1 |  |  |  |  | 17 | close $fh; | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 1 |  |  |  |  | 21 | return @data; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | 1; | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | __END__ |