Role to Serialize/De-Serialize a Perl 6 class to/from XML
Switch branches/tags
Latest commit bc74240 Apr 29, 2017 @jonathanstowe update META
Failed to load latest commit information.
lib/XML Except that's a sub not a method Sep 26, 2016
t Add META test Mar 8, 2017
.gitignore Basic first commit Feb 19, 2016
.travis.yml Add travis Apr 26, 2016 Add doc as markdown Feb 28, 2016
META6.json update META Apr 29, 2017 Update README Jan 21, 2017


Role to Serialize/De-Serialize a Perl 6 class to/from XML

Build Status


use XML::Class;

class Foo does XML::Class[xml-element => 'foo'] {
    has Int $.version = 0;
    has Str $.zub is xml-element;

my $f = => "pow");

say $; # <?xml version="1.0"?><foo xmlns="" version="0"><zub>pow</zub></foo>


This provides a relatively easy way to instantiate a Perl 6 object from XML and create XML that describes the Perl 6 class in a consistent manner.

It is somewhat inspired by the XmlSerialization class of the .Net framework, but there are other antecedents.

Using a relatively static definition of the relation between a class and XML that represents it means that XML can be consistently parsed and generated in a way that should always remain valid to the original description.

This module aims to map between Perl 6 object attributes and XML by providing some default behaviours and some attribute traits to alter that behaviour to model the XML.

By default scalar attributes whose value type can be expressed as an XML simple type (e.g. strings, real numbers, boolean, datetimes) will be serialised as attribute values or (with an xml-element trait,) as elements with simple content. Positional attributes will always be serialised as a sequence of elements (with an optional container specified by a trait,) likewise associative attributes (though the use of these is discouraged as there is no constraint on the names of the elements which are taken from the keys of the Hash.) Perl 6 classes are expressed as XML complex types with the same serialisation as above. Provision is also made for the serialisation and de-serialisation of other than the builtin types to simple contemt (trivial examples might be Version objects for instance,) and for the handling of data that might be unknown at definition time (such as the xsd:Any in SOAP head and body elements,) by the use of "namespace maps".

There are things that explicitly aren't catered for such as "mixed content" (that is where XML markup may be within text content as in XHTML for example,) but that shouldn't be a problem for data storage or messaging applications for the most part.

The full documentation is available as POD or as Markdown


Assuming you have a working Rakudo Perl 6 installation you should be able to install this with panda :

# From the source directory

panda install .

# Remote installation

panda install XML::Class

I haven't tried this with zef but I have no reason to think it shouldn't work if you would rather use that.

Other install mechanisms may be become available in the future.


Although there are quite a few tests for this I'm sure they don't cover all the possible cases. So if you find something that isn't tested for and doesn't work quite as expected please let me know.

Suggestions/patches are welcomed via github at


This is free software.

Please see the LICENCE file in the distribution

© Jonathan Stowe 2016, 2017