XML::Pa...Objects(3)
π NAME π
okay so this page is XML::Pa...Objects(3) β that's the header title β and it comes from the manual name User Contributed Perl Documentation. The full module is XML::PatAct::ToObjects, and it's an action module for creating Perl objects. bestie, it's literally giving XML-to-Perl makeover energy β¨.
β¨ SYNOPSIS β¨
here's the vibe, no cap:
use XML::PatAct::ToObjects;
my $patterns = [ PATTERN => [ OPTIONS ],
PATTERN => "PERL-CODE",
... ];
my $matcher = XML::PatAct::ToObjects->new( Patterns => $patterns,
Matcher => $matcher,
CopyId => 1,
CopyAttributes => 1 );
π¦ DESCRIPTION π¦
XML::PatAct::ToObjects is a PerlSAX handler for applying pattern-action lists to XML parses or trees. It creates Perl objects of the types and contents of the action items you define. iconic behavior, honestly.
New XML::PatAct::ToObject instances are creating by calling new(). Parameters can be passed as a list of key, value pairs or a hash. new() requires the Patterns and Matcher parameters, and the rest are optional:
Patternsβ the pattern-action list to apply. yes, the whole menu π .Matcherβ an instance of the pattern or query matching module.CopyIdβ causes theIDattribute, if any, in a source XML element to be copied to anIDattribute in newly created objects. Note that IDs may be lost when no pattern matches that element, or when an object is not created (-make) for that element. rude but true π.CopyAttributesβ causes all attributes of the element to be copied to the newly created objects.
Each action can either be a list of options defined below or a string containing a fragment of Perl code. If the action is a string of Perl code, some simple substitutions are made as described further below. iykyk.
Options that can be used in an action item containing an option-list:
-holderβ ignore this element, but continue processing its children (compare to-ignore).-pcdatamay be used with this option.-ignoreβ ignore (discard) this element and its children (compare to-holder).-pcdataβ character data in this element should be copied to the"Contents"field.-make PACKAGEβ create an object blessed into PACKAGE, and continue processing this element and its children. PACKAGE may be the type"HASH"to simply create an anonymous hash.-args ARGUMENTSβ useARGUMENTSin creating the object specified by-make. This is commonly used to copy element attributes into fields in the newly created object. For example,-make => 'HASH', -args => 'URL => %{href}'would copy the"href"attribute in an element to the"URL"field of the newly created hash. cute little attribute shuffle π.-field FIELDβ store this element, object, or children of this element in the parent object's field named byFIELD.-push-field FIELDβ similar to-field, except thatFIELDis an array and the contents are pushed onto that array.-value VALUEβ useVALUEas a literal value to store inFIELD, otherwise ignoring this element and its children. Only valid with-fieldor-push-field."%{ATTRIBUTE}"notation can be used to substitute the value of an attribute into the literal value.-as-stringβ convert the contents of this element to a string (as in"XML::Grove::AsString") and store inFIELD. Only valid with-fieldor-push-field.-groveβ copy this element toFIELDwithout further processing. The element can then be processed later as the Perl objects are manipulated. Only valid with-fieldor-push-field. If ToObjects is used with PerlSAX, this will useXML::Grove::Builderto build the grove element.-grove-contentsβ used with-make,-grove-contentscreates an object but then takes all of the content of that element and stores it inContents.
If an action item is a string, that string is treated as a fragment of Perl code. The following simple substitutions are performed on the fragment to provide easy access to the information being converted:
@ELEM@β the object that caused this action to be called. If ToObjects is used with PerlSAX this will be a hash with the element name and attributes, withXML::Grovethis will be the element object, withData::Groveit will be the matching object, and withXML::DOMit will be anXML::DOM::Element.
π EXAMPLE π
The example pattern-action list below will convert the following XML representing a Database schema:
<schema>
<table>
<name>MyTable</name>
<summary>A short summary</summary>
<description>A long description that may
contain a subset of HTML</description>
<column>
<name>MyColumn1</name>
<summary>A short summary</summary>
<description>A long description</description>
<unique/>
<non-null/>
<default>42</default>
</column>
</table>
</schema>
into Perl objects looking like:
[
{ Name => "MyTable",
Summary => "A short summary",
Description => $grove_object,
Columns => [
{ Name => "MyColumn1",
Summary => "A short summary",
Description => $grove_object,
Unique => 1,
NonNull => 1,
Default => 42
}
]
}
]
Here is a Perl script and pattern-action list that will perform the conversion using the simple name matching pattern module XML::PatAct::MatchName. The script accepts a Schema XML file as an argument ($ARGV[0]) to the script. This script creates a grove as one of its objects, so it requires the XML::Grove module.
use XML::Parser::PerlSAX;
use XML::PatAct::MatchName;
use XML::PatAct::ToObjects;
my $patterns = [
'schema' => [ qw{ -holder } ],
'table' => [ qw{ -make Schema::Table } ],
'name' => [ qw{ -field Name -as-string } ],
'summary' => [ qw{ -field Summary -as-string } ],
'description' => [ qw{ -field Description -grove } ],
'column' => [ qw{ -make Schema::Column -push-field Columns } ],
'unique' => [ qw{ -field Unique -value 1 } ],
'non-null' => [ qw{ -field NonNull -value 1 } ],
'default' => [ qw{ -field Default -as-string } ],
];
my $matcher = XML::PatAct::MatchName->new( Patterns => $patterns );
my $handler = XML::PatAct::ToObjects->new( Patterns => $patterns,
Matcher => $matcher);
my $parser = XML::Parser::PerlSAX->new( Handler => $handler );
my $schema = $parser->parse(Source => { SystemId => $ARGV[0] } );
π TODO π
- It'd be nice if patterns could be applied even in
-as-stringand-grove. - Implement Perl code actions.
-as-xmlto write XML into the field.
π AUTHOR π
Ken MacLeod, ken@bitsko.slc.ut.us
π SEE ALSO π
The sections Using PatAct Modules and Creating PatAct Modules in libxml-perl.
and that's the whole page, bestie π«Ά. the footer says perl v5.42.0, dated 2024-03-04, for XML::Pa...Objects(3). slay, go build some Perl objects π
β¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-libxml-perl 0.08-160000.2.2 Β· Artistic-1.0 OR GPL-2.0-or-later
- opensuse-leap 16.0: perl-libxml-perl 0.08-160000.2.2 Β· Artistic-1.0 OR GPL-2.0-or-later
- opensuse-leap 16.0: perl-libxml-perl 0.08-160000.2.2 Β· Artistic-1.0 OR GPL-2.0-or-later
- opensuse-leap 16.0: perl-libxml-perl 0.08-160000.2.2 Β· Artistic-1.0 OR GPL-2.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms