line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Exporter; |
2
|
|
|
|
|
|
|
|
3
|
23
|
|
|
23
|
|
122940
|
use Catmandu::Sane; |
|
23
|
|
|
|
|
66
|
|
|
23
|
|
|
|
|
1421
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.2020'; |
6
|
|
|
|
|
|
|
|
7
|
23
|
|
|
23
|
|
180
|
use Catmandu::Util qw(io); |
|
23
|
|
|
|
|
1362
|
|
|
23
|
|
|
|
|
2470
|
|
8
|
23
|
|
|
23
|
|
149
|
use Moo::Role; |
|
23
|
|
|
|
|
49
|
|
|
23
|
|
|
|
|
270
|
|
9
|
23
|
|
|
23
|
|
9438
|
use namespace::clean; |
|
23
|
|
|
|
|
58
|
|
|
23
|
|
|
|
|
143
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
with 'Catmandu::Logger'; |
12
|
|
|
|
|
|
|
with 'Catmandu::Addable'; |
13
|
|
|
|
|
|
|
with 'Catmandu::Counter'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has file => (is => 'ro', lazy => 1, default => sub {\*STDOUT},); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has fh => ( |
18
|
|
|
|
|
|
|
is => 'ro', |
19
|
|
|
|
|
|
|
lazy => 1, |
20
|
|
|
|
|
|
|
default => sub {io($_[0]->file, mode => 'w', binmode => $_[0]->encoding)}, |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
after add => sub { |
24
|
|
|
|
|
|
|
$_[0]->inc_count; |
25
|
|
|
|
|
|
|
}; |
26
|
|
|
|
|
|
|
|
27
|
44
|
|
|
44
|
1
|
1878
|
sub encoding {':utf8'} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
1; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
__END__ |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=pod |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 NAME |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Catmandu::Exporter - Namespace for packages that can export |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head1 SYNOPSIS |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# From the command line |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# JSON is an importer and YAML an exporter |
44
|
|
|
|
|
|
|
$ catmandu convert JSON to YAML < data.json |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# OAI is an importer and JSON an exporter |
47
|
|
|
|
|
|
|
$ catmandu convert OAI --url http://biblio.ugent.be/oai to JSON |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# From Perl |
50
|
|
|
|
|
|
|
use Catmandu; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
my $importer = Catmandu->importer('JSON', file => 'data.json'); |
53
|
|
|
|
|
|
|
my $exporter = Catmandu->exporter('YAML'); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
$exporter->add({ record => "one"}); |
56
|
|
|
|
|
|
|
$exporter->add_many([ { record => "one" } , { record => "two" } ]); |
57
|
|
|
|
|
|
|
$exporter->add_many($importer); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
$exporter->commit; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
undef($exporter); # Clean up memory |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head1 DESCRIPTION |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
A Catmandu::Exporter is a Perl package that can export data into JSON, YAML, XML |
66
|
|
|
|
|
|
|
or many other formats. By default, data is to STDOUT. Optionally provide a C<file> |
67
|
|
|
|
|
|
|
or C<fh> parameter to write to a file, string, or handle. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Every Catmandu::Exporter is a L<Catmandu::Fixable> thus provides a C<fix> |
70
|
|
|
|
|
|
|
parameter and method to apply fixes to exported items: |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
my $exporter = Catmandu->exporter('JSON', fix => ['upcase(title)']); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# This will be printed to STDOUT like: {"title":"MY TITLE"} |
75
|
|
|
|
|
|
|
$exporter->add({ title => "my title"}); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
Every Catmandu::Exporter is a L<Catmandu::Addable> thus inherits the methods |
78
|
|
|
|
|
|
|
C<add> and C<add_many>. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 CONFIGURATION |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=over |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=item file |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Write output to a local file given by its path or file handle. Alternatively a |
87
|
|
|
|
|
|
|
scalar reference can be passed to write to a string and a code reference can be |
88
|
|
|
|
|
|
|
used to write to a callback function. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=item fh |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Write the output to an L<IO::Handle>. If not specified, |
93
|
|
|
|
|
|
|
L<Catmandu::Util::io|Catmandu::Util/IO-functions> is used to create the output |
94
|
|
|
|
|
|
|
handle from the C<file> argument or by using STDOUT. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
It is the task of the Perl programmer to close any opened IO::Handles. |
97
|
|
|
|
|
|
|
Catmandu will not do this by itself. |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=item encoding |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Binmode of the output stream C<fh>. Set to "C<:utf8>" by default. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=item fix |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
An ARRAY of one or more fixes or file scripts to be applied to exported items. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=back |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 METHODS |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head2 add |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Adds one item to be exported. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head2 add_many |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Adds many items to be exported. This can be either an ARRAY-ref or |
118
|
|
|
|
|
|
|
an L<Catmandu::Iterator>. Returns a true value when the export was |
119
|
|
|
|
|
|
|
successful or undef on error. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head2 count |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Returns the number of items exported by this Catmandu::Exporter. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head2 log |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Returns the current logger. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head2 commit |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Commit all buffers to the output handle. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 CODING |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Create your own exporter by creating a Perl package in the Catmandu::Exporter namespace |
136
|
|
|
|
|
|
|
that implements C<Catmandu::Exporter>. Basically, you need to create a method add which |
137
|
|
|
|
|
|
|
writes a Perl hash to a file handle: |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
package Catmandu::Exporter::Foo; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
use Catmandu::Sane; |
143
|
|
|
|
|
|
|
use Moo; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
with 'Catmandu::Exporter' |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub add { |
148
|
|
|
|
|
|
|
my ($self, $data) = @_; |
149
|
|
|
|
|
|
|
my $fh = $self->fh; |
150
|
|
|
|
|
|
|
$fh->print( "Hello, World!"); |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub commit { |
154
|
|
|
|
|
|
|
my ($self) = @_; |
155
|
|
|
|
|
|
|
# this will be called at the end of the record stream |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
1; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
This exporter can be called from the command line as: |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
$ catmandu convert JSON to Foo < data.json |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Or, via Perl |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
use Catmandu; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
my $exporter = Catmandu->exporter('Foo', file => "/tmp/output.txt"); |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
$exporter->add({test => 123}); |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
$exporter->commit; |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
undef($exporter); |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 SEE ALSO |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
See function L<export_to_string|Catmandu/export_to_string> in module |
179
|
|
|
|
|
|
|
L<Catmandu>. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
The exporters L<Catmandu::Exporter::JSON>, L<Catmandu::Exporter::YAML>, |
182
|
|
|
|
|
|
|
L<Catmandu::Exporter::CSV>, and L<Catmandu::Exporter::Text> are included in |
183
|
|
|
|
|
|
|
Catmandu core. |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
See L<Catmandu::Importer> for the opposite action. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=cut |