MooX::Struct allows you to create cheap struct-like classes for your data using Moo.

While similar in spirit to MooseX::Struct and Class::Struct, MooX::Struct has a somewhat different usage pattern. Rather than providing you with a struct keyword which can be used to define structs, you define all the structs as part of the use statement. This means they happen at compile time.

A struct is just an "anonymous" Moo class. MooX::Struct creates this class for you, and installs a lexical alias for it in your namespace. Thus your module can create a "Point3D" struct, and some other module can too, and they won't interfere with each other. All struct classes inherit from MooX::Struct.

Arguments for MooX::Struct are key-value pairs, where keys are the struct names, and values are arrayrefs.

The elements in the array are the attributes for the struct (which will be created as read-only attributes), however certain array elements are treated specially.

As per the example in the "SYNOPSIS", -extends introduces a list of parent classes for the struct. If not specified, then classes inherit from MooX::Struct itself.

Structs can inherit from other structs, or from normal classes. If inheriting from another struct, then you must define both in the same use statement. Inheriting from a non-struct class is discouraged.

But if you're defining methods for your structs, then you've possibly missed the point of them.

If an attribute name is followed by an arrayref, these are used to set the options for the attribute. For example:

use MooX::Struct
Person => [ name => [ is => 'ro', required => 1 ] ];

Using the init_arg option would probably break stuff. Don't do that.

Attribute names may be "decorated" with prefix and postfix "sigils". The prefix sigils of @ and % specify that the attribute isa arrayref or hashref respectively. (Blessed arrayrefs and hashrefs are accepted; as are objects which overload @{} and %{}.) The prefix sigil $ specifies that the attribute value must not be an unblessed arrayref or hashref. The prefix sigil + indicates the attribute is a number, and provides a default value of 0, unless the attribute is required. The postfix sigil ! specifies that the attribute is required.

There are two supported methods of instatiating structs. You can use a traditional class-like constructor with named parameters:

my $point = Point->new( x => 1, y => 2 );

Or you can use the abbreviated syntax with positional parameters:

my $point = Point[ 1, 2 ];

If you know about Moo and peek around in the source code for this module, then I'm sure you can figure out additional ways to instantiate them, but the above are the only supported two.

When inheritance or roles have been used, it might not always be clear what order the positional parameters come in (though see the documentation for the FIELDS below), so the traditional class-like style may be preferred.

Structs are objects and thus have methods. You can define your own methods as described above. MooX::Struct's built-in methods will always obey the convention of being in ALL CAPS (except in the case of _data_printer). By using lower-case letters to name your own methods, you can avoid naming collisions.

The following methods are currently defined. Additionally all the standard Perl (isa, can, etc) and Moo (new, does, etc) methods are available.

This feature has been included mostly because it's easy to implement on top of the existing code for processing use MooX::Struct. Some subsets of this functionality are sane, such as the ability to add traits to an object. Others (like the ability to add a new uninitialized, read-only attribute to an existing object) are less sensible.