line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::CSV::Slurp; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
215882
|
use strict; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
114
|
|
4
|
3
|
|
|
3
|
|
17
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
85
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
3977
|
use Text::CSV; |
|
3
|
|
|
|
|
48861
|
|
|
3
|
|
|
|
|
20
|
|
7
|
3
|
|
|
3
|
|
2965
|
use IO::File; |
|
3
|
|
|
|
|
47526
|
|
|
3
|
|
|
|
|
488
|
|
8
|
|
|
|
|
|
|
|
9
|
3
|
|
|
3
|
|
25
|
use vars qw/$VERSION/; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
2790
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
$VERSION = 1.01; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
1
|
|
|
1
|
1
|
13
|
my $class = shift; |
15
|
1
|
|
|
|
|
4
|
return bless {}, $class; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub load { |
19
|
4
|
|
|
4
|
1
|
27449
|
my $class = shift; |
20
|
4
|
|
|
|
|
18
|
my %opt = @_; |
21
|
|
|
|
|
|
|
|
22
|
4
|
|
|
|
|
13
|
my %default = ( binary => 1 ); |
23
|
4
|
|
|
|
|
18
|
%opt = (%default, %opt); |
24
|
|
|
|
|
|
|
|
25
|
4
|
0
|
33
|
|
|
24
|
unless (defined $opt{file} || defined $opt{filehandle} || defined $opt{string}) { |
|
|
|
33
|
|
|
|
|
26
|
0
|
|
|
|
|
0
|
die "Need either a file, filehandle or string to work with"; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
4
|
50
|
|
|
|
19
|
if (defined $opt{filehandle}) { |
|
|
50
|
|
|
|
|
|
30
|
0
|
|
|
|
|
0
|
my $io = $opt{filehandle}; |
31
|
0
|
|
|
|
|
0
|
delete $opt{filehandle}; |
32
|
0
|
|
|
|
|
0
|
return _from_handle($io,\%opt); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
elsif (defined $opt{file}) { |
35
|
4
|
|
|
|
|
34
|
my $io = new IO::File; |
36
|
4
|
100
|
|
|
|
336
|
open($io, "<$opt{file}") || die "Could not open $opt{file} $!"; |
37
|
3
|
|
|
|
|
8
|
delete $opt{file}; |
38
|
3
|
|
|
|
|
11
|
return _from_handle($io,\%opt); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
else { |
41
|
0
|
|
|
|
|
0
|
my @data = split /\n/, $opt{string}; |
42
|
0
|
|
|
|
|
0
|
delete $opt{string}; |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
0
|
my $csv = Text::CSV->new(\%opt); |
45
|
0
|
|
|
|
|
0
|
$csv->parse(shift @data); |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
0
|
my @names = $csv->fields(); |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
0
|
my @results; |
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
0
|
for my $line (@data) { |
52
|
0
|
|
|
|
|
0
|
$csv->parse($line); |
53
|
0
|
|
|
|
|
0
|
my %hash; |
54
|
0
|
|
|
|
|
0
|
@hash{@names} = $csv->fields(); |
55
|
0
|
|
|
|
|
0
|
push @results, \%hash; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
0
|
return \@results; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub create { |
63
|
1
|
|
|
1
|
1
|
53
|
my ( undef, %arg ) = @_; |
64
|
|
|
|
|
|
|
|
65
|
1
|
|
|
|
|
9
|
die "Need an an array of hashes input to create CSV from" |
66
|
|
|
|
|
|
|
unless exists $arg{input} && |
67
|
|
|
|
|
|
|
ref( $arg{input} ) eq 'ARRAY' && |
68
|
1
|
50
|
33
|
|
|
19
|
ref( @{ $arg{input} }[0] ) eq 'HASH'; |
|
|
|
33
|
|
|
|
|
69
|
|
|
|
|
|
|
|
70
|
1
|
|
|
|
|
4
|
my $list = $arg{input}; |
71
|
1
|
|
|
|
|
3
|
delete $arg{input}; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# get the field names |
74
|
1
|
|
|
|
|
5
|
my @names = defined $arg{field_order} |
75
|
0
|
|
|
|
|
0
|
? @{ $arg{field_order} } |
76
|
1
|
50
|
|
|
|
6
|
: sort keys %{ $list->[0] }; |
77
|
|
|
|
|
|
|
|
78
|
1
|
|
|
|
|
3
|
delete $arg{field_order}; |
79
|
|
|
|
|
|
|
|
80
|
1
|
|
|
|
|
6
|
%arg = ( binary => 1, %arg ); |
81
|
|
|
|
|
|
|
|
82
|
1
|
|
|
|
|
10
|
my $csv = Text::CSV->new( \%arg ); |
83
|
|
|
|
|
|
|
|
84
|
1
|
50
|
|
|
|
138
|
unless ( $csv->combine( @names ) ) { |
85
|
0
|
|
|
|
|
0
|
die "Failed to create the header row because of this invalid input: " . $csv->error_input; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
1
|
|
|
|
|
316
|
my @string = $csv->string; |
89
|
|
|
|
|
|
|
|
90
|
1
|
|
|
|
|
10
|
for my $row ( @$list ) { |
91
|
2
|
|
|
|
|
8
|
my @data; |
92
|
2
|
|
|
|
|
5
|
for my $name ( @names ) { |
93
|
24
|
|
|
|
|
41
|
push @data, $row->{$name}; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
2
|
50
|
|
|
|
9
|
unless ( $csv->combine( @data ) ) { |
97
|
0
|
|
|
|
|
0
|
die "Failed to create a data row because of this invalid input: " . $csv->error_input; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
2
|
|
|
|
|
336
|
push @string, $csv->string; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
1
|
|
|
|
|
25
|
return join "\n", @string; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub _from_handle { |
107
|
3
|
|
|
3
|
|
4
|
my $io = shift; |
108
|
3
|
|
|
|
|
4
|
my $opt = shift; |
109
|
|
|
|
|
|
|
|
110
|
3
|
|
|
|
|
22
|
my $csv = Text::CSV->new($opt); |
111
|
|
|
|
|
|
|
|
112
|
3
|
100
|
|
|
|
275
|
if ( my $head = $csv->getline($io) ) { |
113
|
2
|
|
|
|
|
1438
|
$csv->column_names( $head ); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
else { |
116
|
1
|
|
|
|
|
525
|
die $csv->error_diag(); |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
2
|
|
|
|
|
86
|
my @results; |
120
|
2
|
|
|
|
|
10
|
while (my $ref = $csv->getline_hr($io)) { |
121
|
2
|
|
|
|
|
1058
|
push @results, $ref; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
2
|
|
|
|
|
235
|
return \@results; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
return qw/Open hearts and empty minds/; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
__END__ |