File Coverage

blib/lib/XML/Writer/Simpler.pm
Criterion Covered Total %
statement 44 44 100.0
branch 11 12 91.6
condition 1 2 50.0
subroutine 8 8 100.0
pod 2 2 100.0
total 66 68 97.0


line stmt bran cond sub pod time code
1             package XML::Writer::Simpler;
2              
3 3     3   79788 use 5.006;
  3         12  
  3         147  
4 3     3   20 use strict;
  3         6  
  3         128  
5 3     3   17 use warnings FATAL => 'all';
  3         11  
  3         216  
6              
7             =head1 NAME
8              
9             XML::Writer::Simpler - Perl extension for writing XML data
10              
11             =head1 SYNOPSIS
12              
13              
14             =head1 DESCRIPTION
15              
16             This is a convenience module for writing XML. It's a subclass of
17             C, with a single convenience method so you usually won't have
18             to deal with remembering to call C, C, and
19             C.
20              
21             This module is lies somewhere in between L and
22             L. It nests calls like XML::Generator, but allows for
23             arbitrary subroutine calls as well. It requires fewer braces in general than
24             XML::Writer::Nest.
25              
26             This module is a subclass of XML::Writer, so if you can't do what you want to
27             with the C method, you can fall back to the methods native to XML::Writer.
28              
29             =cut
30              
31 3     3   25 use base qw/Exporter XML::Writer/;
  3         6  
  3         3307  
32 3     3   61391 use vars qw/@EXPORT_OK $VERSION/;
  3         9  
  3         151  
33              
34 3     3   18 use Carp;
  3         5  
  3         951  
35              
36              
37             $VERSION = '0.11';
38             # package vars for ref to output file
39             my $out;
40              
41             =head1 METHODS
42              
43             =over 4
44              
45             =item * C
46              
47             XML::Writer::Simpler->new(%params);
48              
49             Creates the object. Acceptable hash keys for %params are the same as those
50             for L. This module assumes everything is UTF-8, so you can
51             omit that if you like; it will be provided for you.
52              
53             =cut
54              
55             sub new {
56 12     12 1 7997 my ($class, %params) = @_;
57 12   50     74 $params{ENCODING} ||= 'utf-8';
58              
59 12         85 my $self = $class->SUPER::new(%params);
60 12         2087 $out = &{$self->{GETOUTPUT}};
  12         228  
61 12 50       134 $self->xmlDecl('UTF-8') if $params{ENCODING} eq 'utf-8';
62 12         484 bless $self, $class;
63 12         56 return $self;
64             }
65              
66             =item * tag
67              
68             $xml->tag($tagName, [ \@attributes ], $content);
69              
70             If C<$content> is a plain scalar value, will output the tag with that content.
71             If no content is provided, will output an empty tag:
72              
73             $xml->tag('example', 'foo'); # foo
74             $xml->tag('example'); #
75              
76             You may also pass an array ref of key/value pairs that wind up as attributes:
77              
78             # foo
79             $xml->tag('example', [bar => 'baz'], 'foo');
80              
81             #
82             $xml->tag('example', [bar => 'baz']);
83              
84             If C<$content> is a code ref, this will start the tag, execute the code ref,
85             then close the tag. This allows arbitrarily deep/complex tag structures.
86              
87             # Text 1Text 2
88             $xml->tag('example1', sub {
89             $xml->tag('exA', 'Text 1');
90             $xml->tag('exB', 'Text 2');
91             });
92              
93             # 100101102
94             $xml->('example2', sub {
95             for (my $tag = 'a', my $num = 100; $num < 103; $tag++, $num++) {
96             $xml->($tag, $num);
97             }
98             });
99              
100             These different styles of calling can be combined in a number of ways to
101             output basically whatever you like.
102              
103              
104             # text z
105             $xml->tag('example3', [id => 'ex3'], 'text z');
106              
107             # more text
108             $xml->tag('example4', [id => 'ex4'], sub {
109             $xml->tag('exZ', 'more text');
110             });
111              
112              
113             =cut
114              
115             sub tag {
116 18     18 1 593 my $self = shift;
117 18         24 my $tagName = shift; # name of tag, required
118 18         25 my $content = pop; # might be text or a coderef
119 18         23 my $aref = shift; # attribute arrayref
120              
121 18 100       65 croak unless $tagName;
122              
123 17         33 my @attrs;
124             # if we just have a tag name and an array ref, then the thing we popped
125             # off of @_ might actually be the attribute array ref
126 17 100       47 if (ref $content eq 'ARRAY') {
127 2         4 $aref = $content;
128 2         4 undef($content);
129             }
130 17 100       49 @attrs = @$aref if ref $aref eq 'ARRAY';
131              
132 17 100       41 if (ref $content eq 'CODE') {
133 7         25 $self->startTag($tagName, @attrs);
134 7         346 $content->();
135 7         24 $self->endTag($tagName);
136             } else {
137 10 100       21 if ($content) {
138 4         23 $self->dataElement($tagName, $content, @attrs);
139             } else {
140 6         27 $self->emptyTag($tagName, @attrs);
141             }
142             }
143 17         909 return;
144             }
145              
146              
147             1;
148              
149             __END__