line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/env perl |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
153814
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
54
|
|
4
|
2
|
|
|
2
|
|
10
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
92
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package Gwybodaeth::Parsers::CSV; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Parsers::CSV - Parses CSV into a data structure. |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 SYNOPSIS |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
use CSV; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $csv = CSV->new(); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
$csv->parse(@data); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 DESCRIPTION |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
This module parses CSV documents into a data structure. This structure is an array of arrays. |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=over |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=cut |
27
|
|
|
|
|
|
|
|
28
|
2
|
|
|
2
|
|
12
|
use Carp qw(croak); |
|
2
|
|
|
|
|
11
|
|
|
2
|
|
|
|
|
164
|
|
29
|
2
|
|
|
2
|
|
2298
|
use Text::CSV; |
|
2
|
|
|
|
|
31709
|
|
|
2
|
|
|
|
|
13
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=item new() |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
Returns an instance of the Parsers::CSV class. |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=cut |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub new { |
38
|
2
|
|
|
2
|
1
|
1205
|
my $class = shift; |
39
|
2
|
|
|
|
|
12
|
my $self = { quote_char => '"', |
40
|
|
|
|
|
|
|
sep_char => ',' }; |
41
|
2
|
|
|
|
|
9
|
bless $self, $class; |
42
|
2
|
|
|
|
|
8
|
return $self; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=item parse(@data) |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
Takes a CSV as an array of lines and outputs an array reference |
48
|
|
|
|
|
|
|
to an array of arrays. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=cut |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub parse { |
53
|
7
|
|
|
7
|
1
|
1355
|
my($self, @data) = @_; |
54
|
|
|
|
|
|
|
|
55
|
7
|
50
|
|
|
|
27
|
ref($self) or croak "instance variable needed"; |
56
|
|
|
|
|
|
|
|
57
|
7
|
|
|
|
|
10
|
my @rows; |
58
|
|
|
|
|
|
|
my $i; |
59
|
7
|
|
|
|
|
62
|
my $csv = Text::CSV->new( {binary => 1, |
60
|
|
|
|
|
|
|
quote_char => $self->{quote_char}, |
61
|
|
|
|
|
|
|
sep_char => $self->{sep_char} |
62
|
|
|
|
|
|
|
} ); |
63
|
|
|
|
|
|
|
|
64
|
7
|
|
|
|
|
811
|
for my $row (@data) { |
65
|
11
|
100
|
|
|
|
37
|
if ($csv->parse($row)) { |
66
|
10
|
|
|
|
|
2672
|
my @fields = $csv->fields(); |
67
|
10
|
|
|
|
|
91
|
$rows[$i++] = \@fields; |
68
|
|
|
|
|
|
|
} else { |
69
|
1
|
|
|
|
|
381
|
croak "unable to parse row: " . $csv->error_input . "\n". |
70
|
|
|
|
|
|
|
"Are you sure this is CSV data?"; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
6
|
|
|
|
|
121
|
return \@rows; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
1; |
77
|
|
|
|
|
|
|
__END__ |