| 1 | |||||||||||||||||||||||||||||
| 2 | package YAML::Hobo; | ||||||||||||||||||||||||||||
| 3 | 4 | $YAML::Hobo::VERSION = '0.2.0'; | |||||||||||||||||||||||||||
| 4 | # ABSTRACT: Poor man's YAML | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | require YAML::Tiny; | |||||||||||||||||||||||||||
| 8 | 4 | YAML::Tiny->VERSION('1.70'); | |||||||||||||||||||||||||||
| 9 | 4 | our @ISA = qw(YAML::Tiny); | |||||||||||||||||||||||||||
| 10 | } | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 4 | our @EXPORT_OK = qw(Dump Load); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | sub Dump { | ||||||||||||||||||||||||||
|
Subroutine
Dump: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 3 | return YAML::Hobo->new(@_)->_dump_string; | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | sub Load { | ||||||||||||||||||||||||||
|
Subroutine
Load: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 0 | my $self = YAML::Hobo->_load_string(@_); | |||||||||||||||||||||||||||
| 20 | 0 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 0 | return @$self; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | else { | ||||||||||||||||||||||||||||
| 24 | # To match YAML.pm, return the last document | ||||||||||||||||||||||||||||
| 25 | 0 | return $self->[-1]; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | ### Constants | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # Printed form of the unprintable characters in the lowest range | ||||||||||||||||||||||||||||
| 32 | # of ASCII characters, listed by ASCII ordinal position. | ||||||||||||||||||||||||||||
| 33 | 4 | my @UNPRINTABLE = qw( | |||||||||||||||||||||||||||
| 34 | 0 x01 x02 x03 x04 x05 x06 a b t n v f r x0E x0F | ||||||||||||||||||||||||||||
| 35 | x10 x11 x12 x13 x14 x15 x16 x17 x18 x19 x1A e x1C x1D x1E x1F | ||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # These 3 values have special meaning when unquoted and using the | ||||||||||||||||||||||||||||
| 39 | # default YAML schema. They need quotes if they are strings. | ||||||||||||||||||||||||||||
| 40 | 4 | my %QUOTE = map { $_ => 1 } qw( null true false ); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | ### Dumper functions | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 21 | ▶ | sub _dump_scalar { | ||||||||||||||||||||||||||
|
Subroutine
_dump_scalar: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||
| 45 | 21 | my $string = $_[1]; | |||||||||||||||||||||||||||
| 46 | 21 | my $is_key = $_[2]; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Check this before checking length or it winds up looking like a string! | ||||||||||||||||||||||||||||
| 49 | 21 | my $has_string_flag = YAML::Tiny::_has_internal_string_value($string); | |||||||||||||||||||||||||||
| 50 | 21 | ▶ | return '~' unless defined $string; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 21 | ▶ | return "''" unless length $string; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 21 | ▶ | if ( Scalar::Util::looks_like_number($string) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # keys and values that have been used as strings get quoted | ||||||||||||||||||||||||||||
| 55 | 4 | ▶ | if ( $is_key || $has_string_flag ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$is_key or $has_string_flag
MC/DC50%1/2
$is_key or $has_string_flag
$is_key
$has_string_flag
Truth table66%2/3
A or B
A= $is_keyB= $has_string_flag
| |||||||||||||||||||||||||||||
| 56 | 1 | return qq|"$string"|; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | else { | ||||||||||||||||||||||||||||
| 59 | 3 | return $string; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 17 | ▶ | if ( $string =~ /[\x00-\x09\x0b-\x0d\x0e-\x1f\x7f-\x9f\'\n\s]/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$string =~ /[\x00-\x09\x0b-\x0d\x0e-\x1f\x7f-\x9f\'\n\s]/ or $QUOTE{$string}
MC/DC100%2/2
$string =~ /[\x00-\x09\x0b-\x0d\x0e-\x1f\x7f-\x9f\'\n\s]/ or $QUOTE{$string}
$string =~ /[\x00-\x09\x0b-\x0d\x0e-\x1f\x7f-\x9f\'\n\s]/
$QUOTE{$string}
Truth table100%3/3
A or B
A= $string =~ /[\x00-\x09\x0b-\x0d\x0e-\x1f\x7f-\x9f\'\n\s]/B= $QUOTE{$string}
| |||||||||||||||||||||||||||||
| 63 | or $QUOTE{$string} ) | ||||||||||||||||||||||||||||
| 64 | { | ||||||||||||||||||||||||||||
| 65 | 6 | $string =~ s/\\/\\\\/g; | |||||||||||||||||||||||||||
| 66 | 6 | $string =~ s/"/\\"/g; | |||||||||||||||||||||||||||
| 67 | 6 | $string =~ s/\n/\\n/g; | |||||||||||||||||||||||||||
| 68 | 6 | $string =~ s/[\x85]/\\N/g; | |||||||||||||||||||||||||||
| 69 | 6 | $string =~ s/([\x00-\x1f])/\\$UNPRINTABLE[ord($1)]/g; | |||||||||||||||||||||||||||
| 70 | 0 | $string =~ s/([\x7f-\x9f])/'\x' . sprintf("%X",ord($1))/ge; | |||||||||||||||||||||||||||
| 71 | 6 | return qq|"$string"|; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 11 | ▶ | if ( $string =~ /(?:^[~!@#%&*|>?:,'"`{}\[\]]|^-+$|:\z)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 1 | return "'$string'"; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 10 | ▶ | return $is_key ? $string : qq|"$string"|; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 4 | 1; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | #pod =encoding utf8 | ||||||||||||||||||||||||||||
| 82 | #pod | ||||||||||||||||||||||||||||
| 83 | #pod =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 84 | #pod | ||||||||||||||||||||||||||||
| 85 | #pod use YAML::Hobo; | ||||||||||||||||||||||||||||
| 86 | #pod | ||||||||||||||||||||||||||||
| 87 | #pod $yaml = YAML::Hobo::Dump( | ||||||||||||||||||||||||||||
| 88 | #pod { release => { dist => 'YAML::Tiny', version => '1.70' }, | ||||||||||||||||||||||||||||
| 89 | #pod author => 'ETHER' | ||||||||||||||||||||||||||||
| 90 | #pod } | ||||||||||||||||||||||||||||
| 91 | #pod ); | ||||||||||||||||||||||||||||
| 92 | #pod | ||||||||||||||||||||||||||||
| 93 | #pod # --- | ||||||||||||||||||||||||||||
| 94 | #pod # author: "ETHER" | ||||||||||||||||||||||||||||
| 95 | #pod # release: | ||||||||||||||||||||||||||||
| 96 | #pod # dist: "YAML::Tiny" | ||||||||||||||||||||||||||||
| 97 | #pod # version: "1.70" | ||||||||||||||||||||||||||||
| 98 | #pod | ||||||||||||||||||||||||||||
| 99 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 100 | #pod | ||||||||||||||||||||||||||||
| 101 | #pod L<YAML::Hobo> is a module to read and write a limited subset of YAML. | ||||||||||||||||||||||||||||
| 102 | #pod It does two things: reads YAML from a string – with C<Dump> – | ||||||||||||||||||||||||||||
| 103 | #pod and dumps YAML into a string – via C<Load>. | ||||||||||||||||||||||||||||
| 104 | #pod | ||||||||||||||||||||||||||||
| 105 | #pod Its only oddity is that, when dumping, it prefers double-quoted strings, | ||||||||||||||||||||||||||||
| 106 | #pod as illustrated in the L</SYNOPSIS>. | ||||||||||||||||||||||||||||
| 107 | #pod | ||||||||||||||||||||||||||||
| 108 | #pod L<YAML::Hobo> is built on the top of L<YAML::Tiny>. | ||||||||||||||||||||||||||||
| 109 | #pod So it deals with the same YAML subset supported by L<YAML::Tiny>. | ||||||||||||||||||||||||||||
| 110 | #pod | ||||||||||||||||||||||||||||
| 111 | #pod =head1 WHY? | ||||||||||||||||||||||||||||
| 112 | #pod | ||||||||||||||||||||||||||||
| 113 | #pod The YAML specification requires a serializer to impose ordering | ||||||||||||||||||||||||||||
| 114 | #pod when dumping map pairs, which results in a "stable" generated output. | ||||||||||||||||||||||||||||
| 115 | #pod | ||||||||||||||||||||||||||||
| 116 | #pod This module adds to this output normalization by insisting | ||||||||||||||||||||||||||||
| 117 | #pod on double-quoted string for values whenever possible. | ||||||||||||||||||||||||||||
| 118 | #pod This is meant to create a more familiar format avoiding | ||||||||||||||||||||||||||||
| 119 | #pod frequent switching among non-quoted text, double-quoted and single-quoted strings. | ||||||||||||||||||||||||||||
| 120 | #pod | ||||||||||||||||||||||||||||
| 121 | #pod The intention is to create a dull homogeneous output, | ||||||||||||||||||||||||||||
| 122 | #pod a poor man's YAML, which is quite obvious and readable. | ||||||||||||||||||||||||||||
| 123 | #pod | ||||||||||||||||||||||||||||
| 124 | #pod =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 125 | #pod | ||||||||||||||||||||||||||||
| 126 | #pod =head2 Dump | ||||||||||||||||||||||||||||
| 127 | #pod | ||||||||||||||||||||||||||||
| 128 | #pod $string = Dump(list-of-Perl-data-structures); | ||||||||||||||||||||||||||||
| 129 | #pod | ||||||||||||||||||||||||||||
| 130 | #pod Turns Perl data into YAML. | ||||||||||||||||||||||||||||
| 131 | #pod | ||||||||||||||||||||||||||||
| 132 | #pod =head2 Load | ||||||||||||||||||||||||||||
| 133 | #pod | ||||||||||||||||||||||||||||
| 134 | #pod @data_structures = Load(string-containing-a-YAML-stream); | ||||||||||||||||||||||||||||
| 135 | #pod | ||||||||||||||||||||||||||||
| 136 | #pod Turns YAML into Perl data. | ||||||||||||||||||||||||||||
| 137 | #pod | ||||||||||||||||||||||||||||
| 138 | #pod =head1 CAVEAT | ||||||||||||||||||||||||||||
| 139 | #pod | ||||||||||||||||||||||||||||
| 140 | #pod This module does not export any function. | ||||||||||||||||||||||||||||
| 141 | #pod But it declares C<Dump> and C<Load> as exportable. | ||||||||||||||||||||||||||||
| 142 | #pod That means you can use them fully-qualified – as C<YAML::Hobo::Dump> | ||||||||||||||||||||||||||||
| 143 | #pod and C<YAML::Hobo::Load> – or you can use an I<importer>, like | ||||||||||||||||||||||||||||
| 144 | #pod L<Importer> or L<Importer::Zim>. For example, | ||||||||||||||||||||||||||||
| 145 | #pod | ||||||||||||||||||||||||||||
| 146 | #pod use zim 'YAML::Hobo' => qw(Dump Load); | ||||||||||||||||||||||||||||
| 147 | #pod | ||||||||||||||||||||||||||||
| 148 | #pod will make C<Dump> and C<Load> available to the code that follows. | ||||||||||||||||||||||||||||
| 149 | #pod | ||||||||||||||||||||||||||||
| 150 | #pod =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 151 | #pod | ||||||||||||||||||||||||||||
| 152 | #pod L<YAML::Tiny> | ||||||||||||||||||||||||||||
| 153 | #pod | ||||||||||||||||||||||||||||
| 154 | #pod =cut | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | __END__ | ||||||||||||||||||||||||||||