line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catalyst::View::Download::CSV; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
2136
|
use Moose; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
11
|
|
4
|
2
|
|
|
2
|
|
8169
|
use namespace::autoclean; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
13
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
extends 'Catalyst::View'; |
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
536
|
use Catalyst::Exception; |
|
2
|
|
|
|
|
108127
|
|
|
2
|
|
|
|
|
54
|
|
9
|
2
|
|
|
2
|
|
1297
|
use Text::CSV; |
|
2
|
|
|
|
|
15518
|
|
|
2
|
|
|
|
|
13
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Catalyst::View::Download::CSV |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 VERSION |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
0.05 |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=cut |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our $VERSION = "0.05"; |
22
|
|
|
|
|
|
|
$VERSION = eval $VERSION; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
__PACKAGE__->config( |
25
|
|
|
|
|
|
|
'stash_key' => 'csv', |
26
|
|
|
|
|
|
|
'quote_char' => '"', |
27
|
|
|
|
|
|
|
'escape_char' => '"', |
28
|
|
|
|
|
|
|
'sep_char' => ',', |
29
|
|
|
|
|
|
|
'eol' => "\n", |
30
|
|
|
|
|
|
|
'binary' => 1, |
31
|
|
|
|
|
|
|
'allow_loose_quotes' => 1, |
32
|
|
|
|
|
|
|
'allow_loose_escapes' => 1, |
33
|
|
|
|
|
|
|
'allow_whitespace' => 1, |
34
|
|
|
|
|
|
|
'always_quote' => 1 |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub process { |
38
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
39
|
0
|
|
|
|
|
0
|
my ($c) = @_; |
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
0
|
my $template = $c->stash->{template}; |
42
|
0
|
|
|
|
|
0
|
my $content = $self->render( $c, $template, $c->stash ); |
43
|
|
|
|
|
|
|
|
44
|
0
|
0
|
|
|
|
0
|
$c->res->headers->header( "Content-Type" => "text/csv" ) |
45
|
|
|
|
|
|
|
unless ( $c->res->headers->header("Content-Type") ); |
46
|
0
|
|
|
|
|
0
|
$c->res->body($content); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub render { |
50
|
1
|
|
|
1
|
1
|
710
|
my $self = shift; |
51
|
1
|
|
|
|
|
2
|
my ( $c, $template, $args ) = @_; |
52
|
|
|
|
|
|
|
|
53
|
1
|
|
|
|
|
3
|
my $stash_key = $self->config->{'stash_key'}; |
54
|
1
|
50
|
33
|
|
|
60
|
return '' unless $args->{$stash_key} && ref($args->{$stash_key}) =~ /ARRAY/; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
my $csv = Text::CSV->new( |
57
|
|
|
|
|
|
|
{ |
58
|
|
|
|
|
|
|
quote_char => $self->config->{'quote_char'}, |
59
|
|
|
|
|
|
|
escape_char => $self->config->{'escape_char'}, |
60
|
|
|
|
|
|
|
sep_char => $self->config->{'sep_char'}, |
61
|
|
|
|
|
|
|
eol => $self->config->{'eol'}, |
62
|
|
|
|
|
|
|
binary => $self->config->{'binary'}, |
63
|
|
|
|
|
|
|
allow_loose_quotes => $self->config->{'allow_loose_quotes'}, |
64
|
|
|
|
|
|
|
allow_loose_escapes => $self->config->{'allow_loose_escapes'}, |
65
|
|
|
|
|
|
|
allow_whitespace => $self->config->{'allow_whitespace'}, |
66
|
1
|
|
|
|
|
3
|
always_quote => $self->config->{'always_quote'} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
); |
69
|
|
|
|
|
|
|
|
70
|
1
|
|
|
|
|
400
|
my $content; |
71
|
1
|
|
|
|
|
1
|
foreach my $row ( @{ $args->{$stash_key} } ) { |
|
1
|
|
|
|
|
4
|
|
72
|
4
|
|
|
|
|
15
|
my $status = $csv->combine( @{ $row } ); |
|
4
|
|
|
|
|
10
|
|
73
|
4
|
50
|
|
|
|
301
|
Catalyst::Exception->throw( |
74
|
|
|
|
|
|
|
"Text::CSV->combine Error: " . $csv->error_diag() ) |
75
|
|
|
|
|
|
|
if ( !$status ); |
76
|
4
|
|
|
|
|
10
|
$content .= $csv->string(); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
1
|
|
|
|
|
42
|
return $content; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
1; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
__END__ |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 SYNOPSIS |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# lib/MyApp/View/Download/CSV.pm |
91
|
|
|
|
|
|
|
package MyApp::View::Download::CSV; |
92
|
|
|
|
|
|
|
use base qw( Catalyst::View::Download::CSV ); |
93
|
|
|
|
|
|
|
1; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# lib/MyApp/Controller/SomeController.pm |
96
|
|
|
|
|
|
|
sub example_action_1 : Local { |
97
|
|
|
|
|
|
|
my ($self, $c) = @_; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# Array reference of array references. |
100
|
|
|
|
|
|
|
my $data = [ |
101
|
|
|
|
|
|
|
['col 1','col 2','col ...','col N'], # row 1 |
102
|
|
|
|
|
|
|
['col 1','col 2','col ...','col N'], # row 2 |
103
|
|
|
|
|
|
|
['col 1','col 2','col ...','col N'], # row ... |
104
|
|
|
|
|
|
|
['col 1','col 2','col ...','col N'] # row N |
105
|
|
|
|
|
|
|
]; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# To output your data in comma seperated values pass your array by reference |
108
|
|
|
|
|
|
|
# into hatever stash key you have defined (default is 'csv'). Content passed |
109
|
|
|
|
|
|
|
# via the stash must be passed in a hashref in the key labeled 'data'. |
110
|
|
|
|
|
|
|
$c->stash->{'csv'} = $data; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# Finally forward processing to the CSV View |
113
|
|
|
|
|
|
|
$c->forward('MyApp::View::Download::CSV'); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 DESCRIPTION |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Takes a nested array and outputs CSV formatted content. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 SUBROUTINES |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head2 process |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
This method will be called by Catalyst if it is asked to forward to a component |
125
|
|
|
|
|
|
|
without a specified action. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head2 render |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Allows others to use this view for much more fine-grained content generation. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head1 CONFIG |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=over 4 |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=item stash_key |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Determines the key in the stash this view will look for when attempting to |
138
|
|
|
|
|
|
|
retrieve data to process. If this key isn't found it will then look at the |
139
|
|
|
|
|
|
|
stash as a whole, find any array references and process them. Data passed into |
140
|
|
|
|
|
|
|
the defined stash_key must be a HASHREF with at least the key 'data' existing |
141
|
|
|
|
|
|
|
with the nested array of data to create a CSV from. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
$c->view('MyApp::View::Download::CSV')->config->{'stash_key'} = 'data'; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=item quote_char |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Determines what value will be enclosed within if it contains whitespace or the |
148
|
|
|
|
|
|
|
delimiter character. DEFAULT: '"' |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
$c->view('MyApp::View::Download::CSV')->config->{'quote_char'} = '/'; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item escape_char |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Determines what value will be to escape any delimiter's found in a column. DEFAULT: '"' |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
$c->view('MyApp::View::Download::CSV')->config->{'escape_char'} = '/'; |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item sep_char |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Determines the separator between columns. DEFAULT: ',' |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
$c->view('MyApp::View::Download::CSV')->config->{'sep_char'} = '|'; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item eol |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Any characters defined in eol will be placed at the end of a row. DEFAULT: '\n' |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
$c->view('MyApp::View::Download::CSV')->config->{'eol'} = '\0'; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=back |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 AUTHOR |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Travis Chase, C<< <gaudeon at cpan dot org> >> |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 SEE ALSO |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
L<Catalyst> L<Catalyst::View> L<Catalyst::View::Download> L<Text::CSV> |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head1 LICENSE |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
This program is free software. You can redistribute it and/or modify it |
183
|
|
|
|
|
|
|
under the same terms as Perl itself. |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=cut |