blib/lib/Text/CSV/Slurp.pm

filter: Statementstmt 91.4% 64 / 70 Branchbran 70.0% 14 / 20 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 83.0% 93 / 112 CC 15 SCAR 17.9
CC
15
Cov
83%
create
11.3
load
5.2
_from_handle
3.0
CRAP 6.0
↑ Summary
1 package Text::CSV::Slurp;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use IO::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 $VERSION = 1.03;
13  
14 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
15 1 my $class = shift;
16 1 return bless {}, $class;
17 }
18  
19 5 ▶sub load {
Subroutine load: called CC=5 SCAR=16.5
Pod documented
20 5 my $class = shift;
21 5 my %opt = @_;
22  
23 5 my %default = ( binary => 1 );
24 5 %opt = (%default, %opt);
25  
26 5 my $io;
27 5 ▶ if (defined $opt{filehandle}) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $opt{'filehandle'}) { } 0 5 5
elsif (defined $opt{'file'}) { } 4 1 5
elsif (defined $opt{'string'}) { } 1 0 1
28 0 $io = $opt{filehandle};
29 0 delete $opt{filehandle};
30 }
31 elsif (defined $opt{file}) {
32 4 $io = new IO::File;
33 4 ▶ open($io, "<$opt{file}") || die "Could not open $opt{file} $!";
Branch: 1 branch
BranchTrueFalseTotal
open $io, qq[<$opt{"file"}] or die qq[Could not open $opt{"file"} $!] 3 1 4
34 3 delete $opt{file};
35 3 return _from_handle($io,\%opt);
36 }
37 elsif (defined $opt{string}) {
38 1 $io = IO::Scalar->new(\$opt{string});
39 1 delete $opt{string};
40 }
41 else {
42 0 die "Need either a file, filehandle or string to work with";
43 }
44 1 return _from_handle($io,\%opt);
45 }
46  
47 1 ▶sub create {
Subroutine create: called CC=9 SCAR=24.3
Pod documented
48 1 my ( undef, %arg ) = @_;
49  
50 die "Need an an array of hashes input to create CSV from"
51 unless exists $arg{input} &&
52 ref( $arg{input} ) eq 'ARRAY' &&
53 1 ▶ ref( @{ $arg{input} }[0] ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless exists $arg{"input"} and ref $arg{"input"} eq "ARRAY" and ref @{$arg{"input"};}[0] eq "HASH" 0 1 1
Condition33%2/6
exists $arg{"input"} and ref $arg{"input"} eq "ARRAY" and ref @{$arg{"input"};}[0] eq "HASH"
!ll&&!rl&&r
001
exists $arg{"input"} and ref $arg{"input"} eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/3
exists $arg{"input"} and ref $arg{"input"} eq "ARRAY" and ref @{$arg{"input"};}[0] eq "HASH"
exists $arg{"input"} ref $arg{"input"} eq "ARRAY" ref @{$arg{"input"};}[0] eq "HASH"
Truth table25%1/4
A and B and C
A= exists $arg{"input"}B= ref $arg{"input"} eq "ARRAY"C= ref @{$arg{"input"};}[0] eq "HASH"
ABCresult
0XX0
10X0
1100
1111
54  
55 1 my $list = $arg{input};
56 1 delete $arg{input};
57  
58 # get the field names
59 my @names = defined $arg{field_order}
60 1 ? @{ $arg{field_order} }
61 0 ▶ : sort keys %{ $list->[0] };
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'field_order'} ? : 1 0 1
62  
63 1 delete $arg{field_order};
64  
65 1 %arg = ( binary => 1, %arg );
66  
67 1 my $csv = Text::CSV->new( \%arg );
68  
69 1 ▶ unless ( $csv->combine( @names ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($csv->combine(@names)) 0 1 1
70 0 die "Failed to create the header row because of this invalid input: " . $csv->error_input;
71 }
72  
73 1 my @string = $csv->string;
74  
75 1 for my $row ( @$list ) {
76 2 my @data;
77 2 for my $name ( @names ) {
78 24 push @data, $row->{$name};
79 }
80  
81 2 ▶ unless ( $csv->combine( @data ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($csv->combine(@data)) 0 2 2
82 0 die "Failed to create a data row because of this invalid input: " . $csv->error_input;
83 }
84  
85 2 push @string, $csv->string;
86 }
87  
88 1 return join "\n", @string;
89 }
90  
91 4 ▶sub _from_handle {
Subroutine _from_handle: called CC=3 SCAR=11.0
92 4 my $io = shift;
93 4 my $opt = shift;
94  
95 4 my $csv = Text::CSV->new($opt);
96  
97 4 ▶ if ( my $head = $csv->getline($io) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $head = $csv->getline($io)) { } 3 1 4
98 3 $csv->column_names( $head );
99 }
100 else {
101 1 die $csv->error_diag();
102 }
103  
104 3 my @results;
105 3 ▶ while (my $ref = $csv->getline_hr($io)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $csv->getline_hr($io)) 4 3 7
106 4 push @results, $ref;
107 }
108  
109 3 return \@results;
110 }
111  
112 4 return qw/Open hearts and empty minds/;
113  
114 =pod
115  
116 =encoding UTF-8
117  
118 =head1 NAME
119  
120 Text::CSV::Slurp - Text::CSV::Slurp - convert CSV into an array of hashes, or an array of hashes into CSV
121  
122 =head1 VERSION
123  
124 version 1.03
125  
126 =head1 SUMMARY
127  
128 I often need to take a CSV file that has a header row and turn it into
129 a perl data structure for further manipulation. This package does that
130 in as few steps as possible.
131  
132 I added a C<create> method in version 0.8 because sometimes you just
133 want to create some bog standard CSV from an array of hashes.
134  
135 =head1 USAGE
136  
137 use Text::CSV::Slurp;
138 use strict;
139  
140 # load data from CSV input
141  
142 my $data = Text::CSV::Slurp->load(file => $filename [,%options]);
143 my $data = Text::CSV::Slurp->load(filehandle => $filehandle [,%options]);
144 my $data = Text::CSV::Slurp->load(string => $string [,%options]);
145  
146 # create a string of CSV from an array of hashes
147 my $csv = Text::CSV::Slurp->create( input => \@array_of_hashes [,%options]);
148  
149 =head1 METHODS
150  
151 =head2 new
152  
153 my $slurp = Text::CSV::Slurp->new();
154  
155 Instantiate an object.
156  
157 =head2 load
158  
159 my $data = Text::CSV::Slurp->load(file => $filename);
160 my $data = $slurp->load(file => $filename);
161  
162 Returns an arrayref of hashrefs. Any extra arguments are passed to L<Text::CSV>.
163 The first line of the CSV is assumed to be a header row. Its fields are
164 used as the keys for each of the hashes.
165  
166 =head2 create
167  
168 my $csv = Text::CSV::Slurp->create( input => \@array_of_hashes [,%options]);
169 my $csv = $slurp->create( input => \@array_of_hashes [,%options]);
170  
171 my $file = "/path/to/output/file.csv";
172  
173 open( FH, ">$file" ) || die "Couldn't open $file $!";
174 print FH $csv;
175 close FH;
176  
177 Creates CSV from an arrayref of hashrefs and returns it as a string. All optional
178 arguments are passed to L<Text::CSV> except for C<field_order>.
179  
180 =head3 field_order
181  
182 C<field_order> which is used to determine the fields and order in which they
183 appear in the CSV. For example:
184  
185 my $csv = Text::CSV::Slurp->create( input => \@array_of_hashes,
186 field_order => ['one','three','two'] );
187  
188 If field_order is not supplied then the sorted keys of the first hash in the
189 input are used instead.
190  
191 =head1 DEPENDENCIES
192  
193 L<Text::CSV>
194  
195 L<IO::File>
196  
197 L<Test::Most> - for tests only
198  
199 =head1 LICENCE
200  
201 GNU General Public License v3
202  
203 =head1 SOURCE
204  
205 Available at L<https://github.com/babf/Text-CSV-Slurp>
206  
207 =head1 SEE ALSO
208  
209 L<Text::CSV>
210  
211 L<Spreadsheet::Read>
212  
213 =head1 THANKS
214  
215 To Kyle Albritton for suggesting and testing the L<create> method
216 To Tomas Pokorny for the L<IO::Scalar> patch for the L<load> method
217  
218 =head1 AUTHOR
219  
220 BABF <babf@cpan.org>
221  
222 =head1 COPYRIGHT AND LICENSE
223  
224 This software is copyright (c) 2016 by BABF.
225  
226 This is free software; you can redistribute it and/or modify it under
227 the same terms as the Perl 5 programming language system itself.
228  
229 =cut
230  
231 __END__
↑ Summary