| 1 | package TAP::Parser::YAMLish::Writer; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use base 'TAP::Object'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 10 | our $VERSION = '3.52'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # No EBCDIC support on early perls | ||||||||||||||||||||||||||||
| 11 | *from_native = (ord "A" == 65 || $] < 5.008) | ||||||||||||||||||||||||||||
| 12 | 320 | ▶ | ? sub { return shift } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 0 | ▶ | : sub { utf8::native_to_unicode(shift) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 10 | my $ESCAPE_CHAR = qr{ [ [:cntrl:] \" ] }x; | |||||||||||||||||||||||||||
| 16 | 10 | my $ESCAPE_KEY = qr{ (?: ^\W ) | $ESCAPE_CHAR }x; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 10 | my @UNPRINTABLE; | |||||||||||||||||||||||||||
| 19 | 10 | $UNPRINTABLE[$_] = sprintf("x%02x", from_native($_)) for 0 .. ord(" ") - 1; | |||||||||||||||||||||||||||
| 20 | 10 | $UNPRINTABLE[ord "\0"] = 'z'; | |||||||||||||||||||||||||||
| 21 | 10 | $UNPRINTABLE[ord "\a"] = 'a'; | |||||||||||||||||||||||||||
| 22 | 10 | $UNPRINTABLE[ord "\t"] = 't'; | |||||||||||||||||||||||||||
| 23 | 10 | $UNPRINTABLE[ord "\n"] = 'n'; | |||||||||||||||||||||||||||
| 24 | 10 | $UNPRINTABLE[ord "\cK"] = 'v'; | |||||||||||||||||||||||||||
| 25 | 10 | $UNPRINTABLE[ord "\f"] = 'f'; | |||||||||||||||||||||||||||
| 26 | 10 | $UNPRINTABLE[ord "\r"] = 'r'; | |||||||||||||||||||||||||||
| 27 | 10 | $UNPRINTABLE[ord "\e"] = 'e'; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # new() implementation supplied by TAP::Object | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 20 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 20 | my $self = shift; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 20 | ▶ | die "Need something to write" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | unless @_; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 20 | my $obj = shift; | |||||||||||||||||||||||||||
| 38 | 20 | ▶ | my $out = shift || \*STDOUT; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || \*STDOUT
MC/DC0%0/1
shift() || \*STDOUT
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 20 | ▶ | die "Need a reference to something I can write to" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | unless ref $out; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 20 | $self->{writer} = $self->_make_writer($out); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 20 | $self->_write_obj( '---', $obj ); | |||||||||||||||||||||||||||
| 46 | 20 | $self->_put('...'); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 20 | delete $self->{writer}; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 20 | ▶ | sub _make_writer { | ||||||||||||||||||||||||||
|
Subroutine
_make_writer: called CC=6 SCAR=21.0
| |||||||||||||||||||||||||||||
| 52 | 20 | my $self = shift; | |||||||||||||||||||||||||||
| 53 | 20 | my $out = shift; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 20 | my $ref = ref $out; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 20 | ▶ | if ( 'CODE' eq $ref ) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 58 | 18 | return $out; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | elsif ( 'ARRAY' eq $ref ) { | ||||||||||||||||||||||||||||
| 61 | 1 | ▶ | return sub { push @$out, shift }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | elsif ( 'SCALAR' eq $ref ) { | ||||||||||||||||||||||||||||
| 64 | 1 | ▶ | return sub { $$out .= shift() . "\n" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | ▶ | elsif ( 'GLOB' eq $ref || 'IO::Handle' eq $ref ) { | |||||||||||||||||||||||||||
|
Condition0%0/3
"GLOB" eq $ref or "IO::Handle" eq $ref
MC/DC0%0/2
"GLOB" eq $ref or "IO::Handle" eq $ref
"GLOB" eq $ref
"IO::Handle" eq $ref
Truth table0%0/3
A or B
A= "GLOB" eq $refB= "IO::Handle" eq $ref
| |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | return sub { print $out shift(), "\n" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | die "Can't write to $out"; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 168 | ▶ | sub _put { | ||||||||||||||||||||||||||
|
Subroutine
_put: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | 168 | my $self = shift; | |||||||||||||||||||||||||||
| 75 | 168 | $self->{writer}->( join '', @_ ); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 212 | ▶ | sub _enc_scalar { | ||||||||||||||||||||||||||
|
Subroutine
_enc_scalar: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 79 | 212 | my $self = shift; | |||||||||||||||||||||||||||
| 80 | 212 | my $val = shift; | |||||||||||||||||||||||||||
| 81 | 212 | my $rule = shift; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 212 | ▶ | return '~' unless defined $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 209 | ▶ | if ( $val =~ /$rule/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 19 | $val =~ s/\\/\\\\/g; | |||||||||||||||||||||||||||
| 87 | 19 | $val =~ s/"/\\"/g; | |||||||||||||||||||||||||||
| 88 | 19 | $val =~ s/ ( [[:cntrl:]] ) / '\\' . $UNPRINTABLE[ ord($1) ] /gex; | |||||||||||||||||||||||||||
| 89 | 19 | return qq{"$val"}; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 190 | ▶ | if ( length($val) == 0 or $val =~ /\s/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
length $val == 0 or $val =~ /\s/
MC/DC50%1/2
length $val == 0 or $val =~ /\s/
length $val == 0
$val =~ /\s/
Truth table66%2/3
A or B
A= length $val == 0B= $val =~ /\s/
| |||||||||||||||||||||||||||||
| 93 | 3 | $val =~ s/'/''/; | |||||||||||||||||||||||||||
| 94 | 3 | return "'$val'"; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 187 | return $val; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 148 | ▶ | sub _write_obj { | ||||||||||||||||||||||||||
|
Subroutine
_write_obj: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 101 | 148 | my $self = shift; | |||||||||||||||||||||||||||
| 102 | 148 | my $prefix = shift; | |||||||||||||||||||||||||||
| 103 | 148 | my $obj = shift; | |||||||||||||||||||||||||||
| 104 | 148 | ▶ | my $indent = shift || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 148 | ▶ | if ( my $ref = ref $obj ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 41 | my $pad = ' ' x $indent; | |||||||||||||||||||||||||||
| 108 | 41 | ▶ | if ( 'HASH' eq $ref ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 109 | 30 | ▶ | if ( keys %$obj ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 28 | $self->_put($prefix); | |||||||||||||||||||||||||||
| 111 | 28 | for my $key ( sort keys %$obj ) { | |||||||||||||||||||||||||||
| 112 | 105 | my $value = $obj->{$key}; | |||||||||||||||||||||||||||
| 113 | 105 | $self->_write_obj( | |||||||||||||||||||||||||||
| 114 | $pad . $self->_enc_scalar( $key, $ESCAPE_KEY ) . ':', | ||||||||||||||||||||||||||||
| 115 | $value, $indent + 1 | ||||||||||||||||||||||||||||
| 116 | ); | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | else { | ||||||||||||||||||||||||||||
| 120 | 2 | $self->_put( $prefix, ' {}' ); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | elsif ( 'ARRAY' eq $ref ) { | ||||||||||||||||||||||||||||
| 124 | 11 | ▶ | if (@$obj) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 9 | $self->_put($prefix); | |||||||||||||||||||||||||||
| 126 | 9 | for my $value (@$obj) { | |||||||||||||||||||||||||||
| 127 | 23 | $self->_write_obj( | |||||||||||||||||||||||||||
| 128 | $pad . '-', $value, | ||||||||||||||||||||||||||||
| 129 | $indent + 1 | ||||||||||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | else { | ||||||||||||||||||||||||||||
| 134 | 2 | $self->_put( $prefix, ' []' ); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | else { | ||||||||||||||||||||||||||||
| 138 | 0 | die "Don't know how to encode $ref"; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | else { | ||||||||||||||||||||||||||||
| 142 | 107 | $self->_put( $prefix, ' ', $self->_enc_scalar( $obj, $ESCAPE_CHAR ) ); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 10 | 1; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | __END__ | ||||||||||||||||||||||||||||