6 Moose::Cookbook::Extending::Recipe2 - Acting like Moose.pm and providing sugar Moose-style
10 package MyApp::Mooseish;
15 our @EXPORT = qw( has_table );
22 my $caller = caller();
24 return if $caller eq 'main';
26 Moose::init_meta( $caller,
27 undef, # object base class
31 Moose->import( { into => $caller }, @_ );
33 __PACKAGE__->export_to_level( 1, @_ );
37 my $caller = caller();
39 Moose::remove_keywords(
40 source => __PACKAGE__,
45 Moose->unimport( { into_level => 1 } );
49 my $caller = caller();
51 $caller->meta()->table(shift);
56 The code above shows what it takes to provide an import-based
57 interface just like C<Moose.pm>. This recipe builds on
58 L<Moose::Cookbook::Extending::Recipe1>. Instead of providing our own
59 object base class, we provide our own metaclass class, and we also
60 export a sugar subroutine C<has_table()>.
62 Given the above code, you can now replace all instances of C<use
63 Moose> with C<use MyApp::Mooseish>. Similarly, C<no Moose> is now
64 replaced with C<no MyApp::Mooseish>.
68 This recipe covers a fairly undocumented and ugly part of Moose, and
69 the techniques described here may be deprecated in a future
70 release. If this happens, there will be plenty of warning, as a number
71 of C<MooseX> modules on CPAN already use these techniques.
75 The purpose of all this code is to provide a Moose-like
76 interface. Here's what it would look like in actual use:
91 All of the normal Moose sugar (C<has()>, C<with()>, etc) is available
92 when you C<use MyApp::Mooseish>.
96 The first bit of magic is the call to C<Moose::init_meta()>. What this
97 does is create a metaclass for the specified class. Normally, this is
98 called by C<Moose.pm> in its own C<import()> method. However, we can
99 call it first in order to provide an alternate metaclass class. We
100 could also provide an alternate base object class to replace
101 C<Moose::Object> (see L<Moose::Cookbook::Extending::Recipe1> for an
104 The C<Moose::init_meta()> call takes three parameters. The first is
105 the class for which we are initializing a metaclass object. The second
106 is the base object, which is L<Moose::Object> by default. The third
107 argument is the metaclass class, which is C<Moose::Meta::Class> by
110 The next bit of magic is this:
112 Moose->import( { into => $caller } );
114 This use of "into" is actually part of the C<Sub::Exporter> API, which
115 C<Moose.pm> uses internally to export things like C<has()> and
118 Finally, we call C<< __PACKAGE__->export_to_level() >>. This method
119 actually comes from C<Exporter>.
121 This is all a bit fragile since it doesn't stack terribly well. You
122 can basically only have one Moose-alike module. This may be fixed in
123 the still-notional C<MooseX::Exporter> module someday.
125 The C<unimport()> subroutine calls the C<remove_keywords> function
126 from Moose. This function removes only the keywords exported by
127 this module. More precisely, C<remove_keywords> removes from the
128 C<package> package the keywords given by the C<keywords> argument
129 that were created in the C<source> package.
131 Finally, we have our C<has_table()> subroutine. This provides a bit of
132 sugar that looks a lot like C<has()>.
136 Dave Rolsky E<lt>autarch@urth.orgE<gt>
138 =head1 COPYRIGHT AND LICENSE
140 Copyright 2006-2008 by Infinity Interactive, Inc.
142 L<http://www.iinteractive.com>
144 This library is free software; you can redistribute it and/or modify
145 it under the same terms as Perl itself.