MooseX...cessor(3pm)
π MooseX::XSAccessor β the "make your Moose accessors go zoom" manual π
okay so bestie, this is the manual page for MooseX::XSAccessor, part of the User Contributed Perl Documentation collection, sitting pretty in section 3pm of your friendly neighbourhood man pages. Buckle up, we're making your accessors fast. β¨
πΈ NAME πΈ
MooseX::XSAccessor β use Class::XSAccessor to speed up Moose accessors
π¦ SYNOPSIS π¦
package MyClass;
use Moose;
use MooseX::XSAccessor;
has foo => (...);
β¨ DESCRIPTION β¨
So here's the tea: this module takes your Moose-generated accessors, readers, writers and predicate methods and makes them zoomy using Class::XSAccessor. You get the speed-up for zero extra effort β it just happens. Automatically. To literally every attribute in the class. No cap. π
That said, not everything gets the glow-up. Here's what blocks a reader from being accelerated:
- A lazy builder, or a lazy default.
- Auto-deref. (ngl, does anyone actually use this one?? π)
And here's what blocks a writer from being accelerated:
- Type constraints (except
"Any"β since"Any"is effectively a no-op, it's fine). - Triggers.
- Weak references.
An "rw" accessor is literally just a reader and a writer glued together, so both lists up there apply to it. Both of them, babe.
Predicates though? Predicates can always be accelerated, as long as you're running Class::XSAccessor 1.17 or above. Slay. π
Clearers can not be accelerated (as of current versions of Class::XSAccessor). We don't make the rules.
π«Ά Functions π«Ά
This module also gives you one function, and it is not exported, so you gotta call it by its full name. No shortcuts here.
MooseX::XSAccessor::is_xs($sub)β returns a boolean telling you whether a sub is an XSUB.
$sub can be a coderef, a Class::MOP::Method object, or a qualified sub name as a string (like "MyClass::foo").
And listen β this thing doesn't just work with accessors. It should be able to spot the difference between Perl subs and XS subs in general. It may not be 100% reliable though, so likeβ¦ don't bet your whole life on it. π
π§ Chained accessors and writers π§
MooseX::XSAccessor can clock the chained accessors and writers that MooseX::Attribute::Chained makes, and it can accelerate those too. Iconic.
package Local::Class;
use Moose;
use MooseX::XSAccessor;
use MooseX::Attribute::Chained;
has foo => (traits => ["Chained"], is => "rw");
has bar => (traits => ["Chained"], is => "ro", writer => "_set_bar");
has baz => ( is => "rw"); # not chained
my $obj = "Local::Class"->new;
$obj->foo(1)->_set_bar(2);
print $obj->dump;
π Lvalue accessors π
MooseX::XSAccessor will spot lvalue accessors made with MooseX::LvalueAttribute, and by default it skips accelerating them. Respectfully. β
But β plot twist β if you set $MooseX::XSAccessor::LVALUE to true (preferably with the local Perl keyword), you can force it to accelerate those too. Fair warning though: this introduces a visible change in behaviour. MooseX::LvalueAttribute accessors normally let you set the value two ways:
$obj->foo = 42; # as an lvalue
$obj->foo(42); # as a method call
But once they're accelerated, they may only be set as an lvalue. So for that reason, setting $MooseX::XSAccessor::LVALUE to true is considered an experimental feature. You've been warned, bestie.
π₯ HINTS π₯
- Make attributes read-only when you can. This means type constraints and coercions only apply to the constructor, not the accessors, which frees the accessors up to be accelerated. π
- If you genuinely need a read-write attribute, consider making the main accessor read-only and having a separate writer method instead. (Like
MooseX::SemiAffordanceAccessordoes.) - Make defaults eager instead of lazy whenever possible β that lets your readers get accelerated too. π
- If you only need to accelerate one specific attribute, apply the attribute trait directly, like so:
package MyClass;
use Moose;
has foo => (
traits => ["MooseX::XSAccessor::Trait::Attribute"],
...,
);
- And if you don't wanna add a hard dependency on MooseX::XSAccessor but do wanna use it when it's around, this lil snippet uses it optionally:
package MyClass;
use Moose;
BEGIN { eval "use MooseX::XSAccessor" };
has foo => (...);
π CAVEATS π
- Calling a writer method without a parameter in Moose does not raise an exception:
$person->set_name(); # sets name attribute to "undef"
However β and this is the moment β this is a fatal error in Class::XSAccessor. Different vibe entirely. π
- MooseX::XSAccessor does not play nice with attribute traits that alter accessor behaviour, or that define additional accessors for attributes.
MooseX::SetOnceis an example of this.MooseX::Attribute::Chainedis handled as a special case, because it's the favourite child. π - MooseX::XSAccessor only works on blessed hash storage β so not
MooseX::ArrayReforMooseX::InsideOut, for example. Good news: MooseX::XSAccessor is usually able to detect those situations and silently switch itself off. It knows when to leave the party. π¦
β BUGS β
Please report any bugs to <https://github.com/tobyink/p5-moosex-xsaccessor/issues>. Bestie behaviour is encouraged. π
π SEE ALSO π
MooseX::XSAccessor::Trait::Attribute.Moose,Moo,Class::XSAccessor.
π AUTHOR π
Toby Inkster <tobyink@cpan.org>.
π COPYRIGHT AND LICENCE π
This software is copyright (c) 2013, 2017, 2022 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.
no thoughts, just MooseX::XSAccessor fr fr π«Ά
and that's the whole page, bestie β this was MooseX...cessor(3pm) from Perl v5.40.1, dated 2026-02-15. go forth and accelerate, mwah ππΈβ¨
π¦ where this page comes from
- ubuntu resolute: libmoosex-xsaccessor-perl 0.010-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmoosex-xsaccessor-perl 0.010-2 Β· 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