File Coverage

blib/lib/Iterator/Simple/Util/CSV.pm
Criterion Covered Total %
statement 46 48 95.8
branch 14 18 77.7
condition n/a
subroutine 11 11 100.0
pod 1 1 100.0
total 72 78 92.3


line stmt bran cond sub pod time code
1             package Iterator::Simple::Util::CSV;
2             {
3             $Iterator::Simple::Util::CSV::VERSION = '0.002';
4             }
5              
6             # ABSTRACT: Utility to iterate over CSV data
7              
8 1     1   123364 use strict;
  1         2  
  1         30  
9 1     1   5 use warnings FATAL => 'all';
  1         2  
  1         45  
10              
11 1         9 use Sub::Exporter -setup => {
12             exports => [ 'icsv' ]
13 1     1   900 };
  1         10270  
14              
15 1     1   1563 use Text::CSV_XS;
  1         8907  
  1         72  
16 1     1   11 use IO::Handle;
  1         2  
  1         35  
17 1     1   864 use IO::File;
  1         836  
  1         122  
18 1     1   714 use Iterator::Simple qw( iterator );
  1         2408  
  1         67  
19 1     1   6 use Carp qw( croak );
  1         2  
  1         381  
20              
21             sub icsv {
22 5     5 1 3257 my $input = shift;
23 5 100       24 my $opts = @_ == 1 ? shift @_ : +{ @_ };
24              
25 5         11 my $use_header = delete $opts->{use_header};
26 5         10 my $skip_header = delete $opts->{skip_header};
27 5         9 my $column_names = delete $opts->{column_names};
28            
29 5         8 my $io;
30 5 50       19 if ( ref( $input ) ) {
    50          
31 0         0 $io = $input;
32             }
33             elsif ( $input eq '-' ) {
34 0         0 $io = IO::Handle->new->fdopen( fileno(STDIN), 'r' );
35             }
36             else {
37 5 50       36 $io = IO::File->new( $input, O_RDONLY )
38             or croak "Open $input: $!";
39             }
40            
41 5         521 my $csv = Text::CSV_XS->new( $opts );
42              
43 5         416 my $fetch_row_method = 'getline';
44            
45 5 100       20 if ( $skip_header ) {
    100          
46 3         82 $io->getline;
47             }
48             elsif ( $use_header ) {
49 1         41 $column_names = $csv->getline( $io );
50             }
51              
52 5 100       218 if ( $column_names ) {
53 2         11 $csv->column_names( $column_names );
54 2         54 $fetch_row_method = 'getline_hr';
55             }
56              
57             return iterator {
58 16 100   16   13920 return if $io->eof;
59 1 50   1   10 $csv->$fetch_row_method( $io )
  1         2  
  1         50  
  11         549  
60             or croak "CSV parse error: " . $csv->error_diag;
61 5         44 };
62             }
63              
64             1;
65              
66              
67              
68             =pod
69              
70             =head1 NAME
71              
72             Iterator::Simple::Util::CSV - Utility to iterate over CSV data
73              
74             =head1 VERSION
75              
76             version 0.002
77              
78             =head1 SYNOPSIS
79              
80             use Iterator::Simple::Util::CSV qw( icsv );
81              
82             # Iterate over a CSV file one line at a time
83             my $it = icsv( $some_csv_file );
84             my $r= $it->next; # returns an array ref
85              
86             # Same, but skip the header
87             my $it = icsv( $some_csv_file, skip_header => 1 );
88              
89             # Parse the header, return each row as a hash ref keyed on the
90             # header columns
91             my $it = icsv( $some_csv_file, use_header => 1 );
92             my $r = $it->next; # returns a hash ref
93              
94             # Skip the header, specify the column keys
95             my $it = icsv( $some_csv_file, skip_header => 1, column_name => [ qw( col1 col2 col3 ) ] );
96             my $r = $it->next; returns a hash ref, keys col1, col2, col3
97              
98             =head1 DESCRIPTION
99              
100             This module combines L and L to
101             provide a simple way of iterating over CSV files. It exports a single
102             function, C that constructs an iterator:
103              
104             =over 4
105              
106             =item icsv( I<$input>, [I => I ...] )
107              
108             I<$input> can be a filename or an C object. If the
109             filename is C<->, the iterator will read from STDIN. Options may be
110             specified as a list or a hash reference. The options I,
111             I and I control the behaviour of this
112             module (see the synopsis for details); any other options are passed
113             unchanged to the L constructor.
114              
115             =back
116              
117             =head2 SEE ALSO
118              
119             L, L.
120              
121             =head1 AUTHOR
122              
123             Ray Miller
124              
125             =head1 COPYRIGHT AND LICENSE
126              
127             This software is copyright (c) 2012 by Ray Miller.
128              
129             This is free software; you can redistribute it and/or modify it under
130             the same terms as the Perl 5 programming language system itself.
131              
132             =cut
133              
134              
135             __END__