File Coverage

blib/lib/MooseX/NonMoose.pm
Criterion Covered Total %
statement 11 11 100.0
branch 1 2 50.0
condition 1 3 33.3
subroutine 3 3 100.0
pod 0 1 0.0
total 16 20 80.0


line stmt bran cond sub pod time code
1             package MooseX::NonMoose;
2              
3 22     22   14438722 use Moose::Exporter;
  22         59  
  22         241  
4 22     22   2062 use Moose::Util;
  22         45  
  22         217  
5              
6             # ABSTRACT: easy subclassing of non-Moose classes
7             our $VERSION = '0.27'; # VERSION
8              
9              
10             my ($import, $unimport, $init_meta) = Moose::Exporter->build_import_methods(
11             class_metaroles => {
12             class => ['MooseX::NonMoose::Meta::Role::Class'],
13             constructor => ['MooseX::NonMoose::Meta::Role::Constructor'],
14             },
15             install => [qw(import unimport)],
16             );
17              
18             sub init_meta {
19 35     35 0 149652 my $package = shift;
20 35         198 my %options = @_;
21 35         217 my $meta = Moose::Util::find_meta($options{for_class});
22 35 50 33     837 Carp::cluck('Roles have no use for MooseX::NonMoose')
23             if $meta && $meta->isa('Moose::Meta::Role');
24 35         184 $package->$init_meta(@_);
25             }
26              
27              
28             1;
29              
30             __END__
31              
32             =pod
33              
34             =encoding UTF-8
35              
36             =head1 NAME
37              
38             MooseX::NonMoose - easy subclassing of non-Moose classes
39              
40             =head1 VERSION
41              
42             version 0.27
43              
44             =head1 SYNOPSIS
45              
46             package Term::VT102::NBased;
47             use Moose;
48             use MooseX::NonMoose;
49             extends 'Term::VT102';
50              
51             has [qw/x_base y_base/] => (
52             is => 'ro',
53             isa => 'Int',
54             default => 1,
55             );
56              
57             around x => sub {
58             my $orig = shift;
59             my $self = shift;
60             $self->$orig(@_) + $self->x_base - 1;
61             };
62              
63             # ... (wrap other methods)
64              
65             no Moose;
66             # no need to fiddle with inline_constructor here
67             __PACKAGE__->meta->make_immutable;
68              
69             my $vt = Term::VT102::NBased->new(x_base => 0, y_base => 0);
70              
71             =head1 DESCRIPTION
72              
73             C<MooseX::NonMoose> allows for easily subclassing non-Moose classes with Moose,
74             taking care of the annoying details connected with doing this, such as setting
75             up proper inheritance from L<Moose::Object> and installing (and inlining, at
76             C<make_immutable> time) a constructor that makes sure things like C<BUILD>
77             methods are called. It tries to be as non-intrusive as possible - when this
78             module is used, inheriting from non-Moose classes and inheriting from Moose
79             classes should work identically, aside from the few caveats mentioned below.
80             One of the goals of this module is that including it in a
81             L<Moose::Exporter>-based package used across an entire application should be
82             possible, without interfering with classes that only inherit from Moose
83             modules, or even classes that don't inherit from anything at all.
84              
85             There are several ways to use this module. The most straightforward is to just
86             C<use MooseX::NonMoose;> in your class; this should set up everything necessary
87             for extending non-Moose modules. L<MooseX::NonMoose::Meta::Role::Class> and
88             L<MooseX::NonMoose::Meta::Role::Constructor> can also be applied to your
89             metaclasses manually, either by passing a C<-traits> option to your C<use
90             Moose;> line, or by applying them using L<Moose::Util::MetaRole> in a
91             L<Moose::Exporter>-based package. L<MooseX::NonMoose::Meta::Role::Class> is the
92             part that provides the main functionality of this module; if you don't care
93             about inlining, this is all you need to worry about. Applying
94             L<MooseX::NonMoose::Meta::Role::Constructor> as well will provide an inlined
95             constructor when you immutabilize your class.
96              
97             C<MooseX::NonMoose> allows you to manipulate the argument list that gets passed
98             to the superclass constructor by defining a C<FOREIGNBUILDARGS> method. This is
99             called with the same argument list as the C<BUILDARGS> method, but should
100             return a list of arguments to pass to the superclass constructor. This allows
101             C<MooseX::NonMoose> to support superclasses whose constructors would get
102             confused by the extra arguments that Moose requires (for attributes, etc.)
103              
104             Not all non-Moose classes use C<new> as the name of their constructor. This
105             module allows you to extend these classes by explicitly stating which method is
106             the constructor, during the call to C<extends>. The syntax looks like this:
107              
108             extends 'Foo' => { -constructor_name => 'create' };
109              
110             similar to how you can already pass C<-version> in the C<extends> call in a
111             similar way.
112              
113             =head1 BUGS/CAVEATS
114              
115             =over 4
116              
117             =item * The reference that the non-Moose class uses as its instance type
118             B<must> match the instance type that Moose is using. Moose's default instance
119             type is a hashref, but other modules exist to make Moose use other instance
120             types. L<MooseX::InsideOut> is the most general solution - it should work with
121             any class. For globref-based classes in particular, L<MooseX::GlobRef> will
122             also allow Moose to work. For more information, see the C<032-moosex-insideout>
123             and C<033-moosex-globref> tests bundled with this dist.
124              
125             =item * Modifying your class' C<@ISA> after an initial C<extends> call will potentially
126             cause problems if any of those new entries in the C<@ISA> override the constructor.
127             C<MooseX::NonMoose> wraps the nearest C<new()> method at the time C<extends>
128             is called and will not see any other C<new()> methods in the @ISA hierarchy.
129              
130             =item * Completely overriding the constructor in a class using
131             C<MooseX::NonMoose> (i.e. using C<sub new { ... }>) currently doesn't work,
132             although using method modifiers on the constructor should work identically to
133             normal Moose classes.
134              
135             =back
136              
137             Please report any bugs to GitHub Issues at
138             L<https://github.com/uperl/moosex-nonmoose/issues>.
139              
140             =head1 SEE ALSO
141              
142             =over 4
143              
144             =item * L<Moose::Manual::FAQ/How do I make non-Moose constructors work with Moose?>
145              
146             =item * L<MooseX::Alien>
147              
148             serves the same purpose, but with a radically different (and far more hackish)
149             implementation.
150              
151             =back
152              
153             =head1 SUPPORT
154              
155             You can find this documentation for this module with the perldoc command.
156              
157             perldoc MooseX::NonMoose
158              
159             You can also look for information at:
160              
161             =over 4
162              
163             =item * MetaCPAN
164              
165             L<https://metacpan.org/release/MooseX-NonMoose>
166              
167             =item * Github
168              
169             L<https://github.com/uperl/moosex-nonmoose>
170              
171             =item * RT: CPAN's request tracker
172              
173             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-NonMoose>
174              
175             =back
176              
177             =for Pod::Coverage init_meta
178              
179             =head1 AUTHOR
180              
181             Original author: Jesse Luehrs E<lt>doy@tozt.netE<gt>
182              
183             Current maintainer: Graham Ollis E<lt>plicease@cpan.orgE<gt>
184              
185             =head1 COPYRIGHT AND LICENSE
186              
187             This software is copyright (c) 2009-2025 by Jesse Luehrs.
188              
189             This is free software; you can redistribute it and/or modify it under
190             the same terms as the Perl 5 programming language system itself.
191              
192             =cut