| 1 | package XML::Writer::Simpler; | ||||||||||||
| 2 | |||||||||||||
| 3 | 4 | ▶ | use 5.006; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 4 | ▶ | use warnings FATAL => 'all'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 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<XML::Writer>, with a single convenience method so you usually won't have | ||||||||||||
| 18 | to deal with remembering to call C<startTag()>, C<endTag()>, and | ||||||||||||
| 19 | C<emptyTag()>. | ||||||||||||
| 20 | |||||||||||||
| 21 | This module is lies somewhere in between L<XML::Generator|XML::Generator> and | ||||||||||||
| 22 | L<XML::Writer::Nest>. 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<tag> method, you can fall back to the methods native to XML::Writer. | ||||||||||||
| 28 | |||||||||||||
| 29 | =cut | ||||||||||||
| 30 | |||||||||||||
| 31 | 4 | ▶ | use base qw/Exporter XML::Writer/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 32 | 4 | ▶ | use vars qw/@EXPORT_OK $VERSION/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 33 | |||||||||||||
| 34 | 4 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 35 | |||||||||||||
| 36 | |||||||||||||
| 37 | 4 | $VERSION = '0.11'; | |||||||||||
| 38 | # package vars for ref to output file | ||||||||||||
| 39 | 4 | my $out; | |||||||||||
| 40 | |||||||||||||
| 41 | =head1 METHODS | ||||||||||||
| 42 | |||||||||||||
| 43 | =over 4 | ||||||||||||
| 44 | |||||||||||||
| 45 | =item * C<new> | ||||||||||||
| 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<XML::Writer>. 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 | 12 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||
| 56 | 12 | my ($class, %params) = @_; | |||||||||||
| 57 | 12 | ▶ | $params{ENCODING} ||= 'utf-8'; | ||||||||||
|
Condition50%1/2
$params{"ENCODING"} ||= "utf-8"
MC/DC0%0/1
$params{"ENCODING"} ||= "utf-8"
$params{"ENCODING"}
Truth table50%1/2
A
A= $params{"ENCODING"}
| |||||||||||||
| 58 | |||||||||||||
| 59 | 12 | my $self = $class->SUPER::new(%params); | |||||||||||
| 60 | 12 | $out = &{$self->{GETOUTPUT}}; | |||||||||||
| 61 | 12 | ▶ | $self->xmlDecl('UTF-8') if $params{ENCODING} eq 'utf-8'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 12 | bless $self, $class; | |||||||||||
| 63 | 12 | 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'); # <example>foo</example> | ||||||||||||
| 74 | $xml->tag('example'); # <example /> | ||||||||||||
| 75 | |||||||||||||
| 76 | You may also pass an array ref of key/value pairs that wind up as attributes: | ||||||||||||
| 77 | |||||||||||||
| 78 | # <example bar="baz">foo</example> | ||||||||||||
| 79 | $xml->tag('example', [bar => 'baz'], 'foo'); | ||||||||||||
| 80 | |||||||||||||
| 81 | # <example bar="baz" /> | ||||||||||||
| 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 | # <example1><exA>Text 1</exA><exB>Text 2</exB></example1> | ||||||||||||
| 88 | $xml->tag('example1', sub { | ||||||||||||
| 89 | $xml->tag('exA', 'Text 1'); | ||||||||||||
| 90 | $xml->tag('exB', 'Text 2'); | ||||||||||||
| 91 | }); | ||||||||||||
| 92 | |||||||||||||
| 93 | # <example2><a>100</a><b>101</b><c>102</c></example2> | ||||||||||||
| 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 | # <example3 id='ex3'>text z</example3> | ||||||||||||
| 105 | $xml->tag('example3', [id => 'ex3'], 'text z'); | ||||||||||||
| 106 | |||||||||||||
| 107 | # <example4 id="ex4"><exZ>more text</exZ></example4> | ||||||||||||
| 108 | $xml->tag('example4', [id => 'ex4'], sub { | ||||||||||||
| 109 | $xml->tag('exZ', 'more text'); | ||||||||||||
| 110 | }); | ||||||||||||
| 111 | |||||||||||||
| 112 | |||||||||||||
| 113 | =cut | ||||||||||||
| 114 | |||||||||||||
| 115 | 18 | ▶ | sub tag { | ||||||||||
|
Subroutine
tag: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||
| 116 | 18 | my $self = shift; | |||||||||||
| 117 | 18 | my $tagName = shift; # name of tag, required | |||||||||||
| 118 | 18 | my $content = pop; # might be text or a coderef | |||||||||||
| 119 | 18 | my $aref = shift; # attribute arrayref | |||||||||||
| 120 | |||||||||||||
| 121 | 18 | ▶ | croak unless $tagName; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 122 | |||||||||||||
| 123 | 17 | 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 | ▶ | if (ref $content eq 'ARRAY') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 127 | 2 | $aref = $content; | |||||||||||
| 128 | 2 | undef($content); | |||||||||||
| 129 | } | ||||||||||||
| 130 | 17 | ▶ | @attrs = @$aref if ref $aref eq 'ARRAY'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 131 | |||||||||||||
| 132 | 17 | ▶ | if (ref $content eq 'CODE') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 133 | 7 | $self->startTag($tagName, @attrs); | |||||||||||
| 134 | 7 | $content->(); | |||||||||||
| 135 | 7 | $self->endTag($tagName); | |||||||||||
| 136 | } else { | ||||||||||||
| 137 | 10 | ▶ | if ($content) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 138 | 4 | $self->dataElement($tagName, $content, @attrs); | |||||||||||
| 139 | } else { | ||||||||||||
| 140 | 6 | $self->emptyTag($tagName, @attrs); | |||||||||||
| 141 | } | ||||||||||||
| 142 | } | ||||||||||||
| 143 | 17 | return; | |||||||||||
| 144 | } | ||||||||||||
| 145 | |||||||||||||
| 146 | |||||||||||||
| 147 | 4 | 1; | |||||||||||
| 148 | |||||||||||||
| 149 | __END__ | ||||||||||||