NAME

MooX::XSConstructor - glue between Moo and Class::XSConstructor

SYNOPSIS

package Foo;
use Moo;
use MooX::XSConstructor;

# do normal Moo stuff here

DESCRIPTION

MooX::XSConstructor will look at your class attributes, and see if it could be built using Class::XSConstructor. If your class seems too complicated, it is a no-op. If your class is simple enough, you will hopefully get a faster constructor.

Which features are too complicated for MooX::XSConstructor to handle?

  • Any MooX extensions which cause your constructor generator to be changed from the default of Method::Generate::Constructor.

    Luckily hardly any extensions exist which do that. MooX::StrictConstructor is one fairly popular extension that does, and we support that as a special case, but only if you use it with the -late option.

  • Any MooX extensions which cause your accessor generator to be changed in such a way that it generates the "core set" to be different from the standard one.

    Basically what this means is that by standard, Moo attributes can be set using $object->{"attribute_name"} = VALUE, but if you've got a MooX extension that changes that, MooX::StrictConstructor will bail out.

    This includes: MooX::InsideOut and MooX::AttributeFilter.

  • If any of your attributes, including inherited ones, are defined using options other than: is, reader, writer, accessor, predicate, clearer, handles, handles_via, init_arg, required, isa, coerce, builder, default, lazy, trigger, weak_ref, and documentation.

    Certain extensions like MooX::Should or MooX::Aliases do define other options for attributes, but also clean up after themselves, deleting the additional options from the attribute spec before MooX::StrictConstructor can see them. So they are okay!

API

Normal usage is just to use MooX::XSConstructor in your Moo class. However, an API is provided.

MooX::XSConstructor->setup_for( $class )

Run setup for the given class, replacing its constructor with a faster XS constructor. Do not call the setup_for method until you are sure the class has finished compiling, with all attributes already having been added (using has or role composition).

Returns the class's name if it successfully replaced the constructor. Returns undef otherwise.

MooX::XSConstructor->is_suitable_class( $class )

Checks that the class is a suitable Moo class for replacing the constructor of. setup_for calls this, so you don't actually need to do this check yourself too.

MooX::XSConstructor::is_xs( $coderef )

Returns true if the coderef is implemented with XS. Can be used to double-check setup_for was successful if you don't trust it for some reason.

BUGS

Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=MooX-XSConstructor.

SEE ALSO

Moo, Class::XSConstructor, MooseX::XSConstructor, Marlin.

You may also be interested in Class::XSAccessor. Moo already includes all the glue to interface with that, so a MooX module like this one isn't necessary.

AUTHOR

Toby Inkster <tobyink@cpan.org>.

COPYRIGHT AND LICENCE

This software is copyright (c) 2018, 2026 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.