| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # | 
| 2 |  |  |  |  |  |  | # This file is part of Data-Importer | 
| 3 |  |  |  |  |  |  | # | 
| 4 |  |  |  |  |  |  | # This software is copyright (c) 2014 by Kaare Rasmussen. | 
| 5 |  |  |  |  |  |  | # | 
| 6 |  |  |  |  |  |  | # This is free software; you can redistribute it and/or modify it under | 
| 7 |  |  |  |  |  |  | # the same terms as the Perl 5 programming language system itself. | 
| 8 |  |  |  |  |  |  | # | 
| 9 |  |  |  |  |  |  | package Data::Importer::Iterator::Csv; | 
| 10 |  |  |  |  |  |  | $Data::Importer::Iterator::Csv::VERSION = '0.006'; | 
| 11 | 1 |  |  | 1 |  | 34 | use 5.010; | 
|  | 1 |  |  |  |  | 4 |  | 
|  | 1 |  |  |  |  | 42 |  | 
| 12 | 1 |  |  | 1 |  | 7 | use namespace::autoclean; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 11 |  | 
| 13 | 1 |  |  | 1 |  | 74 | use Moose; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 9 |  | 
| 14 | 1 |  |  | 1 |  | 796806 | use Text::CSV; | 
|  | 1 |  |  |  |  | 14351 |  | 
|  | 1 |  |  |  |  | 7 |  | 
| 15 | 1 |  |  | 1 |  | 1266 | use Encode qw/encode/; | 
|  | 1 |  |  |  |  | 10464 |  | 
|  | 1 |  |  |  |  | 480 |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | extends 'Data::Importer::Iterator'; | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | =head1 Description | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | Subclass for handling the import of a csv file | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | =head1 ATTRIBUTES | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | =head2 csv | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | The csv object | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | =cut | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | has 'csv' => ( | 
| 32 |  |  |  |  |  |  | is => 'ro', | 
| 33 |  |  |  |  |  |  | lazy_build => 1, | 
| 34 |  |  |  |  |  |  | ); | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | =head1 "PRIVATE" ATTRIBUTES | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | =head2 file | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | The import file | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | =cut | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | has 'file' => ( | 
| 45 |  |  |  |  |  |  | is => 'ro', | 
| 46 |  |  |  |  |  |  | lazy_build => 1, | 
| 47 |  |  |  |  |  |  | ); | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | =head1 METHODS | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | =head2 _build_csv | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | The lazy builder for the csv object | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | =cut | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | sub _build_csv { | 
| 58 | 1 |  |  | 1 |  | 3 | my $self = shift; | 
| 59 | 1 |  |  |  |  | 12 | my $csv = Text::CSV->new ({ binary => 1, eol => $/ }); | 
| 60 | 1 |  |  |  |  | 145 | return $csv; | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | =head2 _build_file | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | The lazy builder for the file | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | The base class opens a file as UTF-8 and returns it. | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | =cut | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | sub _build_file { | 
| 72 | 1 |  |  | 1 |  | 10 | my $self = shift; | 
| 73 | 1 |  |  |  |  | 46 | my $filename = $self->file_name; | 
| 74 | 1 | 50 |  |  |  | 44 | my $attr = $self->has_encoding ? ':encoding(' . $self->encoding . ')' : ''; | 
| 75 | 1 | 50 |  | 1 |  | 8 | open(my $file, "<$attr", $filename) or die "$filename: $!"; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 8 |  | 
|  | 1 |  |  |  |  | 41 |  | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 1 |  |  |  |  | 1647 | return $file; | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | =head2 next | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | Return the next row of data from the file | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | =cut | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | sub next { | 
| 87 | 63 |  |  | 63 | 1 | 91 | my $self = shift; | 
| 88 | 63 |  |  |  |  | 2094 | my $file = $self->file; | 
| 89 | 63 |  |  |  |  | 2333 | my $csv = $self->csv; | 
| 90 |  |  |  |  |  |  | # Use the first row as column names: | 
| 91 | 63 | 100 |  |  |  | 186 | if (!$csv->column_names) { | 
| 92 | 1 |  |  |  |  | 15 | my $row = $csv->getline($file); | 
| 93 | 1 |  |  |  |  | 9504 | my @fieldnames = map {my $header = lc $_; $header =~ tr/ /_/; $header} @$row; | 
|  | 7 |  |  |  |  | 12 |  | 
|  | 7 |  |  |  |  | 11 |  | 
|  | 7 |  |  |  |  | 18 |  | 
| 94 | 1 | 50 |  |  |  | 7 | die "Only one column detected, please use comma ',' to separate data." if @fieldnames < 2; | 
| 95 | 1 |  |  |  |  | 3 | my %fieldnames = map {$_ => 1} @fieldnames; | 
|  | 7 |  |  |  |  | 21 |  | 
| 96 | 1 | 50 |  |  |  | 4 | if (my @missing = grep {!$fieldnames{$_} } @{ $self->mandatory }) { | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 1 |  |  |  |  | 53 |  | 
| 97 | 0 |  |  |  |  | 0 | die 'Column(s) required, but not found:' . join ', ', @missing; | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 1 |  |  |  |  | 6 | $csv->column_names(@fieldnames); | 
| 101 |  |  |  |  |  |  | } | 
| 102 | 63 |  |  |  |  | 3077 | $self->inc_lineno; | 
| 103 | 63 |  |  |  |  | 152 | return $csv->getline_hr($file); | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | __PACKAGE__->meta->make_immutable; | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | # | 
| 109 |  |  |  |  |  |  | # This file is part of Data-Importer | 
| 110 |  |  |  |  |  |  | # | 
| 111 |  |  |  |  |  |  | # This software is copyright (c) 2014 by Kaare Rasmussen. | 
| 112 |  |  |  |  |  |  | # | 
| 113 |  |  |  |  |  |  | # This is free software; you can redistribute it and/or modify it under | 
| 114 |  |  |  |  |  |  | # the same terms as the Perl 5 programming language system itself. | 
| 115 |  |  |  |  |  |  | # | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | __END__ |