File Coverage

blib/lib/YAML/Hobo.pm
Criterion Covered Total %
statement 15 29 51.7
branch 9 16 56.2
condition 2 6 33.3
subroutine 3 4 75.0
pod 2 2 100.0
total 31 57 54.3


line stmt bran cond sub pod time code
1              
2             package YAML::Hobo;
3             $YAML::Hobo::VERSION = '0.1.0';
4             # ABSTRACT: Poor man's YAML
5              
6             BEGIN {
7 2     2   44656 require YAML::Tiny;
8 2         8368 YAML::Tiny->VERSION('1.70');
9 2         691 our @ISA = qw(YAML::Tiny);
10             }
11              
12             our @EXPORT_OK = qw(Dump Load);
13              
14             sub Dump {
15 1     1 1 75 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             sub _dump_scalar {
30 7     7   98 my $string = $_[1];
31 7         8 my $is_key = $_[2];
32              
33             # Check this before checking length or it winds up looking like a string!
34 7         10 my $has_string_flag = YAML::Tiny::_has_internal_string_value($string);
35 7 50       47 return '~' unless defined $string;
36 7 50       12 return "''" unless length $string;
37 7 100       14 if ( Scalar::Util::looks_like_number($string) ) {
38              
39             # keys and values that have been used as strings get quoted
40 1 50 33     6 if ( $is_key || $has_string_flag ) {
41 1         4 return qq|"$string"|;
42             }
43             else {
44 0         0 return $string;
45             }
46             }
47 6 50       15 if ( $string =~ /[\x00-\x09\x0b-\x0d\x0e-\x1f\x7f-\x9f\'\n]/ ) {
48 0         0 $string =~ s/\\/\\\\/g;
49 0         0 $string =~ s/"/\\"/g;
50 0         0 $string =~ s/\n/\\n/g;
51 0         0 $string =~ s/[\x85]/\\N/g;
52 0         0 $string =~ s/([\x00-\x1f])/\\$UNPRINTABLE[ord($1)]/g;
53 0         0 $string =~ s/([\x7f-\x9f])/'\x' . sprintf("%X",ord($1))/ge;
  0         0  
54 0         0 return qq|"$string"|;
55             }
56 6 50 33     29 if ( $string =~ /(?:^[~!@#%&*|>?:,'"`{}\[\]]|^-+$|\s|:\z)/
57             or $QUOTE{$string} )
58             {
59 0         0 return "'$string'";
60             }
61 6 100       19 return $is_key ? $string : qq|"$string"|;
62             }
63              
64             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 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
88             #pod and dumps YAML into a string – via C.
89             #pod
90             #pod Its only oddity is that, when dumping, it prefers double-quoted strings,
91             #pod as illustrated in the L.
92             #pod
93             #pod L is built on the top of L.
94             #pod So it deals with the same YAML subset supported by L.
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 and C as exportable.
127             #pod That means you can use them fully-qualified – as C
128             #pod and C – or you can use an I, like
129             #pod L or L. For example,
130             #pod
131             #pod use zim 'YAML::Hobo' => qw(Dump Load);
132             #pod
133             #pod will make C and C available to the code that follows.
134             #pod
135             #pod =head1 SEE ALSO
136             #pod
137             #pod L
138             #pod
139             #pod =cut
140              
141             __END__