| 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.2025'; | |||||||||||||||||||||||||||
| 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 | 1 | ▶ | sub _define_backslash_escapes { | ||||||||||||||||||||||||||
|
Subroutine
_define_backslash_escapes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 1 | %Interpolated = @_; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # $original_string = unbackslash( $special_characters_escaped ); | ||||||||||||||||||||||||||||
| 29 | 4 | ▶ | sub unbackslash ($) { | ||||||||||||||||||||||||||
|
Subroutine
unbackslash: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 30 | 4 | ▶ | local $_ = (defined $_[0] ? $_[0] : ''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 4 | s/ (\A|\G|[^\\]) [\\] ( [0][0-9][0-9] | [x][0-9a-fA-F]{2} | . ) / $1 . ( $Interpolated{lc($2) }) /gsxe; | |||||||||||||||||||||||||||
| 32 | 4 | return $_; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # End from String::Escape | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | has line_sep => ( | ||||||||||||||||||||||||||||
| 38 | is => 'ro', | ||||||||||||||||||||||||||||
| 39 | 1 | ▶ | default => sub {"\n"}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 2 | ▶ | coerce => sub {unbackslash($_[0]);} | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 1 | ); | |||||||||||||||||||||||||||
| 42 | has field_sep => ( | ||||||||||||||||||||||||||||
| 43 | is => 'ro', | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | default => sub {undef}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 45 | 2 | ▶ | coerce => sub {unbackslash($_[0])} | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 1 | ); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 6 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 49 | 6 | my ($self, $data) = @_; | |||||||||||||||||||||||||||
| 50 | 6 | my $text = $self->hash_text('', $data); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 6 | $self->fh->print($text); | |||||||||||||||||||||||||||
| 53 | 6 | ▶ | $self->fh->print($self->line_sep) if defined $self->line_sep; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 6 | ▶ | sub hash_text { | ||||||||||||||||||||||||||
|
Subroutine
hash_text: called CC=7 SCAR=21.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 6 | my ($self, $text, $hash) = @_; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 6 | for my $k (sort keys %$hash) { | |||||||||||||||||||||||||||
| 60 | 6 | ▶ | next if ($k =~ /^_.*/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 6 | my $item = $hash->{$k}; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 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
| |||||||||||||||||||||||||||||
| 64 | if defined $self->field_sep && length($text); | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 6 | ▶ | if (Catmandu::Util::is_array_ref($item)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 67 | 2 | $text .= $self->array_text($text, $item); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | elsif (Catmandu::Util::is_hash_ref($item)) { | ||||||||||||||||||||||||||||
| 70 | 0 | $text .= $self->hash_text($text, $item); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | else { | ||||||||||||||||||||||||||||
| 73 | 4 | $text .= $item; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 6 | return $text; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 2 | ▶ | sub array_text { | ||||||||||||||||||||||||||
|
Subroutine
array_text: called CC=6 SCAR=19.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 2 | my ($self, $text, $arr) = @_; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 2 | for my $item (@$arr) { | |||||||||||||||||||||||||||
| 84 | 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
| |||||||||||||||||||||||||||||
| 85 | if defined $self->field_sep && length($text); | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 4 | ▶ | if (Catmandu::Util::is_array_ref($item)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 88 | 0 | $text .= $self->array_text($text, $item); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | elsif (Catmandu::Util::is_hash_ref($item)) { | ||||||||||||||||||||||||||||
| 91 | 0 | $text .= $self->hash_text($text, $item); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | else { | ||||||||||||||||||||||||||||
| 94 | 4 | $text .= $item; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 2 | return $text; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 1 | 1; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | __END__ | ||||||||||||||||||||||||||||