MooseX::NonMoose(3pm)
π MooseX::NonMoose π
okay bestie, so this is MooseX::NonMoose(3pm), straight out of the User Contributed Perl Documentation, and it's about to make your whole subclassing situation so much less tragic β¨
version 0.27 β yes, she's been around, and she still slays π
β¨ SYNOPSIS β¨
like, just look at how easy this is:
package Term::VT102::NBased;
use Moose;
use MooseX::NonMoose;
extends 'Term::VT102';
has [qw/x_base y_base/] => (
is => 'ro',
isa => 'Int',
default => 1,
);
around x => sub {
my $orig = shift;
my $self = shift;
$self->$orig(@_) + $self->x_base - 1;
};
# ... (wrap other methods)
no Moose;
# no need to fiddle with inline_constructor here
__PACKAGE__->meta->make_immutable;
my $vt = Term::VT102::NBased->new(x_base => 0, y_base => 0);
no cap, that's the whole vibe πΈ
π DESCRIPTION π
so MooseX::NonMoose lets you easily subclass non-Moose classes with Moose, and it handles all the annoying little details that come with that, bestie. the ones that make you go "why is this happening π". things like setting up proper inheritance from Moose::Object, and installing a constructor that makes sure stuff like BUILD methods actually gets called β and inlining it, at make_immutable time, because we're thorough like that. π
she tries to be as non-intrusive as possible β when you use this module, inheriting from non-Moose classes and inheriting from Moose classes should work identically, aside from the few caveats mentioned below (we'll get to those, don't worry π). one of the goals here is that you can include it in a Moose::Exporter-based package used across an entire application, without interfering with classes that only inherit from Moose modules, or even classes that don't inherit from anything at all. iconic behavior honestly π
there are several ways to use this module, so pick your fighter:
- the most straightforward way is to just
use MooseX::NonMoose;in your class. that sets up everything necessary for extending non-Moose modules. periodt. MooseX::NonMoose::Meta::Role::ClassandMooseX::NonMoose::Meta::Role::Constructorcan also be applied to your metaclasses manually β either by passing a-traitsoption to youruse Moose;line, or by applying them usingMoose::Util::MetaRolein aMoose::Exporter-based package.
and like, here's the breakdown on those two, iykyk:
MooseX::NonMoose::Meta::Role::Classis the part that provides the main functionality of this module. if you don't care about inlining, this is all you need to worry about π«Ά- applying
MooseX::NonMoose::Meta::Role::Constructoras well will provide an inlined constructor when you immutabilize your class β¨
now, MooseX::NonMoose lets you manipulate the argument list that gets passed to the superclass constructor by defining a FOREIGNBUILDARGS method. she gets called with the same argument list as the BUILDARGS method, but she should return a list of arguments to pass to the superclass constructor. this is the move that lets MooseX::NonMoose support superclasses whose constructors would get confused by the extra arguments that Moose requires (for attributes, etc.). π
also!! not all non-Moose classes use new as the name of their constructor. rude of them, but okay. this module lets you extend those classes by explicitly stating which method is the constructor, during the call to extends. the syntax looks like this:
extends 'Foo' => { -constructor_name => 'create' };
similar to how you can already pass -version in the extends call in a similar way. slay π
π¦ BUGS/CAVEATS π¦
okay this is the part where we get real, because these are actual caveats and you need to know them:
- the reference that the non-Moose class uses as its instance type must match the instance type that Moose is using. Moose's default instance type is a hashref, but other modules exist to make Moose use other instance types.
MooseX::InsideOutis the most general solution β it should work with any class. for globref-based classes in particular,MooseX::GlobRefwill also allow Moose to work. for more information, see the032-moosex-insideoutand033-moosex-globreftests bundled with this dist π§ - modifying your class'
@ISAafter an initialextendscall will potentially cause problems if any of those new entries in the@ISAoverride the constructor.MooseX::NonMoosewraps the nearestnew()method at the timeextendsis called and will not see any othernew()methods in the@ISAhierarchy. π - completely overriding the constructor in a class using
MooseX::NonMoose(i.e. usingsub new { ... }) currently doesn't work, although using method modifiers on the constructor should work identically to normal Moose classes. π
please report any bugs to GitHub Issues at <https://github.com/uperl/moosex-nonmoose/issues>. bestie, they want to hear from you π
π SEE ALSO π
- "How do I make non-Moose constructors work with Moose?" in
Moose::Manual::FAQ MooseX::Alienβ serves the same purpose, but with a radically different (and far more hackish) implementation. honestly? respect for the audacity π₯
π SUPPORT π
you can find this documentation for this module with the perldoc command:
perldoc MooseX::NonMoose
you can also look for information at:
- MetaCPAN β <https://metacpan.org/release/MooseX-NonMoose>
- Github β <https://github.com/uperl/moosex-nonmoose>
- RT: CPAN's request tracker β <http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-NonMoose>
π AUTHOR π
- original author: Jesse Luehrs <doy@tozt.net>
- current maintainer: Graham Ollis <plicease@cpan.org>
πΈ COPYRIGHT AND LICENSE πΈ
this software is copyright (c) 2009-2025 by Jesse Luehrs.
this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. π«Ά
and that's the whole page, bestie!! this doc was served up by perl v5.40.0 on 2025-01-11 π thanks for reading all the way to the bottom, you're literally the main character β bye ily πβ¨
π¦ where this page comes from
- debian trixie: libmoosex-nonmoose-perl 0.27-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmoosex-nonmoose-perl 0.27-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmoosex-nonmoose-perl 0.27-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