File Coverage

blib/lib/Catmandu/Exporter/Text.pm
Criterion Covered Total %
statement 35 38 92.1
branch 10 16 62.5
condition 3 6 50.0
subroutine 9 9 100.0
pod 0 3 0.0
total 57 72 79.1


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