CAB/Format/YAML.pm untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 180 CC 20 SCAR 33.8
CC
20
Cov
0%
parseYamlString
72.0
BEGIN
56.0
putDocument
30.0
CRAP 29.4
1 # -*- Mode: CPerl -*-
2 ##
3 ## File: DTA::CAB::Format::YAML.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: Datum parser|formatter: YML code (generic)
6  
7 package DTA::CAB::Format::YAML;
8 use DTA::CAB::Format;
9 use DTA::CAB::Datum ':all';
10 # use YAML::XS;
11 use IO::File;
12 use Carp;
13 use strict;
14  
15 ##==============================================================================
16 ## Globals
17 ##==============================================================================
18  
19 our @ISA = qw(DTA::CAB::Format);
20 our ($lib);
21  
22 BEGIN {
23 DTA::CAB::Format->registerFormat(name=>__PACKAGE__, short=>'yaml', filenameRegex=>qr/\.(?i:ya?ml(?:[\.\-\_]xs)?)$/);
24 DTA::CAB::Format->registerFormat(name=>__PACKAGE__, short=>$_) foreach (qw(yamlxs yaml-xs yml ymlxs yml-xs));
25  
26 ##-- load underlying lib
27 if (!$lib) {
28 eval 'use YAML::XS qw();';
29 $lib = 'YAML::XS' if (!$@);
30 }
31 if (!$lib) {
32 eval 'use YAML::Syck qw();';
33 $lib = 'YAML::Syck' if (!$@);
34 }
35 if (!$lib) {
36 eval 'use YAML qw();';
37 $lib = 'YAML' if (!$@);
38 }
39  
40 undef $@;
41 }
42  
43 ##==============================================================================
44 ## Constructors etc.
45 ##==============================================================================
46  
47 ## $fmt = CLASS_OR_OBJ->new(%args)
48 ## + object structure: assumed HASH
49 ## (
50 ## ##---- Input
51 ## doc => $doc, ##-- buffered input document
52 ## raw => $bool, ##-- if true, parse/print raw data (no document massaging)
53 ##
54 ## ##---- INHERITED from DTA::CAB::Format
55 ## #encoding => $encoding, ##-- n/a: always UTF-8 octets
56 ## level => $formatLevel, ##-- 0:raw, 1:typed, ...
57 ## outbuf => $stringBuffer, ##-- buffered output
58 ## )
59 sub new {
60 my $that = shift;
61 my $fmt = bless({
62 ##-- I/O common
63 #utf8 => 1, ##-- always true, but we don't want the I/O flag set
64  
65 ##-- Input
66 #doc => undef,
67  
68 ##-- Output
69 level => 0,
70 outbuf => '',
71  
72 ##-- user args
73 @_
74 }, ref($that)||$that);
75 return $fmt;
76 }
77  
78 ##==============================================================================
79 ## Methods: Persistence
80 ##==============================================================================
81  
82 ## @keys = $class_or_obj->noSaveKeys()
83 ## + returns list of keys not to be saved
84 sub noSaveKeys {
85 return ($_[0]->SUPER::noSaveKeys, qw(doc outbuf));
86 }
87  
88 ##==============================================================================
89 ## Methods: I/O: generic
90 ##==============================================================================
91  
92 ## @layers = $fmt->iolayers()
93 ## + returns PerlIO layers to use for I/O handles
94 ## + override returns ':raw'
95 sub iolayers {
96 return (':raw');
97 }
98  
99 ##==============================================================================
100 ## Methods: Input
101 ##==============================================================================
102  
103 ##--------------------------------------------------------------
104 ## Methods: Input: Input selection
105  
106 ## $fmt = $fmt->fromString(\$string)
107 ## + override calls $fmt->parseYamlString(\$string)
108 sub fromString {
109 my $fmt = shift;
110 $fmt->close();
111 return $fmt->parseYamlString(ref($_[0]) ? $_[0] : \$_[0]);
112 }
113  
114 ## $fmt = $fmt->fromFile($filename_or_handle)
115 ## + inherited default calls $fmt->fromFh()
116  
117 ## $fmt = $fmt->fromFh($filename_or_handle)
118 ## + override calls $fmt->fromFh_str($fh)
119 sub fromFh {
120 return $_[0]->fromFh_str(@_[1..$#_]);
121 }
122  
123 ##--------------------------------------------------------------
124 ## Methods: Input: Local
125  
126 ## $fmt = $fmt->parseYamlString(\$str)
127 ## + parse a buffered YAML string
128 sub parseYamlString {
129 my $fmt = shift;
130 my $bufr = ref($_[0]) ? $_[0] : \$_[0];
131 utf8::encode($$bufr) if (utf8::is_utf8($$bufr));
132  
133 my ($doc);
134 if (!$lib) {
135 $fmt->logconfess("ParseYamlString(): no underlying YAML library found!");
136 }
137 elsif ($lib eq 'YAML::XS') {
138 $doc = YAML::XS::Load($$bufr)
139 or $fmt->logcluck("ParseYamlString(): YAML::XS::Load() failed: $!");
140 }
141 elsif ($lib eq 'YAML::Syck') {
142 $doc = YAML::Syck::Load($$bufr)
143 or $fmt->logcluck("ParseYamlString(): YAML::Syck::Load() failed: $!");
144 }
145 elsif ($lib eq 'YAML') {
146 $doc = YAML::Load($$bufr)
147 or $fmt->logcluck("ParseYamlString(): YAML::Load() failed: $!");
148 }
149 $fmt->{doc} = $fmt->{raw} ? $doc : $fmt->forceDocument($doc);
150 return $fmt;
151 }
152  
153 ##--------------------------------------------------------------
154 ## Methods: Input: Generic API
155  
156 ## $doc = $fmt->parseDocument()
157 sub parseDocument { return $_[0]{doc}; }
158  
159 ##==============================================================================
160 ## Methods: Output
161 ##==============================================================================
162  
163 ##--------------------------------------------------------------
164 ## Methods: Output: MIME
165  
166 ## $type = $fmt->mimeType()
167 ## + override returns text/yaml
168 #sub mimeType { return 'text/yaml'; }
169 sub mimeType { return 'text/x-yaml'; }
170  
171 ## $ext = $fmt->defaultExtension()
172 ## + returns default filename extension for this format
173 sub defaultExtension { return '.yml'; }
174  
175 ## $short = $fmt->formatName()
176 ## + returns "official" short name for this format
177 ## + default just returns package suffix
178 sub shortName {
179 return 'yaml';
180 }
181  
182 ##--------------------------------------------------------------
183 ## Methods: Output: output selection
184 ## + inherited
185  
186  
187 ##--------------------------------------------------------------
188 ## Methods: Output: Generic API
189  
190 ## \$buf = $fmt->formatBuf(\$buf)
191 ## + formats YAML buffer \$buf according to $fmt->{level}
192 sub formatBuf {
193 #if (!$_[0]{level} || $_[0]{level} >= 2)
194 if ($_[0]{level} && $_[0]{level} >= 2) {
195 #${$_[1]} =~ s/^\-\-\- !!perl\/\w+\:[\w\:]+\n/---\n/sg; ##-- remove yaml typing on doc borders
196 ${$_[1]} =~ s/(?<!^\-\-\- )!!perl\/\w+\:[\w\:]+\n\s*//sg; ##-- remove yaml typing on content
197 }
198 return $_[1];
199 }
200  
201  
202 ## $fmt = $fmt->putToken($tok)
203 sub putToken {
204 my $tmp = YAML::XS::Dump($_[1]);
205 $tmp =~ s/^---\s*//;
206 $_[0]{fh}->print(${$_[0]->formatBuf(\$tmp)});
207 return $_[0];
208 }
209  
210 ## $fmt = $fmt->putSentence($sent)
211 sub putSentence {
212 my $tmp = YAML::XS::Dump($_[1]);
213 $tmp =~ s/^---\s*//;
214 $_[0]{fh}->print(${$_[0]->formatBuf(\$tmp)});
215 return $_[0];
216 }
217  
218 ## $fmt = $fmt->putDocument($doc)
219 sub putDocument {
220 my ($tmp);
221 if (!$lib) {
222 $_[0]->logconfess("putDocument(): no underlying YAML library found!");
223 }
224 elsif ($lib eq 'YAML::XS') {
225 $tmp = YAML::XS::Dump($_[1]);
226 }
227 elsif ($lib eq 'YAML::Syck') {
228 $tmp = YAML::Syck::Dump($_[1]);
229 }
230 elsif ($lib eq 'YAML') {
231 $tmp = YAML::Dump($_[1]);
232 }
233 $_[0]{fh}->print(${$_[0]->formatBuf(\$tmp)});
234 return $_[0];
235 }
236  
237 ## $fmt = $fmt->putData($data)
238 sub putData {
239 $_[0]->putDocument($_[1]);
240 }
241  
242 1; ##-- be happy
243  
244 __END__