Rabbit.pm

filter: Statementstmt 93.3% 28 / 30 Branchbran 25.0% 1 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.8% 8 / 9 Podpod 100% 1 / 1 totaltotal 86.3% 38 / 44 CC 3 SCAR 7.4
CC
3
Cov
85%
unimport
6.0
import
2.0
BEGIN
1.0
CRAP 2.1
« Rabbit/Trait/XPathObjectMap.pm ↑ Summary Rabbit/Root.pm »
1 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package XML::Rabbit;
5 {
6 6 $XML::Rabbit::VERSION = '0.4.0';
7 }
8 6 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # ABSTRACT: Consume XML with Moose and xpath queries
11  
12 6 ▶use XML::Rabbit::Sugar (); # no magic, just load
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use namespace::autoclean (); # no cleanup, just load
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 my ($import, $unimport, $init_meta) = Moose::Exporter->build_import_methods(
17 also => 'XML::Rabbit::Sugar',
18 base_class_roles => ['XML::Rabbit::Node'],
19 );
20  
21  
22 sub import {
23 11 ▶ namespace::autoclean->import( -cleanee => scalar caller );
Subroutine import: called CC=2 SCAR=7.1
24 11 ▶ return unless $import;
Branch: 1 branch
BranchTrueFalseTotal
unless $import 0 11 11
25 11 goto &$import;
26 }
27  
28  
29 sub unimport {
30 0 ▶ return unless $unimport;
Subroutine unimport: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $unimport 0 0 0
31 0 goto &$unimport;
32 }
33  
34  
35 #sub init_meta {
36 # return unless $init_meta;
37 # goto &$init_meta;
38 #}
39  
40 # FIXME: https://rt.cpan.org/Ticket/Display.html?id=51561
41 # Hopefully fixed by 2.06 (doy)
42 sub init_meta {
43 11 ▶ my ($dummy, %opts) = @_;
Subroutine init_meta: called CC=1 SCAR=0.0
Pod documented
44 11 Moose->init_meta(%opts);
45 Moose::Util::MetaRole::apply_base_class_roles(
46 for => $opts{for_class},
47 11 roles => ['XML::Rabbit::Node']
48 );
49 11 return Class::MOP::class_of($opts{for_class});
50 }
51  
52 6 1;
53  
54 __END__
55  
56 =pod
57  
58 =encoding utf-8
59  
60 =head1 NAME
61  
62 XML::Rabbit - Consume XML with Moose and xpath queries
63  
64 =head1 VERSION
65  
66 version 0.4.0
67  
68 =head1 SYNOPSIS
69  
70 my $xhtml = W3C::XHTML->new( file => 'index.xhtml' );
71 print "Title: " . $xhtml->title . "\n";
72 print "First image source: " . $xhtml->body->images->[0]->src . "\n";
73  
74 exit;
75  
76 package W3C::XHTML;
77 use XML::Rabbit::Root;
78  
79 add_xpath_namespace 'xhtml' => 'http://www.w3.org/1999/xhtml';
80  
81 has_xpath_value 'title' => '/xhtml:html/xhtml:head/xhtml:title';
82  
83 has_xpath_object 'body' => '/xhtml:html/xhtml:body' => 'W3C::XHTML::Body';
84  
85 has_xpath_object_list 'all_anchors_and_images' => '//xhtml:a|//xhtml:img',
86 {
87 'xhtml:a' => 'W3C::XHTML::Anchor',
88 'xhtml:img' => 'W3C::XHTML::Image',
89 },
90 ;
91  
92 finalize_class();
93  
94 package W3C::XHTML::Body;
95 use XML::Rabbit;
96  
97 has_xpath_object_list 'images' => './/xhtml:img' => 'W3C::XHTML::Image';
98  
99 finalize_class();
100  
101 package W3C::XHTML::Image;
102 use XML::Rabbit;
103  
104 has_xpath_value 'src' => './@src';
105 has_xpath_value 'alt' => './@alt';
106 has_xpath_value 'title' => './@title';
107  
108 finalize_class();
109  
110 package W3C::XHTML::Anchor;
111 use XML::Rabbit;
112  
113 has_xpath_value 'href' => './@src';
114 has_xpath_value 'title' => './@title';
115  
116 finalize_class();
117  
118 =head1 DESCRIPTION
119  
120 XML::Rabbit is a Moose-based class construction toolkit you can use to make
121 XPath-based XML extractors with very little code. Each attribute in your
122 class created with the above helper function is linked to an XPath query
123 that is executed on your XML document when you request the value. Creating
124 object hierarchies that mimic the layout of the XML document is almost as
125 easy as doing a search and replace on the XML DTD (if you have one).
126  
127 You can return multiple values from the XML either as arrays or hashes,
128 depending on how you need to work with the data from the XML document.
129  
130 The example in the synopsis shows how to create a class hierarchy that
131 enables easy retrival of certain information from an XHTML document.
132  
133 Also notice that if you specify an xpath query that can return multiple XML
134 elements, you need to specify a hash map (xml tag => class name) instead of
135 just specifying the class name returned.
136  
137 All the array and hash-returning attributes are tagged with the Array and
138 Hash native traits, so it is quick and easy to specify additional
139 delegations. All the C<has_xpath_value*> helpers expect the value to be of
140 a C<Str> type constraint. Array and hash-returning attributes automatically
141 set their isa to C<ArrayRef[Str]> and C<HashRef[Str]> respectively.
142  
143 All the helper methods and their associated arguments are explained in
144 L<XML::Rabbit::Sugar> detail.
145  
146 Be aware that if your XML document contains a default XML namespace (like
147 XHTML does), you MUST specify it with C<add_xpath_namespace()>, or else your
148 xpath queries will not match anything. The XML document is not scanned for
149 XML namespaces during initialization.
150  
151 =head1 FUNCTIONS
152  
153 =head2 import
154  
155 Automatically loads L<namespace::autoclean> into the caller's package and
156 dispatches to L<XML::Rabbit::Sugar/"import"> (tail call).
157  
158 =head2 unimport
159  
160 Dispatches to L<XML::Rabbit::Sugar/"unimport"> (tail call).
161  
162 =head2 init_meta
163  
164 Initializes the metaclass of the calling class and adds the role
165 L<XML::Rabbit::Node>.
166  
167 =head1 IMPORTS
168  
169 When you specify C<use XML::Rabbit::Root> (or C<use XML::Rabbit> in child
170 nodes) you do the equivalent of the following code:
171  
172 use Moose;
173 with "XML::Rabbit::RootNode"; # if you use XML::Rabbit::Root
174 with "XML::Rabbit::Node"; # if you use XML::Rabbit
175 use namespace::autoclean;
176 use XML::Rabbit::Sugar;
177  
178 This ensures that you don't have to specify C<no Moose> at the end of your
179 code to clean up imported functions you have used in your class.
180  
181 The C<finalize_class()> call at the end of the file is the same as writing
182 the following piece of code:
183  
184 __PACKAGE__->meta->make_immutable();
185  
186 1;
187  
188 This optimizes the class and ensures it always returns a true value, which
189 is required to successfully load a Perl script file.
190  
191 =head1 TECHNICAL DETAILS
192  
193 The trait applied to the attribute will wrap the type constraint union in an
194 ArrayRef if the trait name is XPathObjectList and as a HashRef if the trait
195 name is XPathObjectMap. As all the traits that end with List return array
196 references, their C<isa> must be an ArrayRef. The same is valid for the
197 *Map traits, just that they return HashRef instead of ArrayRef.
198  
199 The namespace prefix used in C<isa_map> MUST be specified in the
200 C<namespace_map>. If a prefix is used in C<isa_map> without a corresponding
201 entry in C<namespace_map> an exception will be thrown.
202  
203 =head1 CAVEATS
204  
205 Be aware of the syntax of XPath when used with namespaces. You should almost
206 always define C<namespace_map> when dealing with XML that use namespaces.
207 Namespaces explicitly declared in the XML are usable with the prefix
208 specified in the XML (except if you use C<isa_map>). Be aware that a prefix
209 must usually be declared for the default namespace (xmlns=...) to be able to
210 use it in XPath queries. See the example above (on XHTML) for details. See
211 L<XML::LibXML::Node/findnodes> for more information.
212  
213 Because XML::Rabbit uses XML::LibXML's DOM parser it is limited to handling
214 XML documents that can fit in available memory. Unfortunately there is no
215 easy way around this, because XPath queries need to work on a tree model,
216 and I am not aware of any way of doing that without keeping the document in
217 memory. Luckily XML::LibXML's DOM implementation is written in C, so it
218 should use much less memory than a pure Perl DOM parser.
219  
220 =head1 SEMANTIC VERSIONING
221  
222 This module uses semantic versioning concepts from L<http://semver.org/>.
223  
224 =head1 ACKNOWLEDGEMENTS
225  
226 The following people have helped to review or otherwise encourage
227 me to work on this module.
228  
229 Chris Prather (perigrin)
230  
231 Matt S. Trout (mst)
232  
233 Stevan Little (stevan)
234  
235 =head1 SEE ALSO
236  
237 =over 4
238  
239 =item *
240  
241 L<Moose>
242  
243 =item *
244  
245 L<XML::LibXML>
246  
247 =item *
248  
249 L<namespace::autoclean>
250  
251 =item *
252  
253 L<XML::Toolkit>
254  
255 =item *
256  
257 L<XML::Twig>
258  
259 =item *
260  
261 L<Mojo::DOM>
262  
263 =item *
264  
265 L<XPath tutorial|http://zvon.org/comp/r/tut-XPath_1.html>
266  
267 =item *
268  
269 L<XPath Specification|http://www.w3.org/TR/xpath/>
270  
271 =item *
272  
273 L<Implementing WWW::LastFM, a client library to the Last.FM API, with XML::Rabbit|http://blog.robin.smidsrod.no/2011/09/30/implementing-www-lastfm-part-1>
274  
275 =back
276  
277 =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
278  
279 =head1 SUPPORT
280  
281 =head2 Perldoc
282  
283 You can find documentation for this module with the perldoc command.
284  
285 perldoc XML::Rabbit
286  
287 =head2 Websites
288  
289 The following websites have more information about this module, and may be of help to you. As always,
290 in addition to those websites please use your favorite search engine to discover more resources.
291  
292 =over 4
293  
294 =item *
295  
296 MetaCPAN
297  
298 A modern, open-source CPAN search engine, useful to view POD in HTML format.
299  
300 L<http://metacpan.org/release/XML-Rabbit>
301  
302 =item *
303  
304 Search CPAN
305  
306 The default CPAN search engine, useful to view POD in HTML format.
307  
308 L<http://search.cpan.org/dist/XML-Rabbit>
309  
310 =item *
311  
312 RT: CPAN's Bug Tracker
313  
314 The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
315  
316 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=XML-Rabbit>
317  
318 =item *
319  
320 AnnoCPAN
321  
322 The AnnoCPAN is a website that allows community annotations of Perl module documentation.
323  
324 L<http://annocpan.org/dist/XML-Rabbit>
325  
326 =item *
327  
328 CPAN Ratings
329  
330 The CPAN Ratings is a website that allows community ratings and reviews of Perl modules.
331  
332 L<http://cpanratings.perl.org/d/XML-Rabbit>
333  
334 =item *
335  
336 CPAN Forum
337  
338 The CPAN Forum is a web forum for discussing Perl modules.
339  
340 L<http://cpanforum.com/dist/XML-Rabbit>
341  
342 =item *
343  
344 CPANTS
345  
346 The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
347  
348 L<http://cpants.perl.org/dist/overview/XML-Rabbit>
349  
350 =item *
351  
352 CPAN Testers
353  
354 The CPAN Testers is a network of smokers who run automated tests on uploaded CPAN distributions.
355  
356 L<http://www.cpantesters.org/distro/X/XML-Rabbit>
357  
358 =item *
359  
360 CPAN Testers Matrix
361  
362 The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
363  
364 L<http://matrix.cpantesters.org/?dist=XML-Rabbit>
365  
366 =item *
367  
368 CPAN Testers Dependencies
369  
370 The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
371  
372 L<http://deps.cpantesters.org/?module=XML::Rabbit>
373  
374 =back
375  
376 =head2 Bugs / Feature Requests
377  
378 Please report any bugs or feature requests by email to C<bug-xml-rabbit at rt.cpan.org>, or through
379 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=XML-Rabbit>. You will be automatically notified of any
380 progress on the request by the system.
381  
382 =head2 Source Code
383  
384 The code is open to the world, and available for you to hack on. Please feel free to browse it and play
385 with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
386 from your repository :)
387  
388 L<http://github.com/robinsmidsrod/XML-Rabbit>
389  
390 git clone git://github.com/robinsmidsrod/XML-Rabbit.git
391  
392 =head1 AUTHOR
393  
394 Robin Smidsrød <robin@smidsrod.no>
395  
396 =head1 COPYRIGHT AND LICENSE
397  
398 This software is copyright (c) 2013 by Robin Smidsrød.
399  
400 This is free software; you can redistribute it and/or modify it under
401 the same terms as the Perl 5 programming language system itself.
402  
403 =cut
« Rabbit/Trait/XPathObjectMap.pm ↑ Summary Rabbit/Root.pm »