| 1 | package Catmandu::Exporter::Text; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Catmandu::Sane; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | our $VERSION = '1.0201_02'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Catmandu::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | with 'Catmandu::Exporter'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use vars qw(%Interpolated ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # From String::Escape | ||||||||||||||||||||||||||||
| 16 | # Earlier definitions are preferred to later ones, thus we output \n not \x0d | ||||||||||||||||||||||||||||
| 17 | _define_backslash_escapes( | ||||||||||||||||||||||||||||
| 18 | 4 | (map {$_ => $_} ('\\', '"', '$', '@')), | |||||||||||||||||||||||||||
| 19 | ('r' => "\r", 'n' => "\n", 't' => "\t"), | ||||||||||||||||||||||||||||
| 20 | 256 | (map {'x' . unpack('H2', chr($_)) => chr($_)} (0 .. 255)), | |||||||||||||||||||||||||||
| 21 | 1 | (map {sprintf('%03o', $_) => chr($_)} (0 .. 255)), | |||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | sub _define_backslash_escapes { | ||||||||||||||||||||||||||||
| 25 | 1 | ▶ | %Interpolated = @_; | ||||||||||||||||||||||||||
|
Subroutine
_define_backslash_escapes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # $original_string = unbackslash( $special_characters_escaped ); | ||||||||||||||||||||||||||||
| 29 | sub unbackslash ($) { | ||||||||||||||||||||||||||||
| 30 | 4 | ▶ | local $_ = (defined $_[0] ? $_[0] : ''); | ||||||||||||||||||||||||||
|
Subroutine
unbackslash: called CC=2 SCAR=7.0
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 4 | s/ (\A|\G|[^\\]) [\\] ( [0]\d\d | [x][\da-fA-F]{2} | . ) / $1 . ( $Interpolated{lc($2) }) /gsxe; | |||||||||||||||||||||||||||
| 32 | 4 | return $_; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # End from String::Escape | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | has line_sep => | ||||||||||||||||||||||||||||
| 38 | 1 | ▶ | (is => 'ro', default => sub {"\n"}, coerce => sub {unbackslash($_[0]);}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | has field_sep => | ||||||||||||||||||||||||||||
| 40 | 0 | ▶ | (is => 'ro', default => sub {undef}, coerce => sub {unbackslash($_[0])}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | sub add { | ||||||||||||||||||||||||||||
| 43 | 6 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 44 | 6 | my $text = $self->hash_text('', $data); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 6 | $self->fh->print($text); | |||||||||||||||||||||||||||
| 47 | 6 | ▶ | $self->fh->print($self->line_sep) if defined $self->line_sep; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub hash_text { | ||||||||||||||||||||||||||||
| 51 | 6 | ▶ | my ($self, $text, $hash) = @_; | ||||||||||||||||||||||||||
|
Subroutine
hash_text: called CC=7 SCAR=21.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 6 | for my $k (keys %$hash) { | |||||||||||||||||||||||||||
| 54 | 6 | ▶ | next if ($k =~ /^_.*/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 6 | my $item = $hash->{$k}; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 6 | ▶ | $text .= $self->field_sep | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->field_sep and length $text
MC/DC0%0/2
defined $self->field_sep and length $text
defined $self->field_sep
length $text
Truth table33%1/3
A and B
A= defined $self->field_sepB= length $text
| |||||||||||||||||||||||||||||
| 58 | if defined $self->field_sep && length($text); | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 6 | ▶ | if (Catmandu::Util::is_array_ref($item)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 61 | 2 | $text .= $self->array_text($text, $item); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | elsif (Catmandu::Util::is_hash_ref($item)) { | ||||||||||||||||||||||||||||
| 64 | 0 | $text .= $self->hash_text($text, $item); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | else { | ||||||||||||||||||||||||||||
| 67 | 4 | $text .= $item; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 6 | return $text; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | sub array_text { | ||||||||||||||||||||||||||||
| 75 | 2 | ▶ | my ($self, $text, $arr) = @_; | ||||||||||||||||||||||||||
|
Subroutine
array_text: called CC=6 SCAR=19.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2 | for my $item (@$arr) { | |||||||||||||||||||||||||||
| 78 | 4 | ▶ | $text .= $self->field_sep | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->field_sep and length $text
MC/DC50%1/2
defined $self->field_sep and length $text
defined $self->field_sep
length $text
Truth table66%2/3
A and B
A= defined $self->field_sepB= length $text
| |||||||||||||||||||||||||||||
| 79 | if defined $self->field_sep && length($text); | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 4 | ▶ | if (Catmandu::Util::is_array_ref($item)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 82 | 0 | $text .= $self->array_text($text, $item); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | elsif (Catmandu::Util::is_hash_ref($item)) { | ||||||||||||||||||||||||||||
| 85 | 0 | $text .= $self->hash_text($text, $item); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | else { | ||||||||||||||||||||||||||||
| 88 | 4 | $text .= $item; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 2 | return $text; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 1 | 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::Exporter::Text; | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # Print to STDOUT | ||||||||||||||||||||||||||||
| 117 | my $exporter = Catmandu::Exporter::YAML->new(fix => 'myfix.txt'); | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # Print to file or IO::Handle | ||||||||||||||||||||||||||||
| 120 | my $exporter = Catmandu::Exporter::YAML->new(file => '/tmp/out.yml'); | ||||||||||||||||||||||||||||
| 121 | my $exporter = Catmandu::Exporter::YAML->new(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 objects\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 | ||||||||||||||||||||||||||||