line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::CSV; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
652
|
use Carp qw/croak/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
44
|
|
4
|
1
|
|
|
1
|
|
5
|
use Mojo::Collection qw/c/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
5
|
1
|
|
|
1
|
|
593
|
use Text::CSV; |
|
1
|
|
|
|
|
10986
|
|
|
1
|
|
|
|
|
44
|
|
6
|
1
|
|
|
1
|
|
6
|
use Scalar::Util qw/reftype/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
48
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
454
|
use Moo; |
|
1
|
|
|
|
|
8889
|
|
|
1
|
|
|
|
|
4
|
|
9
|
1
|
|
|
1
|
|
1611
|
use MooX::ChainedAttributes; |
|
1
|
|
|
|
|
5693
|
|
|
1
|
|
|
|
|
5
|
|
10
|
1
|
|
|
1
|
|
15941
|
use strictures 2; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
61
|
|
11
|
1
|
|
|
1
|
|
619
|
use namespace::clean; |
|
1
|
|
|
|
|
9464
|
|
|
1
|
|
|
|
|
5
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '1.001004'; # VERSION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has in => ( |
16
|
|
|
|
|
|
|
is => 'rw', |
17
|
|
|
|
|
|
|
chained => 1, |
18
|
|
|
|
|
|
|
coerce => sub { __get_fh( $_[0], '<' ); } |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
has out => ( |
21
|
|
|
|
|
|
|
is => 'rw', |
22
|
|
|
|
|
|
|
chained => 1, |
23
|
|
|
|
|
|
|
coerce => sub { __get_fh( $_[0], '>' ); } |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
has _obj => ( |
26
|
|
|
|
|
|
|
is => 'ro', |
27
|
|
|
|
|
|
|
default => sub { Text::CSV->new({ binary => 1 }) }, |
28
|
|
|
|
|
|
|
); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub __get_fh { |
31
|
11
|
|
|
11
|
|
17
|
my ( $what, $how ) = @_;; |
32
|
|
|
|
|
|
|
|
33
|
11
|
|
|
|
|
12
|
my $fh; |
34
|
11
|
100
|
|
|
|
37
|
if ( ref $what eq 'Mojo::Asset::File' ) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
35
|
1
|
|
|
|
|
4
|
$fh = $what->handle; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
elsif ( ref $what eq 'Mojo::Asset::Memory' ) { |
38
|
1
|
50
|
|
1
|
|
44
|
open $fh, $how, \ $what->slurp or die $!; |
|
1
|
|
|
|
|
11
|
|
|
1
|
|
|
|
|
18
|
|
|
3
|
|
|
|
|
16
|
|
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
elsif ( ref $what ) { |
41
|
1
|
|
|
|
|
3
|
$fh = $what; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
elsif ( defined $what ) { |
44
|
6
|
50
|
|
|
|
313
|
open $fh, $how, $what or croak "Failed to open CSV file ($what): $!"; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
else { |
47
|
0
|
|
|
|
|
0
|
croak 'Could not figure out how to access resource'; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
11
|
|
|
|
|
1019
|
return $fh; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub flush { |
54
|
4
|
|
|
4
|
1
|
8
|
my $self = shift; |
55
|
4
|
|
|
|
|
89
|
close $self->out; |
56
|
|
|
|
|
|
|
|
57
|
4
|
|
|
|
|
153
|
$self; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub row { |
61
|
64
|
|
|
64
|
1
|
4294
|
my $self = shift; |
62
|
64
|
100
|
|
|
|
905
|
my $in = $self->in or croak 'You must specify what to read from'; |
63
|
63
|
100
|
|
|
|
1290
|
my $row = $self->_obj->getline( $in ) or return; |
64
|
56
|
|
|
|
|
1462
|
return $row; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub slurp { |
68
|
6
|
|
|
6
|
1
|
485
|
my $self = shift; |
69
|
6
|
50
|
|
|
|
88
|
@_ and $self->in( shift ); |
70
|
|
|
|
|
|
|
|
71
|
6
|
|
|
|
|
40
|
my @rows; |
72
|
6
|
|
|
|
|
14
|
while ( my $r = $self->row ) { |
73
|
48
|
|
|
|
|
103
|
push @rows, $r; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
6
|
|
|
|
|
192
|
return c @rows; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub slurp_body { |
80
|
2
|
|
|
2
|
1
|
228
|
my $self = shift; |
81
|
2
|
|
|
|
|
4
|
my $r = $self->slurp( @_ ); |
82
|
2
|
|
|
|
|
26
|
return c @$r[ 1 .. $r->size-1 ]; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub spurt { |
86
|
2
|
|
|
2
|
1
|
18
|
my $self = shift; |
87
|
2
|
|
|
|
|
27
|
my $what = shift; |
88
|
2
|
50
|
|
|
|
6
|
@_ and $self->out( shift ); |
89
|
2
|
|
|
|
|
6
|
$self->trickle( $_ ) for @$what; |
90
|
2
|
|
|
|
|
5
|
$self->flush; |
91
|
|
|
|
|
|
|
|
92
|
2
|
|
|
|
|
6
|
$self; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub text { |
96
|
9
|
|
|
9
|
1
|
3383
|
my $self = shift; |
97
|
9
|
50
|
|
|
|
20
|
my $what = shift or return ''; |
98
|
|
|
|
|
|
|
|
99
|
9
|
|
|
|
|
10
|
my @out; |
100
|
9
|
|
|
|
|
19
|
my $obj = $self->_obj; |
101
|
9
|
100
|
33
|
|
|
61
|
if ( reftype $what |
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
102
|
|
|
|
|
|
|
and reftype $what eq 'ARRAY' |
103
|
|
|
|
|
|
|
and reftype $what->[0] |
104
|
|
|
|
|
|
|
and reftype $what->[0] eq 'ARRAY' |
105
|
|
|
|
|
|
|
) { |
106
|
1
|
|
|
|
|
2
|
for my $row ( @$what ) { |
107
|
8
|
50
|
|
|
|
43
|
$obj->combine( @$row ) or next; |
108
|
8
|
|
|
|
|
102
|
push @out, $obj->string; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
else { |
112
|
8
|
50
|
|
|
|
24
|
$obj->combine( @$what ) and push @out, $obj->string; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
9
|
|
|
|
|
192
|
return join "\n", @out; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub trickle { |
119
|
19
|
|
|
19
|
1
|
502
|
my $self = shift; |
120
|
19
|
50
|
|
|
|
33
|
my $what = shift or return $self; |
121
|
19
|
100
|
|
|
|
303
|
my $out = $self->out or croak 'You must specify where to write'; |
122
|
18
|
|
|
|
|
93
|
my $obj = $self->_obj; |
123
|
18
|
50
|
|
|
|
44
|
$obj->combine( @$what ) or return $self; |
124
|
18
|
|
|
|
|
269
|
print $out $obj->string . "\n"; |
125
|
|
|
|
|
|
|
|
126
|
18
|
|
|
|
|
135
|
$self; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
q| |
130
|
|
|
|
|
|
|
Computers are like air conditioners. |
131
|
|
|
|
|
|
|
They work fine until you start opening windows. |
132
|
|
|
|
|
|
|
|; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
__END__ |