| 1 | package Data::YAML::Writer; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use vars qw{$VERSION}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | $VERSION = '0.0.7'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | my $ESCAPE_CHAR = qr{ [\x00-\x1f\"] }x; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | my @UNPRINTABLE = qw( | |||||||||||||||||||||||||||
| 14 | z x01 x02 x03 x04 x05 x06 a | ||||||||||||||||||||||||||||
| 15 | x08 t n v f r x0e x0f | ||||||||||||||||||||||||||||
| 16 | x10 x11 x12 x13 x14 x15 x16 x17 | ||||||||||||||||||||||||||||
| 17 | x18 x19 x1a e x1c x1d x1e x1f | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # Create an empty Data::YAML::Writer object | ||||||||||||||||||||||||||||
| 21 | 19 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 19 | my $class = shift; | |||||||||||||||||||||||||||
| 23 | 19 | bless {}, $class; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 19 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 19 | my $self = shift; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 19 | ▶ | croak "Need something to write" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | unless @_; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 19 | my $obj = shift; | |||||||||||||||||||||||||||
| 33 | 19 | ▶ | my $out = shift || \*STDOUT; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || \*STDOUT
MC/DC0%0/1
shift() || \*STDOUT
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 19 | ▶ | croak "Need a reference to something I can write to" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | unless ref $out; | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 19 | $self->{writer} = $self->_make_writer( $out ); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 19 | $self->_write_obj( '---', $obj ); | |||||||||||||||||||||||||||
| 41 | 19 | $self->_put( '...' ); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 19 | delete $self->{writer}; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 19 | ▶ | sub _make_writer { | ||||||||||||||||||||||||||
|
Subroutine
_make_writer: called CC=6 SCAR=21.0
| |||||||||||||||||||||||||||||
| 47 | 19 | my $self = shift; | |||||||||||||||||||||||||||
| 48 | 19 | my $out = shift; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 19 | my $ref = ref $out; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 19 | ▶ | if ( 'CODE' eq $ref ) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 53 | 17 | return $out; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | elsif ( 'ARRAY' eq $ref ) { | ||||||||||||||||||||||||||||
| 56 | 1 | ▶ | return sub { push @$out, shift }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | elsif ( 'SCALAR' eq $ref ) { | ||||||||||||||||||||||||||||
| 59 | 1 | ▶ | return sub { $$out .= shift() . "\n" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | ▶ | 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
| |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | return sub { print $out shift(), "\n" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | croak "Can't write to $out"; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 166 | ▶ | sub _put { | ||||||||||||||||||||||||||
|
Subroutine
_put: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | 166 | my $self = shift; | |||||||||||||||||||||||||||
| 70 | 166 | $self->{writer}->( join '', @_ ); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 214 | ▶ | sub _enc_scalar { | ||||||||||||||||||||||||||
|
Subroutine
_enc_scalar: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 74 | 214 | my $self = shift; | |||||||||||||||||||||||||||
| 75 | 214 | my $val = shift; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 214 | ▶ | return '~' unless defined $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 211 | ▶ | if ( $val =~ /$ESCAPE_CHAR/ or $val =~ /:$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$val =~ /$ESCAPE_CHAR/ or $val =~ /:$/
MC/DC100%2/2
$val =~ /$ESCAPE_CHAR/ or $val =~ /:$/
$val =~ /$ESCAPE_CHAR/
$val =~ /:$/
Truth table100%3/3
A or B
A= $val =~ /$ESCAPE_CHAR/B= $val =~ /:$/
| |||||||||||||||||||||||||||||
| 80 | 12 | $val =~ s/\\/\\\\/g; | |||||||||||||||||||||||||||
| 81 | 12 | $val =~ s/"/\\"/g; | |||||||||||||||||||||||||||
| 82 | 12 | $val =~ s/ ( [\x00-\x1f] ) / '\\' . $UNPRINTABLE[ ord($1) ] /gex; | |||||||||||||||||||||||||||
| 83 | 12 | return qq{"$val"}; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 199 | ▶ | if ( length( $val ) == 0 or $val =~ /\s/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $val == 0 or $val =~ /\s/
MC/DC100%2/2
length $val == 0 or $val =~ /\s/
length $val == 0
$val =~ /\s/
Truth table100%3/3
A or B
A= length $val == 0B= $val =~ /\s/
| |||||||||||||||||||||||||||||
| 87 | 11 | $val =~ s/'/''/; | |||||||||||||||||||||||||||
| 88 | 11 | return "'$val'"; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 188 | return $val; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 147 | ▶ | sub _write_obj { | ||||||||||||||||||||||||||
|
Subroutine
_write_obj: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 95 | 147 | my $self = shift; | |||||||||||||||||||||||||||
| 96 | 147 | my $prefix = shift; | |||||||||||||||||||||||||||
| 97 | 147 | my $obj = shift; | |||||||||||||||||||||||||||
| 98 | 147 | ▶ | my $indent = shift || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 147 | ▶ | if ( my $ref = ref $obj ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 42 | my $pad = ' ' x $indent; | |||||||||||||||||||||||||||
| 102 | 42 | $self->_put( $prefix ); | |||||||||||||||||||||||||||
| 103 | 42 | ▶ | if ( 'HASH' eq $ref ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 104 | 34 | for my $key ( sort keys %$obj ) { | |||||||||||||||||||||||||||
| 105 | 109 | my $value = $obj->{$key}; | |||||||||||||||||||||||||||
| 106 | 109 | $self->_write_obj( $pad . $self->_enc_scalar( $key ) . ':', | |||||||||||||||||||||||||||
| 107 | $value, $indent + 1 ); | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | elsif ( 'ARRAY' eq $ref ) { | ||||||||||||||||||||||||||||
| 111 | 8 | for my $value ( @$obj ) { | |||||||||||||||||||||||||||
| 112 | 19 | $self->_write_obj( $pad . '-', $value, $indent + 1 ); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | else { | ||||||||||||||||||||||||||||
| 116 | 0 | croak "Don't know how to encode $ref"; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | else { | ||||||||||||||||||||||||||||
| 120 | 105 | $self->_put( $prefix, ' ', $self->_enc_scalar( $obj ) ); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 4 | 1; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | __END__ | ||||||||||||||||||||||||||||