blib/lib/Data/XML/Variant.pm

filter: Statementstmt 68.7% 11 / 16 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 3 / 4 Podpod 100% 1 / 1 totaltotal 65.2% 15 / 23 CC 2 SCAR 11.0
CC
2
Cov
61%
new
6.0
BEGIN
1.0
BEGIN
1.0
CRAP 3.0
« blib/lib/Data/XML/Variant/Build.pm ↑ Summary lib/Data/XML/Variant/Output.pm »
1 package Data::XML::Variant;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use aliased 'Data::XML::Variant::Build';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Data::XML::Variant - Output XML 'variants'
11  
12 =head1 VERSION
13  
14 Version 0.01
15  
16 =cut
17  
18 1 our $VERSION = '0.02';
19  
20 =head1 SYNOPSIS
21  
22 use Data::XML::Variant;
23  
24 my $xml = Data::XML::Variant->new(
25 {
26 'doc' => 'doc',
27 'ns:customer' => 'customer',
28 'sales' => 'sales',
29 }
30 );
31 print $xml->doc(
32 [ 'xmlns:customer' => $url ],
33 $xml->customer([id => 1], 'bob'),
34 $xml->customer([id => 2], 'alice'),
35 $xml->customer([id => 3], 'charlie'),
36 $xml->sales("3000.12"),
37 );
38 __END__
39 <doc xmlns:ns="$url">
40 <ns:customer id="1">bob</ns:customer>
41 <ns:customer id="2">alice</ns:customer>
42 <ns:customer id="3">charlie</ns:customer>
43 <sales>3000.12</sales>
44 </doc>
45
46 B<Note>: newlines and indentation were added in the above example for
47 illustrative purposes only. See C<NEWLINES> in the
48 L<Data::XML::Variant::Build> documentation.
49  
50 =head1 DESCRIPTION
51  
52 Many shops have "legacy" XML variants which look very similar to XML but
53 aren't quite there. Unfortunately, this means that most XML generating tools
54 will not build this "legacy" XML. Sometimes they require attributes in a
55 specific order, don't quote attributes, fail to close tags, use illegal
56 characters, etc. Rather than writing your own code to produce bad XML, this
57 module at least allows you to do this systematically.
58  
59 L<Data::XML::Variant> makes it very, very easy to write XML. It also makes it
60 very easy to shoot yourself in the foot. You are responsible for B<all>
61 output. If you get it wrong, you've been warned. On the plus side, you don't
62 have to worry about namespaces, inserting arbitrary data (very handy if you
63 have an XML snippet from somewhere else), or remembering a lot of complicated
64 stuff.
65  
66 This module should B<not> be used casually. It assumes that you, the
67 programmer, really want to do what you're asking it to do. As a result, do
68 not use this module unless you have a test suite which can verify that you're
69 really creating the XML you need. It's easy to create bogus XML with this.
70  
71 =head1 EXPORT
72  
73 None.
74  
75 =head1 METHODS
76  
77 =head2 new
78  
79 my $xml = XML::Compose->new(
80 {
81 $tag1 => $method1,
82 $tag2 => $method2,
83 }
84 );
85  
86 C<new> accepts a hashref of tags and their appropriate methods. Methods must
87 begin with a lower-case later. Tags will be built with the exact text used.
88 Namespaces will B<not> be checked, validated, whatever.
89  
90 See the C<Add> method in C<Data::XML::Variant::Build> for more information
91 about how methods are added.
92  
93 =cut
94  
95 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
96 0 my ($class, $tags) = @_;
97 0 my $build = Build->New;
98 0 ▶ while (my ($tag, $method) = each %$tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$tags) 0 0 0
99 0 $build->Add($tag, $method);
100 }
101 0 return $build;
102 }
103  
104 =head2 Instance methods.
105  
106 See L<Data::XML::Variant::Build> for a list of built-in methods. Public
107 methods I<all> begin with an upper case letter to distinguish them from the
108 methods you add as tags.
109  
110 =head2 A more complicated example
111  
112 Just to let you see a better example:
113  
114 my $xml = Data::XML::Variant->new({
115 'ns:foo' => 'foo',
116 'bar' => 'bar',
117 'ns2:baz' => 'baz',
118 });
119 print $xml->Decl, # add declaration (optional)
120 $xml->PI('xml-stylesheet', [type => 'text/xsl', href=>"$xslt_url"]),
121 $xml->foo(
122 [ id => 3, 'xmlns:ns2' => $url],
123 $xml->bar('silly'),
124 $xml->Comment('this is a > comment'),
125 $xml->baz(['asdf:some_attr' => 'value'], 'whee!'),
126 );
127  
128 __END__
129 <?xml version="1.0">
130 <?xml-stylesheet type="text/xsl" href="$xslt_url"?>
131 <ns:foo id="3" xmlns:ns2="$url">
132 <bar>silly</bar>
133 <!-- this is a &gt; comment -->
134 <ns2:baz asdf:some_attr="value">whee!</ns2:baz>
135 </ns:foo>
136  
137 =head1 SHOULD YOU USE THIS MODULE?
138  
139 You probably don't want to use this module if you need to produce well-formed
140 XML. However, this module I<will> allow you to do that. Regardless of
141 whether or not you use this module, because XML variants are frequently
142 sloppy, you should not use this unless you are in the habit of writing
143 automated tests. It's very easy to generate XML (or something similar) which
144 does not match your expectations.
145  
146 =head1 AUTHOR
147  
148 Curtis "Ovid" Poe, C<< <ovid@cpan.org> >>
149  
150 =head1 BUGS
151  
152 Please report any bugs or feature requests to
153 C<bug-data-xml-variant@rt.cpan.org>, or through the web interface at
154 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-XML-Variant>.
155 I will be notified, and then you'll automatically be notified of progress on
156 your bug as I make changes.
157  
158 =head1 ACKNOWLEDGEMENTS
159  
160 =head1 COPYRIGHT & LICENSE
161  
162 Copyright 2005 Curtis "Ovid" Poe, all rights reserved.
163  
164 This program is free software; you can redistribute it and/or modify it
165 under the same terms as Perl itself.
166  
167 =cut
168  
169 1 1; # End of Data::XML::Variant
« blib/lib/Data/XML/Variant/Build.pm ↑ Summary lib/Data/XML/Variant/Output.pm »