File Coverage

blib/lib/YAML/Hobo.pm
Criterion Covered Total %
statement 24 29 82.7
branch 12 16 75.0
condition 5 6 83.3
subroutine 3 4 75.0
pod 2 2 100.0
total 46 57 80.7


line stmt bran cond sub pod time code
1              
2             package YAML::Hobo;
3             $YAML::Hobo::VERSION = '0.2.0';
4             # ABSTRACT: Poor man's YAML
5              
6             BEGIN {
7 4     4   267253 require YAML::Tiny;
8 4         26128 YAML::Tiny->VERSION('1.70');
9 4         2252 our @ISA = qw(YAML::Tiny);
10             }
11              
12             our @EXPORT_OK = qw(Dump Load);
13              
14             sub Dump {
15 3     3 1 318 return YAML::Hobo->new(@_)->_dump_string;
16             }
17              
18             sub Load {
19 0     0 1 0 my $self = YAML::Hobo->_load_string(@_);
20 0 0       0 if (wantarray) {
21 0         0 return @$self;
22             }
23             else {
24             # To match YAML.pm, return the last document
25 0         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             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             my %QUOTE = map { $_ => 1 } qw( null true false );
41              
42             ### Dumper functions
43              
44             sub _dump_scalar {
45 21     21   340 my $string = $_[1];
46 21         30 my $is_key = $_[2];
47              
48             # Check this before checking length or it winds up looking like a string!
49 21         39 my $has_string_flag = YAML::Tiny::_has_internal_string_value($string);
50 21 50       205 return '~' unless defined $string;
51 21 50       41 return "''" unless length $string;
52 21 100       56 if ( Scalar::Util::looks_like_number($string) ) {
53              
54             # keys and values that have been used as strings get quoted
55 4 100 66     21 if ( $is_key || $has_string_flag ) {
56 1         4 return qq|"$string"|;
57             }
58             else {
59 3         11 return $string;
60             }
61             }
62 17 100 100     83 if ( $string =~ /[\x00-\x09\x0b-\x0d\x0e-\x1f\x7f-\x9f\'\n\s]/
63             or $QUOTE{$string} )
64             {
65 6         20 $string =~ s/\\/\\\\/g;
66 6         10 $string =~ s/"/\\"/g;
67 6         14 $string =~ s/\n/\\n/g;
68 6         11 $string =~ s/[\x85]/\\N/g;
69 6         21 $string =~ s/([\x00-\x1f])/\\$UNPRINTABLE[ord($1)]/g;
70 6         12 $string =~ s/([\x7f-\x9f])/'\x' . sprintf("%X",ord($1))/ge;
  0         0  
71 6         31 return qq|"$string"|;
72             }
73 11 100       41 if ( $string =~ /(?:^[~!@#%&*|>?:,'"`{}\[\]]|^-+$|:\z)/ ) {
74 1         4 return "'$string'";
75             }
76 10 100       34 return $is_key ? $string : qq|"$string"|;
77             }
78              
79             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 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
103             #pod and dumps YAML into a string – via C.
104             #pod
105             #pod Its only oddity is that, when dumping, it prefers double-quoted strings,
106             #pod as illustrated in the L.
107             #pod
108             #pod L is built on the top of L.
109             #pod So it deals with the same YAML subset supported by L.
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 and C as exportable.
142             #pod That means you can use them fully-qualified – as C
143             #pod and C – or you can use an I, like
144             #pod L or L. For example,
145             #pod
146             #pod use zim 'YAML::Hobo' => qw(Dump Load);
147             #pod
148             #pod will make C and C available to the code that follows.
149             #pod
150             #pod =head1 SEE ALSO
151             #pod
152             #pod L
153             #pod
154             #pod =cut
155              
156             __END__