XML::C...::Cache(3pm)
β¨ NAME β¨
XML::Compile::Cache β Cache compiled XML translators π
okay so real talk, this is the XML::C...::Cache(3pm) page from the User Contributed Perl Documentation crowd, and bestie, it's a whole personality. let's go π«Ά
𧬠INHERITANCE π§¬
XML::Compile::Cache
is a XML::Compile::Schema
is a XML::Compile
it's giving "family tree of slay" β your girl inherits from XML::Compile::Schema, which itself is a XML::Compile β¨
π SYNOPSIS π
my $cache = XML::Compile::Cache->new(...);
$cache->declare('READER', $type, @options);
$cache->declare(RW => \@types, @options);
$cache->declare(WRITER => $type, \@options);
$cache->compileAll;
$cache->compileAll('RW');
# get the cached code ref for the reader
my $reader = $cache->reader($type, @opts);
use Data::Dumper;
print Dumper $reader->($xml);
# get the cached code ref for the writer, and use it
my $doc = XML::LibXML::Document->new('1.0', 'UTF-8');
my $xml = $cache->writer($type)->($doc, $perl);
print $xml->toString(1);
# use the base-class uncached, the XML::Compile::Schema
my $do = $cache->compile(READER => $type, @opts);
look at that. compile once, reuse forever. main character energy, no cap π
π¦ DESCRIPTION π¦
Extends "DESCRIPTION" in XML::Compile::Schema.
that's literally the whole description, bestie. short and iconic, she said "go read my parent page, iykyk" πΈ
π METHODS π
Extends "METHODS" in XML::Compile::Schema.
π Constructors
Extends "Constructors" in XML::Compile::Schema.
XML::Compile::Cache->new( [$xml], %options )
here's the full option table, verbatim, because we do not skip the boring parts in this house:
-Option --Defined in --Default
allow_undeclared <false>
any_element 'ATTEMPT'
block_namespace XML::Compile::Schema []
hook XML::Compile::Schema undef
hooks XML::Compile::Schema []
ignore_unused_tags XML::Compile::Schema <false>
key_rewrite XML::Compile::Schema []
opts_readers []
opts_rw []
opts_writers []
parser_options XML::Compile <many>
prefixes <smart>
schema_dirs XML::Compile undef
typemap {}
xsi_type {}
and now, one by one, like the little icons they are π
allow_undeclared=>BOOLEANβ when it's true, you may call the reader or writer with types which were not registered withdeclare(). In that case, the reader or writer may also get options passed for the compiler, as long as they are consistent over each use of the type. β¨any_element=>CODE|'TAKE_ALL'|'SKIP_ALL'|'ATTEMPT'|'SLOPPY'β seeanyElement(). [1.02] the default is toATTEMPTcompiling any handlers automatically. Before version 1.02, the default was toSKIP_ALLelements which would match the occurs and namespace restrictions of the any specification. However, that fails for reperative blocks (for instance, it fails for an choice which may occur unbounded times). πblock_namespace=>NAMESPACE|TYPE|HASH|CODE|ARRAYβ inherited straight fromXML::Compile::Schema, no extra tea spilled on this page πhook=>$hook|ARRAYβ also fromXML::Compile::Schema, same deal πhooks=>ARRAYβ yep,XML::Compile::Schemaagain π¦ignore_unused_tags=>BOOLEAN|REGEXPβ inherited fromXML::Compile::Schemaπkey_rewrite=>HASH|CODE|ARRAYβ inherited fromXML::Compile::SchemaπΈopts_readers=>HASH|ARRAY-of-PAIRSβ inherited fromXML::Compile::Schemaπ«opts_rw=>HASH|ARRAY-of-PAIRSβ Options added to both READERs and WRITERS. Options which are passed withdeclare()and"opts_readers"or"opts_writers"will overrule these. SeeaddCompileOptions(). π₯opts_writers=>HASH|ARRAY-of-PAIRSβ inherited fromXML::Compile::Schemaβparser_options=>HASH|ARRAYβ coming at you fromXML::Compileπ«Άprefixes=>HASH|ARRAY-of-PAIRSβ Define prefix name to name-space mappings. Passed tocompile(prefixes)for each reader and writer, but also used to permitfindName()to accept types which use a prefix.
Specify an ARRAY of (prefix, name-space) pairs, or a HASH which maps name-spaces to prefixes (HASH order is reversed from ARRAY order!) When you wish to collect the results, like usage counts, of the translation processing, you will need to specify a HASH.
prefixes => [ mine => $myns, your => $yourns ]
prefixes => { $myns => 'mine', $yourns => 'your' }
# the previous is short for:
prefixes => { $myns => [ uri => $myns, prefix => 'mine', used => 0 ]
, $yourns => [ uri => $yourns, prefix => 'your', ...] }
lowkey obsessed with how the HASH order is flipped, watch out for that bestie π
schema_dirs=>$directory|ARRAY-OF-directoriesβ fromXML::Compileπ§typemap=>HASH|ARRAYβ inherited fromXML::Compile::Schemaπxsi_type=>HASH|ARRAYβ inherited fromXML::Compile::Schemaπ
π«Ά Accessors
Extends "Accessors" in XML::Compile::Schema.
$obj->addHook($hook|LIST|undef)β Inherited, see "Accessors" inXML::Compile::Schema$obj->addHooks( $hook, [$hook, ...] )β Inherited, see "Accessors" inXML::Compile::Schema$obj->addKeyRewrite($predef|CODE|HASH, ...)β Inherited, see "Accessors" inXML::Compile::Schema$obj->addSchemaDirs(@directories|$filename)XML::Compile::Cache->addSchemaDirs(@directories|$filename)β Inherited, see "Accessors" inXML::Compile$obj->addSchemas($xml, %options)β Inherited, see "Accessors" inXML::Compile::Schema$obj->addTypemap(PAIR)β Inherited, see "Accessors" inXML::Compile::Schema$obj->addTypemaps(PAIRS)β Inherited, see "Accessors" inXML::Compile::Schema$obj->addXsiType( [HASH|ARRAY|LIST] )β [1.01] add globalxsi_typedeclarations. Returns thexsiTypeset. The ARRAY or LIST contains pairs, just like the HASH.
The value component can be 'AUTO' to automatically detect the "xsi:type" extensions. This does only work for complex types. π
$obj->allowUndeclared( [BOOLEAN] )β Whether it is permitted to create readers and writers which are not declared cleanly. π$obj->anyElement('ATTEMPT'|'SLOPPY'|'SKIP_ALL'|'TAKE_ALL'|CODE)β [as method since 0.99] How to process ANY elements, see alsonew(any_element).
Reader: "ATTEMPT" will convert all any elements, applying the reader for each element found. When an element is not found in a schema, it will be included as XML::LibXML::Element node.
[0.93] Reader: With "SLOPPY", first automatic typed conversion is attempted. But is the type is not known, XML::LibXML::Simple::XMLin() is called to the resque. πβ¨
$obj->blockNamespace($ns|$type|HASH|CODE|ARRAY)β Inherited, see "Accessors" inXML::Compile::Schema$obj->hooks( [<'READER'|'WRITER'>] )β Inherited, see "Accessors" inXML::Compile::Schema$obj->typemap( [HASH|ARRAY|PAIRS] )β [0.98] Add global knowledge on typemaps. Returns the typemap. π$obj->useSchema( $schema, [$schema, ...] )β Inherited, see "Accessors" inXML::Compile::Schema
π§ Prefix management
The cache layer on top of XML::Compile::Schema adds smart use of prefixes. Of course, smartness comes with a small performance cost, but the code gets much cleaner. β¨ (worth it, honestly)
$obj->addNicePrefix(BASE, NAMESPACE)β [1.03] Register NAMESPACE β if not yet defined β with prefix name BASE. When that prefix name is already in use for some other namespace, BASE followed by a number are attempted (starting with01). The prefix is returned.
When the BASE already ends on a number, that number will get counted.
example:
my $prefix = $schema->addNicePrefix('call', $myns);
# $prefix now can be call, call01, call02 etc
$obj->addPrefixes( [PAIRS|ARRAY|HASH] )β The X::C logic does auto-detect prefix/namespaces combinations from the XML, but does not search extensively for namespace declarations. Also, sometimes the same namespace is used with different prefixes. Sometimes, the same prefix is used for different namesapces. To complete the list, or control the actual prefix being used, you explicitly declare combinations.
The best way to add prefixes is via new(prefixes), which will give your names preference over the names found in the schema's which get loaded. For instance, use "::WSDL->new(prefixes => [ $prefix => $ns ]"
[0.995] Returns the HASH with prefix to name-space translations. You should not modify the returned HASH: new PAIRS of prefix to namespace relations can be passed as arguments.
[0.14] If a name-space appears for the second time, then the new prefix will be recognized by findName(), but not used in the output. When the prefix already exists for a different namespace, then an error will be casted.
[0.90] You may also provide an ARRAY of pairs or a HASH. π
$obj->learnPrefixes($node)β [0.993] Take all the prefixes defined in the$node, andXML::LibXML::Element. This is not recursive: only on those defined at the top$node. π¦$obj->prefix($prefix)β Lookup a prefix definition. This returns a HASH with namespace info.$obj->prefixFor($uri)β Lookup the preferred prefix for the$uri.$obj->prefixed( $type|<$ns,$local> )β Translate the fully qualified$typeinto a prefixed version. Will produceundefif the namespace is unknown.
[0.993] When your $type is not in packed form, you can specify a namespace and $local type name as separate arguments.
example:
print $schema->prefixed($type) || $type;
print $schema->prefixed($ns, $local);
$obj->prefixes( [$params] )β Return prefixes table. The$paramsare deprecated since [0.995], seeaddPrefixes(). π« (deprecated, bestie, don't do it)
π» Compilers
The name of this module refers to its power to administer compiled XML encoders (writers) and decoders (readers). This means that your program only need to pass on a ::Cache object (for instance a XML::Compile::WSDL11, not a CODE reference for each compiled translator. π₯
Extends "Compilers" in XML::Compile::Schema.
$obj->addCompileOptions( ['READERS'|'WRITERS'|'RW'], %options )β [0.99] You may provide global compile options withnew(opts_rw),"opts_readers"and"opts_writers", but also later using this method. β¨$obj->compile( <'READER'|'WRITER'>, $type, %options )β Inherited, see "Compilers" inXML::Compile::Schema$obj->compileAll( ['READERS'|'WRITERS'|'RW', [$ns]] )β Compile all the declared readers and writers with the default'RW'. You may also select to pre-compile only the READERS or only the WRITERS. The selection can be limited further by specifying a$ns.
By default, the processors are only compiled when used. This method is especially useful in a daemon process, where preparations can take as much time as they want to... and running should be as fast as possible. π
$obj->compileType( <'READER'|'WRITER'>, $type, %options )β Inherited, see "Compilers" inXML::Compile::Schema$obj->dataToXML( $node|REF-XML|XML-STRING|$filename|$fh|$known )XML::Compile::Cache->dataToXML($node|REF-XML|XML-STRING|$filename|$fh|$known )β Inherited, see "Compilers" inXML::Compile$obj->initParser(%options)XML::Compile::Cache->initParser(%options)β Inherited, see "Compilers" inXML::Compile$obj->reader($type|$name, %options)β hands you back the reader CODE for that$typeor$name(peepfindName()for how names get resolved). π
The %options are only allowed if new(allow_undeclared) is true β and they have to be the same as the options you passed in the previous call to this method. no switching up on it mid-way, bestie.
The reader gets compiled the very first time it's actually used, and then that same CODE reference comes back every single time you ask for the same $type. Want them all built up front instead of on-demand? Call compileAll() to have all readers compiled by force. β¨
-Option --Default
is_type <false>
is_type => BOOLEANβ[1.03]usecompileType()with the given element, to replacecompile(). You probably want an additional"element"parameter. π
example:
my $schema = XML::Compile::Cache->new(\@xsd,
prefixes => [ gml => $GML_NAMESPACE ] );
my $data = $schema->reader('gml:members')->($xml);
my $getmem = $schema->reader('gml:members');
my $data = $getmem->($xml);
$obj->template( <'XML'|'PERL'|'TREE'>, $element, %options )β inherited, see "Compilers" in XML::Compile::Schema. π¦$obj->writer($type|$name)β returns the writer CODE for the$typeor$name(again,findName()is your girl for name stuff). OPTIONS are only permitted ifnew(allow_undeclared)is true, and they're the same as the previous call to this method. same rules, different direction. π
The writer gets compiled the first time it's used, and that same CODE reference will be returned each next request for the same type. periodt.
-Option --Default
is_type <false>
is_type => BOOLEANβ[1.03]usecompileType()with the given element, to replacecompile(). You probably want an additional"element"parameter. πΈ
example:
my $xml = $cache->writer('gml:members')->($doc, $data);
my $doc = XML::LibXML::Document->new('1.0', 'UTF-8');
my $wr = $cache->writer('gml:members');
my $xml = $wr->($doc, $data);
$doc->setDocumentElement($xml);
print $doc->toString(1);
π οΈ Administration π οΈ
Extends "Administration" in XML::Compile::Schema. π«Ά
$obj->declare( <'READER'|'WRITER'|'RW'>, <$type|ARRAY>, %options )β registers that the indicated$type(or anARRAYfull of them) is allowed to be used, and needs to be translated with the%options(either handed over as anARRAYor asPAIRS). Then you say whether it can be used as aREADER, aWRITER, or both at once (RW). π
Heads up though: if theREADERand theWRITERneed different options, you have to declare them separately β in that case you literally cannot useRW. π
The $type should be something findName() can understand, so it may be prefixed. π
example:
$cache->declare(READER => 'pref:count', sloppy_integers => 1)
->declare(RW => '{myns}mylocal');
$cache->declare(WRITER => [ 'xsd:int', '{http://}aap' ]);
$obj->doesExtend($exttype, $basetype)β inherited, see "Administration" in XML::Compile::Schema. β¨$obj->elements()β inherited, see "Administration" in XML::Compile::Schema. π$obj->findName($name)β translates the$namespec into a schema-defined full type. The$namecan be a full type (like'{namespace}localname', usually built withXML::Compile::Util::pack_type()) or a prefixed type (like'myns:localname', where"myns"got defined throughnew(prefixes)orprefixes()). π
When the form is 'myns:' (so, no local name at all), the namespace uri is what comes back. π¦
example: of findName()
$schema->addPrefixes(pre => 'http://namespace');
my $type = $schema->findName('pre:name');
print $type; # {http://namespace}name
my $ns = $schema->findName('pre:');
print $ns; # http://namespace
my $type = $schema->findName('{somens}name');
print $type; # {somens}name [a no-op]
$obj->findSchemaFile($filename)XML::Compile::Cache->findSchemaFile($filename)β inherited, see "Administration" in XML::Compile. π$obj->importDefinitions($xmldata, %options)β inherited, see "Administration" in XML::Compile::Schema. πΈ$obj->knownNamespace($ns|PAIRS)XML::Compile::Cache->knownNamespace($ns|PAIRS)β inherited, see "Administration" in XML::Compile. β$obj->namespaces()β inherited, see "Administration" in XML::Compile::Schema. π$obj->printIndex( [$fh], %options )
-Option --Default
show_declared <true>
show_declared => BOOLEANβ adds an indicator to every line, telling you whether readers and writers are declared for the type. Declared readers and writers show up with the flags"r"and"w"respectively. Compiled readers and writers carry a"R"and/or"W". π$obj->types()β inherited, see "Administration" in XML::Compile::Schema. π§$obj->walkTree($node, CODE)β inherited, see "Administration" in XML::Compile. π₯
π DETAILS π
Extends "DETAILS" in XML::Compile::Schema. π
π DESCRIPTIONS π
XML::Compile::Cache is the smart brother of XML::Compile::Schema β it keeps track of your compiled readers and writers, and it also helps you administer the parameters used to handle compilation. On top of all that, it lets you use easy prefixes instead of full namespaces. iconic behaviour, honestly. π
With XML::Compile::Schema::compile() (defined over in the SUPER class of this module) you can construct translators from XML to Perl and back. These translators are code references, which are "expensive" to create, but "cheap" in use β so call them as often as you want. This module is the one that helps you administer them. π
When the schemas grow larger, it gets harder to see which code references have already been created and which have not. And those code references need compile options that you absolutely do not want scattered all over your whole program. Finally, in a daemon application, you do not want to create the translators when they get used (which can be in every client again) β you want them made once, during the initiation of the daemon. no cap. β¨
One of the most important contributions to the compile management is the addition of smart prefix handling. This means you can use prefixed names in stead of full types, which are often created with XML::Compile::Util::pack_type(). π
π SEE ALSO π
This module is part of XML-Compile-Cache distribution version 1.06, built on March 04, 2018. Website: http://perl.overmeer.net/xml-compile/ π
π LICENSE π
Copyrights 2008-2018 by [Mark Overmeer]. For other contributors see ChangeLog. π«Ά
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://dev.perl.org/licenses/ π
and that's a wrap, bestie π this page ships as perl v5.26.2, dated 2018-03-20, and it's XML::C...::Cache(3pm) β go forth and cache responsibly π
β¨
π¦ where this page comes from
- ubuntu jammy: libxml-compile-cache-perl 1.06-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libxml-compile-cache-perl 1.06-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms