line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Exporter::Text; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
111060
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.2020'; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
7
|
use Moo; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
8
|
1
|
|
|
1
|
|
358
|
use Catmandu::Util; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
38
|
|
9
|
1
|
|
|
1
|
|
6
|
use namespace::clean; |
|
1
|
|
|
|
|
13
|
|
|
1
|
|
|
|
|
7
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
with 'Catmandu::Exporter'; |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
314
|
use vars qw(%Interpolated ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
2190
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# From String::Escape |
16
|
|
|
|
|
|
|
# Earlier definitions are preferred to later ones, thus we output \n not \x0d |
17
|
|
|
|
|
|
|
_define_backslash_escapes( |
18
|
|
|
|
|
|
|
(map {$_ => $_} ('\\', '"', '$', '@')), |
19
|
|
|
|
|
|
|
('r' => "\r", 'n' => "\n", 't' => "\t"), |
20
|
|
|
|
|
|
|
(map {'x' . unpack('H2', chr($_)) => chr($_)} (0 .. 255)), |
21
|
|
|
|
|
|
|
(map {sprintf('%03o', $_) => chr($_)} (0 .. 255)), |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub _define_backslash_escapes { |
25
|
1
|
|
|
1
|
|
429
|
%Interpolated = @_; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# $original_string = unbackslash( $special_characters_escaped ); |
29
|
|
|
|
|
|
|
sub unbackslash ($) { |
30
|
4
|
50
|
|
4
|
0
|
13
|
local $_ = (defined $_[0] ? $_[0] : ''); |
31
|
4
|
|
|
|
|
12
|
s/ (\A|\G|[^\\]) [\\] ( [0][0-9][0-9] | [x][0-9a-fA-F]{2} | . ) / $1 . ( $Interpolated{lc($2) }) /gsxe; |
|
1
|
|
|
|
|
9
|
|
32
|
4
|
|
|
|
|
65
|
return $_; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# End from String::Escape |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
has line_sep => |
38
|
|
|
|
|
|
|
(is => 'ro', default => sub {"\n"}, coerce => sub {unbackslash($_[0]);}); |
39
|
|
|
|
|
|
|
has field_sep => |
40
|
|
|
|
|
|
|
(is => 'ro', default => sub {undef}, coerce => sub {unbackslash($_[0])}); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub add { |
43
|
|
|
|
|
|
|
my ($self, $data) = @_; |
44
|
|
|
|
|
|
|
my $text = $self->hash_text('', $data); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
$self->fh->print($text); |
47
|
|
|
|
|
|
|
$self->fh->print($self->line_sep) if defined $self->line_sep; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub hash_text { |
51
|
6
|
|
|
6
|
0
|
14
|
my ($self, $text, $hash) = @_; |
52
|
|
|
|
|
|
|
|
53
|
6
|
|
|
|
|
27
|
for my $k (sort keys %$hash) { |
54
|
6
|
50
|
|
|
|
18
|
next if ($k =~ /^_.*/); |
55
|
6
|
|
|
|
|
13
|
my $item = $hash->{$k}; |
56
|
|
|
|
|
|
|
|
57
|
6
|
50
|
33
|
|
|
30
|
$text .= $self->field_sep |
58
|
|
|
|
|
|
|
if defined $self->field_sep && length($text); |
59
|
|
|
|
|
|
|
|
60
|
6
|
100
|
|
|
|
51
|
if (Catmandu::Util::is_array_ref($item)) { |
|
|
50
|
|
|
|
|
|
61
|
2
|
|
|
|
|
9
|
$text .= $self->array_text($text, $item); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
elsif (Catmandu::Util::is_hash_ref($item)) { |
64
|
0
|
|
|
|
|
0
|
$text .= $self->hash_text($text, $item); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
else { |
67
|
4
|
|
|
|
|
12
|
$text .= $item; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
6
|
|
|
|
|
17
|
return $text; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub array_text { |
75
|
2
|
|
|
2
|
0
|
5
|
my ($self, $text, $arr) = @_; |
76
|
|
|
|
|
|
|
|
77
|
2
|
|
|
|
|
4
|
for my $item (@$arr) { |
78
|
4
|
100
|
66
|
|
|
19
|
$text .= $self->field_sep |
79
|
|
|
|
|
|
|
if defined $self->field_sep && length($text); |
80
|
|
|
|
|
|
|
|
81
|
4
|
50
|
|
|
|
15
|
if (Catmandu::Util::is_array_ref($item)) { |
|
|
50
|
|
|
|
|
|
82
|
0
|
|
|
|
|
0
|
$text .= $self->array_text($text, $item); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
elsif (Catmandu::Util::is_hash_ref($item)) { |
85
|
0
|
|
|
|
|
0
|
$text .= $self->hash_text($text, $item); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
else { |
88
|
4
|
|
|
|
|
9
|
$text .= $item; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
2
|
|
|
|
|
8
|
return $text; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
__END__ |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=pod |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 NAME |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Catmandu::Exporter::Text - a Text exporter |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 SYNOPSIS |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# From the command line |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# Write all field values as a line of Text |
110
|
|
|
|
|
|
|
$ catmandu convert JSON to Text --field_sep "," < data.json |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# In a Perl script |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
use Catmandu; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# Print to STDOUT |
117
|
|
|
|
|
|
|
my $exporter = Catmandu->exporter('Text', fix => 'myfix.txt'); |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# Print to file or IO::Handle |
120
|
|
|
|
|
|
|
my $exporter = Catmandu->exporter('Text', file => '/tmp/out.yml'); |
121
|
|
|
|
|
|
|
my $exporter = Catmandu->exporter('Text', file => $fh); |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
$exporter->add_many($arrayref); |
124
|
|
|
|
|
|
|
$exporter->add_many($iterator); |
125
|
|
|
|
|
|
|
$exporter->add_many(sub { }); |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
$exporter->add($hashref); |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
printf "exported %d items\n" , $exporter->count; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head1 DESCRIPTION |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
This C<Catmandu::Exporter> exports items as raw text. All field values found |
134
|
|
|
|
|
|
|
in the data will be contactenated using C<field_sep> as delimiter. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 CONFIGURATION |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=over 4 |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=item file |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Write output to a local file given by its path or file handle. Alternatively a |
143
|
|
|
|
|
|
|
scalar reference can be passed to write to a string and a code reference can be |
144
|
|
|
|
|
|
|
used to write to a callback function. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item fh |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Write the output to an L<IO::Handle>. If not specified, |
149
|
|
|
|
|
|
|
L<Catmandu::Util::io|Catmandu::Util/IO-functions> is used to create the output |
150
|
|
|
|
|
|
|
handle from the C<file> argument or by using STDOUT. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item fix |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
An ARRAY of one or more fixes or file scripts to be applied to exported items. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=item encoding |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Binmode of the output stream C<fh>. Set to "C<:utf8>" by default. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=item line_sep STR |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Use the STR at each end of line. Set to "C<\n>" by default. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item field_sep STR |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Use the STR at each end of a field. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=back |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 SEE ALSO |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
L<Catmandu::Exporter> , L<Catmandu::Importer::Text> |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=cut |