| 1 | |||||||||||||||||||||||||||||
| 2 | package YAML::Hobo; | ||||||||||||||||||||||||||||
| 3 | 2 | $YAML::Hobo::VERSION = '0.1.0'; | |||||||||||||||||||||||||||
| 4 | # ABSTRACT: Poor man's YAML | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | BEGIN { | ||||||||||||||||||||||||||||
| 7 | 2 | ▶ | require YAML::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | YAML::Tiny->VERSION('1.70'); | |||||||||||||||||||||||||||
| 9 | 2 | our @ISA = qw(YAML::Tiny); | |||||||||||||||||||||||||||
| 10 | } | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | our @EXPORT_OK = qw(Dump Load); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | sub Dump { | ||||||||||||||||||||||||||||
| 15 | 1 | ▶ | return YAML::Hobo->new(@_)->_dump_string; | ||||||||||||||||||||||||||
|
Subroutine
Dump: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sub Load { | ||||||||||||||||||||||||||||
| 19 | 0 | ▶ | my $self = YAML::Hobo->_load_string(@_); | ||||||||||||||||||||||||||
|
Subroutine
Load: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 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 | sub _dump_scalar { | ||||||||||||||||||||||||||||
| 30 | 7 | ▶ | my $string = $_[1]; | ||||||||||||||||||||||||||
|
Subroutine
_dump_scalar: called CC=10 SCAR=29.9
| |||||||||||||||||||||||||||||
| 31 | 7 | my $is_key = $_[2]; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # Check this before checking length or it winds up looking like a string! | ||||||||||||||||||||||||||||
| 34 | 7 | my $has_string_flag = YAML::Tiny::_has_internal_string_value($string); | |||||||||||||||||||||||||||
| 35 | 7 | ▶ | return '~' unless defined $string; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 7 | ▶ | return "''" unless length $string; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 7 | ▶ | if ( Scalar::Util::looks_like_number($string) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # keys and values that have been used as strings get quoted | ||||||||||||||||||||||||||||
| 40 | 1 | ▶ | if ( $is_key || $has_string_flag ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$is_key or $has_string_flag
MC/DC0%0/2
$is_key or $has_string_flag
$is_key
$has_string_flag
Truth table33%1/3
A or B
A= $is_keyB= $has_string_flag
| |||||||||||||||||||||||||||||
| 41 | 1 | return qq|"$string"|; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | else { | ||||||||||||||||||||||||||||
| 44 | 0 | return $string; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 6 | ▶ | if ( $string =~ /[\x00-\x09\x0b-\x0d\x0e-\x1f\x7f-\x9f\'\n]/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | $string =~ s/\\/\\\\/g; | |||||||||||||||||||||||||||
| 49 | 0 | $string =~ s/"/\\"/g; | |||||||||||||||||||||||||||
| 50 | 0 | $string =~ s/\n/\\n/g; | |||||||||||||||||||||||||||
| 51 | 0 | $string =~ s/[\x85]/\\N/g; | |||||||||||||||||||||||||||
| 52 | 0 | $string =~ s/([\x00-\x1f])/\\$UNPRINTABLE[ord($1)]/g; | |||||||||||||||||||||||||||
| 53 | 0 | $string =~ s/([\x7f-\x9f])/'\x' . sprintf("%X",ord($1))/ge; | |||||||||||||||||||||||||||
| 54 | 0 | return qq|"$string"|; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 6 | ▶ | if ( $string =~ /(?:^[~!@#%&*|>?:,'"`{}\[\]]|^-+$|\s|:\z)/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$string =~ /(?:^[~!\@#%&*|>?:,'"`{}\[\]]|^-+$|\s|:\z)/ or $QUOTE{$string}
MC/DC0%0/2
$string =~ /(?:^[~!\@#%&*|>?:,'"`{}\[\]]|^-+$|\s|:\z)/ or $QUOTE{$string}
$string =~ /(?:^[~!\@#%&*|>?:,'"`{}\[\]]|^-+$|\s|:\z)/
$QUOTE{$string}
Truth table33%1/3
A or B
A= $string =~ /(?:^[~!\@#%&*|>?:,'"`{}\[\]]|^-+$|\s|:\z)/B= $QUOTE{$string}
| |||||||||||||||||||||||||||||
| 57 | or $QUOTE{$string} ) | ||||||||||||||||||||||||||||
| 58 | { | ||||||||||||||||||||||||||||
| 59 | 0 | return "'$string'"; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 6 | ▶ | return $is_key ? $string : qq|"$string"|; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | 1; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | #pod =encoding utf8 | ||||||||||||||||||||||||||||
| 67 | #pod | ||||||||||||||||||||||||||||
| 68 | #pod =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 69 | #pod | ||||||||||||||||||||||||||||
| 70 | #pod use YAML::Hobo; | ||||||||||||||||||||||||||||
| 71 | #pod | ||||||||||||||||||||||||||||
| 72 | #pod $yaml = YAML::Hobo::Dump( | ||||||||||||||||||||||||||||
| 73 | #pod { release => { dist => 'YAML::Tiny', version => '1.70' }, | ||||||||||||||||||||||||||||
| 74 | #pod author => 'ETHER' | ||||||||||||||||||||||||||||
| 75 | #pod } | ||||||||||||||||||||||||||||
| 76 | #pod ); | ||||||||||||||||||||||||||||
| 77 | #pod | ||||||||||||||||||||||||||||
| 78 | #pod # --- | ||||||||||||||||||||||||||||
| 79 | #pod # author: "ETHER" | ||||||||||||||||||||||||||||
| 80 | #pod # release: | ||||||||||||||||||||||||||||
| 81 | #pod # dist: "YAML::Tiny" | ||||||||||||||||||||||||||||
| 82 | #pod # version: "1.70" | ||||||||||||||||||||||||||||
| 83 | #pod | ||||||||||||||||||||||||||||
| 84 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 85 | #pod | ||||||||||||||||||||||||||||
| 86 | #pod L<YAML::Hobo> is a module to read and write a limited subset of YAML. | ||||||||||||||||||||||||||||
| 87 | #pod It does two things: reads YAML from a string – with C<Dump> – | ||||||||||||||||||||||||||||
| 88 | #pod and dumps YAML into a string – via C<Load>. | ||||||||||||||||||||||||||||
| 89 | #pod | ||||||||||||||||||||||||||||
| 90 | #pod Its only oddity is that, when dumping, it prefers double-quoted strings, | ||||||||||||||||||||||||||||
| 91 | #pod as illustrated in the L</SYNOPSIS>. | ||||||||||||||||||||||||||||
| 92 | #pod | ||||||||||||||||||||||||||||
| 93 | #pod L<YAML::Hobo> is built on the top of L<YAML::Tiny>. | ||||||||||||||||||||||||||||
| 94 | #pod So it deals with the same YAML subset supported by L<YAML::Tiny>. | ||||||||||||||||||||||||||||
| 95 | #pod | ||||||||||||||||||||||||||||
| 96 | #pod =head1 WHY? | ||||||||||||||||||||||||||||
| 97 | #pod | ||||||||||||||||||||||||||||
| 98 | #pod The YAML specification requires a serializer to impose ordering | ||||||||||||||||||||||||||||
| 99 | #pod when dumping map pairs, which results in a "stable" generated output. | ||||||||||||||||||||||||||||
| 100 | #pod | ||||||||||||||||||||||||||||
| 101 | #pod This module adds to this output normalization by insisting | ||||||||||||||||||||||||||||
| 102 | #pod on double-quoted string for values whenever possible. | ||||||||||||||||||||||||||||
| 103 | #pod This is meant to create a more familiar format avoiding | ||||||||||||||||||||||||||||
| 104 | #pod frequent switching among non-quoted text, double-quoted and single-quoted strings. | ||||||||||||||||||||||||||||
| 105 | #pod | ||||||||||||||||||||||||||||
| 106 | #pod The intention is to create a dull homogeneous output, | ||||||||||||||||||||||||||||
| 107 | #pod a poor man's YAML, which is quite obvious and readable. | ||||||||||||||||||||||||||||
| 108 | #pod | ||||||||||||||||||||||||||||
| 109 | #pod =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 110 | #pod | ||||||||||||||||||||||||||||
| 111 | #pod =head2 Dump | ||||||||||||||||||||||||||||
| 112 | #pod | ||||||||||||||||||||||||||||
| 113 | #pod $string = Dump(list-of-Perl-data-structures); | ||||||||||||||||||||||||||||
| 114 | #pod | ||||||||||||||||||||||||||||
| 115 | #pod Turns Perl data into YAML. | ||||||||||||||||||||||||||||
| 116 | #pod | ||||||||||||||||||||||||||||
| 117 | #pod =head2 Load | ||||||||||||||||||||||||||||
| 118 | #pod | ||||||||||||||||||||||||||||
| 119 | #pod @data_structures = Load(string-containing-a-YAML-stream); | ||||||||||||||||||||||||||||
| 120 | #pod | ||||||||||||||||||||||||||||
| 121 | #pod Turns YAML into Perl data. | ||||||||||||||||||||||||||||
| 122 | #pod | ||||||||||||||||||||||||||||
| 123 | #pod =head1 CAVEAT | ||||||||||||||||||||||||||||
| 124 | #pod | ||||||||||||||||||||||||||||
| 125 | #pod This module does not export any function. | ||||||||||||||||||||||||||||
| 126 | #pod But it declares C<Dump> and C<Load> as exportable. | ||||||||||||||||||||||||||||
| 127 | #pod That means you can use them fully-qualified – as C<YAML::Hobo::Dump> | ||||||||||||||||||||||||||||
| 128 | #pod and C<YAML::Hobo::Load> – or you can use an I<importer>, like | ||||||||||||||||||||||||||||
| 129 | #pod L<Importer> or L<Importer::Zim>. For example, | ||||||||||||||||||||||||||||
| 130 | #pod | ||||||||||||||||||||||||||||
| 131 | #pod use zim 'YAML::Hobo' => qw(Dump Load); | ||||||||||||||||||||||||||||
| 132 | #pod | ||||||||||||||||||||||||||||
| 133 | #pod will make C<Dump> and C<Load> available to the code that follows. | ||||||||||||||||||||||||||||
| 134 | #pod | ||||||||||||||||||||||||||||
| 135 | #pod =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 136 | #pod | ||||||||||||||||||||||||||||
| 137 | #pod L<YAML::Tiny> | ||||||||||||||||||||||||||||
| 138 | #pod | ||||||||||||||||||||||||||||
| 139 | #pod =cut | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | __END__ | ||||||||||||||||||||||||||||