/usr/share/perl5/XML/LibXML/LazyBuilder.pm is in libxml-libxml-lazybuilder-perl 0.03-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | package XML::LibXML::LazyBuilder;
use 5.008000;
use strict;
use warnings;
require Exporter;
our @ISA = qw(Exporter);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
# This allows declaration use XML::LibXML::LazyBuilder ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw(
E DOM
) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw(
);
our $VERSION = '0.03';
# Preloaded methods go here.
use XML::LibXML;
sub E ($;$@) {
my ($name, $attr, @contents) = @_;
sub {
my ($dom) = @_;
my $elem = $dom->createElement ($name);
if (ref ($attr) eq 'HASH') {
while (my ($n, $v) = each %$attr) {
$elem->setAttribute ($n, $v);
}
}
for my $child (@contents) {
if (ref $child) {
$elem->appendChild ($child->($dom));
} else {
$elem->appendTextNode ($child);
}
}
$elem;
}
}
sub DOM ($;$$) {
my ($elem, $ver, $enc) = @_;
my $dom = XML::LibXML::Document->new ($ver || "1.0", $enc || "utf-8");
$dom->setDocumentElement ($elem->($dom));
$dom;
}
1;
__END__
=head1 NAME
XML::LibXML::LazyBuilder - easy and lazy way to create XML document for XML::LibXML
=head1 SYNOPSIS
use XML::LibXML::LazyBuilder;
{
package XML::LibXML::LazyBuilder;
$d = DOM (E A => {at1 => "val1", at2 => "val2"},
((E B => {}, ((E "C"),
(E D => {}, "Content of D"))),
(E E => {}, ((E F => {}, "Content of F"),
(E "G")))));
}
=head1 DESCRIPTION
You can describe XML documents like simple function call instead of
using createElement, appendChild, etc...
=head2 FUNCTIONS
=over 4
=item E
E "tagname", \%attr, @children
Creats CODEREF that generates C<XML::LibXML::Element> which tag name
is given by first argument. Rest arguments are list of text content
or child element created by C<E> (so you can nest C<E>).
Since the output of this function is CODEREF, the creation of actual
C<XML::LibXML::Element> object will be delayed until C<DOM> function
is called.
=item DOM
DOM \&docroot, $var, $enc
Generates C<XML::LibXML::Document> object actually. First argument is
a CODEREF created by C<E> function. C<$var> is version number of XML
docuemnt, C<"1.0"> by default. C<$enc> is encoding, C<"utf-8"> by
default.
=back
=head2 EXPORT
None by default.
=over 4
=item :all
Exports C<E> and C<DOM>.
=back
=head2 EXAMPLES
I recommend to use C<package> statement in a small scope so that you
can use short function name and avoid to pollute global name space.
my $d;
{
package XML::LibXML::LazyBuilder;
$d = DOM (E A => {at1 => "val1", at2 => "val2"},
((E B => {}, ((E "C"),
(E D => {}, "Content of D"))),
(E E => {}, ((E F => {}, "Content of F"),
(E "G")))));
}
Then, C<< $d->toString >> will generate XML like this:
<?xml version="1.0" encoding="utf-8"?>
<A at1="val1" at2="val2"><B><C/><D>Content of D</D></B><E><F>Content of F</F><G/></E></A>
=head1 SEE ALSO
L<XML::LibXML>
=head1 AUTHOR
Toru Hisai, E<lt>toru@torus.jpE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2008 by Toru Hisai
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.10.0 or,
at your option, any later version of Perl 5 you may have available.
=cut
|