File Coverage

blib/lib/Util/XML_YAML_Perl.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Util::XML_YAML_Perl;
2              
3 1     1   29643 use warnings;
  1         2  
  1         32  
4 1     1   5 use strict;
  1         2  
  1         33  
5              
6 1     1   505 use XML::Simple;
  0            
  0            
7             use YAML;
8             use YAML::AppConfig;
9              
10             =head1 NAME
11              
12             Util::XML_YAML_Perl - Interconversion between PERL,YAML and XML.
13              
14             =head1 VERSION
15              
16             Version 1.0.0
17              
18             =cut
19              
20             our $VERSION = '1.0.0';
21              
22             =head1 SYNOPSIS
23              
24             This module serves as single point for all typical operations done on these three -PERL,YAML and XML. Also this doesnt degrade any of advanced features provided by independent modules.
25              
26             This will serve as quick point for interconversions saving time of experimenting with atleast two independent modules.
27              
28             use XML::YAML_PERL;
29              
30             #create XML::YAML_PERL object
31             my $obj=XML::YAML_PERL->new();
32            
33             my $perl_ref=$obj->xml_to_perl($filename);
34             my $b=$obj->xml_to_perl('-');
35              
36             my $y_ref=$obj->perl_to_yaml($hash);
37             $y_ref=$obj->perl_to_yaml($hash,$file);
38             $y_ref->dump($file);
39             $y_ref->dump();
40             $y_ref->get_h;
41             $y_ref->get("h");
42             $y_ref->set("h",10);
43             $y_ref->set_h(10);
44             $y_ref->config->{h}=10;
45              
46             my $yaml_ref=$obj->xml_to_yaml($filename);
47              
48             my $xml_string=$obj->perl_to_xml($perl_ref);
49             my $xml_file=$obj->perl_to_xml($perl_ref,$options);
50              
51             my $hash=$obj->yaml_to_perl('m.yml');
52             my $hash=$obj->yaml_to_perl($string);
53              
54             my $xml_string=$obj->yaml_to_xml($yaml_ref);
55             my $xml_file=$obj->yaml_to_xml($yaml_ref,$op);
56            
57              
58             read more about methods with examples below.
59              
60              
61             =cut
62              
63             =head1 METHODS
64              
65             =cut
66              
67             =head2 new ()
68              
69             Retuns a bless object.
70             This module has no exported methods to it has to be used in OOO way only.
71              
72             my $obj = XML::YAML_PERL->new();
73              
74             =cut
75              
76             sub new {
77             my $class= shift;
78              
79             return bless {}, $class;
80              
81             }
82              
83             =head2 xml_to_perl ( $xml_file,$options )
84              
85             Convert XML into perl hash.
86              
87             my $perl_ref=$obj->xml_to_perl($filename);
88              
89             To read from STDIN, File name should be '-'.
90              
91             Options of XML::Simple can be used. C<$options> is a ref to array consisting of options available on XML::Simple.
92              
93             returns perl hash
94              
95             =head3 EXAMPLES:
96              
97             1)
98             $ cat a.xml
99            
100            
101             John Doe
102             43
103             M
104             Operations
105            
106              
107             my $b=$obj->xml_to_perl('a.xml');
108             print "".Dumper($b);
109              
110             $VAR1 = {
111             'department' => 'Operations',
112             'name' => 'John Doe',
113             'sex' => 'M',
114             'age' => '43'
115             };
116              
117             2)
118             my $b=$obj->xml_to_perl('-');
119             #Such a call with wait for XML input to be teminated by Ctrl+d
120              
121             $ perl c.pl
122            
123             John Doe
124             43
125             M
126             Operations
127            
128             [CTRL+D]
129             $VAR1 = {
130             'department' => 'Operations',
131             'name' => 'John Doe',
132             'sex' => 'M',
133             'age' => '43'
134             };
135              
136             =cut
137              
138             sub xml_to_perl {
139              
140             my ( $self, $xml_file,$options) = @_;
141              
142             my $xs = XML::Simple->new(@$options);
143             my $perl_ref = $xs->XMLin($xml_file);
144              
145             return $perl_ref;
146              
147             }
148              
149             =head2 perl_to_yaml($perl_ref,$file)
150              
151             Converts perl hash into YAML string or file.
152              
153             $perl_ref - perl hash
154             $file - File to write into (optional)
155              
156             returns YAML::AppConfig object
157              
158              
159             my $hash={h=>1, m=>4};
160             my $y_ref=$obj->perl_to_yaml($hash);
161             $y_ref=$obj->perl_to_yaml($hash,$file);
162              
163             print $y_ref->dump(); #prints Yaml equivalent for perl object
164             $y_ref->dump($file); #prints Yaml equivalent into C<$file>for perl object
165              
166             $y_ref->get_h; #returns value of h
167             $y_ref->get("h"); #returns value of h
168              
169             # Set etc_dir in three different ways, all equivalent.
170             $y_ref->set("h",10);
171             $y_ref->set_h(10);
172             $y_ref->config->{h}=10;
173              
174             Above described dump,get_*/set_* methods will work on Yaml reference objects returned by other subroutines in this module.
175              
176             =cut
177              
178             sub perl_to_yaml {
179              
180             my ($self,$perl_ref,$file)=@_;
181              
182             my $yaml_string=Dump($perl_ref);
183             my $yaml_ref= YAML::AppConfig->new(string => $yaml_string);
184              
185             $yaml_ref->dump($file) if (defined $file);
186             return $yaml_ref;
187              
188             }
189              
190             =head2 xml_to_yaml($xml_file,$options)
191              
192             Converts XML string/file into YAML string or file.
193              
194             $xml_file- XML file path
195             $options- to be drawn from XML::Simple[optional]
196              
197             returns YAML::AppConfig object
198              
199             dump()
200             get_*()
201             set_*()
202              
203             read how above methods work from perl_to_yaml().
204              
205             =cut
206              
207              
208             sub xml_to_yaml {
209              
210             my ($self,$xml_file,$options) = @_;
211              
212             my $xs = XML::Simple->new(@$options);
213             my $perl_ref = $xs->XMLin($xml_file);
214              
215             my $yaml_string=Dump($perl_ref);
216             my $yaml_ref= YAML::AppConfig->new(string => $yaml_string);
217              
218             return $yaml_ref;
219              
220             }
221              
222             =head2 perl_to_xml($perl_ref,$options)
223              
224             Converts perl hashes into XML string/File
225            
226             $perl_ref - refernce to perl hash
227             $options- to be drawn from XML::Simple[optional]
228              
229             returns XML string
230             returns 1 if file written.
231              
232             open my $fh, '>:encoding(iso-8859-1)', $path or die "open($path): $!";
233             my $op=[OutputFile => $fh];
234              
235             my $return_val=$obj->perl_to_xml($perl_ref,$options)
236              
237             =head3 EXAMPLES:
238              
239             my $hash={h=>1, m=>4};
240             my $d=$obj->perl_to_xml($hash);
241              
242              
243             open(my $fh,">","ur.xml") or die $!;
244             my $op=[OutputFile => $fh];
245             my $d=$obj->perl_to_xml($hash,$op);
246              
247             =cut
248              
249             sub perl_to_xml {
250              
251             my ($self,$perl_ref,$options)=@_;
252            
253             my $xs = XML::Simple->new(@$options);
254             my $xml= $xs->XMLout($perl_ref) or die $!;
255              
256             return $xml;
257              
258             }
259              
260             =head2 yaml_to_perl($yaml_ref)
261              
262             Converts YAML hashes into perl.
263            
264             $yaml_ref - can be YAML string /YAML syntax file
265            
266             returns reference to perl hash
267              
268             =head3 EXAMPLES:
269            
270             1)
271             $cat m.yaml
272             ---
273             age: 43
274             department: Operations
275             name: John Doe
276             sex: M
277              
278             my $b=$obj->yaml_to_perl('m.yml');
279             print Dumper($b);
280              
281             $VAR1 = {
282             'department' => 'Operations',
283             'name' => 'John Doe',
284             'sex' => 'M',
285             'age' => '43'
286             };
287             2)
288             my $string='---
289             age: 43
290             department: Operations
291             name: John Doe
292             sex: M
293             ';
294              
295             my $b=$obj->yaml_to_perl($string);
296             print Dumper($b);
297              
298             $VAR1 = {
299             'department' => 'Operations',
300             'name' => 'John Doe',
301             'sex' => 'M',
302             'age' => '43'
303             };
304              
305             =cut
306              
307             sub yaml_to_perl {
308            
309             my ( $self, $yaml_ref)=@_;
310            
311             my $perl_ref;
312              
313             if( $yaml_ref =~ m/.*\n.*/){
314              
315             $perl_ref= YAML::Load($yaml_ref);
316              
317             }elsif(-e $yaml_ref){
318              
319             $perl_ref= YAML::LoadFile($yaml_ref);
320             }else {
321              
322             die "YAML file/string not found for conversion\n";
323              
324             }
325              
326             return $perl_ref;
327             }
328              
329              
330             =head2 yaml_to_xml($yaml_ref,$options)
331            
332             Convert YAML string/file into XML file/string
333            
334             $yaml_ref - can be YAML string /YAML syntax file
335             $options - you are free to use XML::Simple options
336             [ OutputFile => $file_handle ]
337              
338             Returns XML string. Incase of writing to file returns 1;
339              
340             =head3 EXAMPLES:
341            
342             my $xml_string=$obj->yaml_to_xml($yaml_ref);
343              
344             #to write XML to file
345             open ( $c,">", "kl.xml") or die $!;
346             my $op=[OutputFile=>$c];
347             my $return_val=$obj->yaml_to_xml($yaml_ref,$op); #returns 1 on success
348              
349             =cut
350              
351              
352             sub yaml_to_xml {
353              
354             my ( $self,$yaml_ref,$options) =@_;
355              
356             my $perl_ref;
357              
358             if( $yaml_ref =~ m/.*\n.*/){
359              
360             $perl_ref= YAML::Load($yaml_ref);
361              
362             }elsif(-e $yaml_ref){
363              
364             $perl_ref= YAML::LoadFile($yaml_ref);
365             } else {
366              
367             die "YAML file/string not found for conversion\n";
368              
369             }
370            
371             my $xs = XML::Simple->new(@$options);
372             my $xml= $xs->XMLout($perl_ref) or die $!;
373              
374             return $xml;
375              
376             }
377              
378             =head1 AUTHOR
379              
380             Feel free to abuse or praise me.
381              
382             Ravi Chandra. M, C<< >>
383              
384             =head1 CHANGES
385              
386             2011-08-18, v1.0.0 - rchandram
387              
388             =head1 BUGS
389              
390             Please report any bugs or feature requests to C, or through
391             the web interface at L. I will be notified, and then you'll
392             automatically be notified of progress on your bug as I make changes.
393              
394             =head1 SUPPORT
395              
396             You can find documentation for this module with the perldoc command.
397              
398             perldoc Util::XML_YAML_Perl
399              
400              
401             You can also look for information at:
402              
403             =over 4
404              
405             =item * RT: CPAN's request tracker
406              
407             L
408              
409             =item * AnnoCPAN: Annotated CPAN documentation
410              
411             L
412              
413             =item * CPAN Ratings
414              
415             L
416              
417             =item * Search CPAN
418              
419             L
420              
421             =back
422              
423              
424             =cut
425              
426             1;
427