MooseX::NonMoose(3pm)

πŸ“¦
version 0.27 Β· 5 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

πŸ’– 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::Class and MooseX::NonMoose::Meta::Role::Constructor can also be applied to your metaclasses manually β€” either by passing a -traits option to your use Moose; line, or by applying them using Moose::Util::MetaRole in a Moose::Exporter-based package.

and like, here's the breakdown on those two, iykyk:

  • MooseX::NonMoose::Meta::Role::Class is 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::Constructor as 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::InsideOut is the most general solution β€” it should work with any class. for globref-based classes in particular, MooseX::GlobRef will also allow Moose to work. for more information, see the 032-moosex-insideout and 033-moosex-globref tests bundled with this dist 🧚
  • modifying your class' @ISA after an initial extends call will potentially cause problems if any of those new entries in the @ISA override the constructor. MooseX::NonMoose wraps the nearest new() method at the time extends is called and will not see any other new() methods in the @ISA hierarchy. 😭
  • completely overriding the constructor in a class using MooseX::NonMoose (i.e. using sub 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:


πŸ’œ AUTHOR πŸ’œ

  • original author: Jesse Luehrs <doy@tozt.net>
  • current maintainer: Graham Ollis <plicease@cpan.org>

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