line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Sweet::File::DSV; |
2
|
1
|
|
|
1
|
|
419043
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
use namespace::autoclean; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
extends 'Sweet::File'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has _fields => ( |
8
|
|
|
|
|
|
|
builder => '_build_fields', |
9
|
|
|
|
|
|
|
handles => { |
10
|
|
|
|
|
|
|
field => 'get', |
11
|
|
|
|
|
|
|
fields => 'elements', |
12
|
|
|
|
|
|
|
num_fields => 'count', |
13
|
|
|
|
|
|
|
}, |
14
|
|
|
|
|
|
|
is => 'ro', |
15
|
|
|
|
|
|
|
isa => 'ArrayRef[Str]', |
16
|
|
|
|
|
|
|
lazy => 1, |
17
|
|
|
|
|
|
|
traits => ['Array'], |
18
|
|
|
|
|
|
|
); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub _build_fields { |
21
|
|
|
|
|
|
|
my $self = shift; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $header = $self->header; |
24
|
|
|
|
|
|
|
my $sep = $self->sep; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# If separator is a pipe, skip it. |
27
|
|
|
|
|
|
|
$sep = '\|' if ($sep eq '|'); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
my @fields = split $sep, $header; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
return \@fields; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
has header => ( |
35
|
|
|
|
|
|
|
builder => '_build_header', |
36
|
|
|
|
|
|
|
is => 'ro', |
37
|
|
|
|
|
|
|
isa => 'Str', |
38
|
|
|
|
|
|
|
lazy => 1, |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub _build_header { |
42
|
|
|
|
|
|
|
my $self = shift; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
my $header = $self->line(0); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
chomp $header; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
return $header; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
has sep => ( |
52
|
|
|
|
|
|
|
builder => '_build_sep', |
53
|
|
|
|
|
|
|
is => 'ro', |
54
|
|
|
|
|
|
|
isa => 'Str', |
55
|
|
|
|
|
|
|
lazy => 1, |
56
|
|
|
|
|
|
|
); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
has _rows => ( |
59
|
|
|
|
|
|
|
builder => '_build_rows', |
60
|
|
|
|
|
|
|
traits => ['Array'], |
61
|
|
|
|
|
|
|
handles => { |
62
|
|
|
|
|
|
|
num_rows => 'count', |
63
|
|
|
|
|
|
|
row => 'get', |
64
|
|
|
|
|
|
|
rows => 'elements', |
65
|
|
|
|
|
|
|
}, |
66
|
|
|
|
|
|
|
is => 'ro', |
67
|
|
|
|
|
|
|
isa => 'ArrayRef[Str]', |
68
|
|
|
|
|
|
|
lazy => 1, |
69
|
|
|
|
|
|
|
); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub _build_rows { |
72
|
|
|
|
|
|
|
my $self = shift; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my @rows = $self->lines; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# Remove header. |
77
|
|
|
|
|
|
|
shift @rows; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
chomp @rows; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
return \@rows; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
1; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 NAME |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
Sweet::File::DSV |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 SYNOPSIS |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Given a C<file.dat> in your home dir. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
FIELD_A|FIELD_B |
97
|
|
|
|
|
|
|
foo|bar |
98
|
|
|
|
|
|
|
2|3 |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Create a pipe separated value file instance. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
my $dir = Sweet::HomeDir->new; |
103
|
|
|
|
|
|
|
my $file = Sweet::File::DSV->new( |
104
|
|
|
|
|
|
|
dir => $dir, |
105
|
|
|
|
|
|
|
name => 'file.dat', |
106
|
|
|
|
|
|
|
sep => '|', |
107
|
|
|
|
|
|
|
); |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head2 header |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head2 sep |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Field separator. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 METHODS |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head2 num_rows |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head2 field |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
say $file->field(0); # FIELD_A |
124
|
|
|
|
|
|
|
say $file->field(1); # FIELD_B |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head2 fields |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head2 rows |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
say $_ for $file->rows; |
131
|
|
|
|
|
|
|
# foo|bar |
132
|
|
|
|
|
|
|
# 2|3 |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 SEE ALSO |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
L<Delimiter-separated values|https://en.wikipedia.org/wiki/Delimiter-separated_values> Wikipedia page. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=cut |
139
|
|
|
|
|
|
|
|