Moose->unimport has to be caled as a method for the into_level stuff to work
[gitmo/Moose.git] / lib / Moose / Cookbook / Extending / Recipe2.pod
CommitLineData
6fa0a13f 1
2=pod
3
4=head1 NAME
5
6Moose::Cookbook::Extending::Recipe2 - Acting like Moose.pm and providing sugar Moose-style
7
8=head1 SYNOPSIS
9
10 package MyApp::Mooseish;
11
12 use strict;
13 use warnings;
14
15 our @EXPORT = qw( has_table );
16
17 use base 'Exporter';
18 use Class::MOP;
19 use Moose ();
20
21 sub import {
22 my $caller = caller();
23
24 return if $caller eq 'main';
25
26 Moose::init_meta( $caller,
27 undef, # object base class
28 'MyApp::Meta::Class',
29 );
30
31 Moose->import( { into => $caller }, @_ );
32
33 __PACKAGE__->export_to_level( 1, @_ );
34 }
35
36 sub unimport {
37 my $caller = caller();
38
ce265cc3 39 Moose::remove_keywords(
40 source => __PACKAGE__,
41 package => $caller,
42 keywords => \@EXPORT,
43 );
6fa0a13f 44
99a218ef 45 Moose->unimport( { into_level => 1 } );
6fa0a13f 46 }
47
48 sub has_table {
49 my $caller = caller();
50
51 $caller->meta()->table(shift);
52 }
53
54=head1 DESCRIPTION
55
56The code above shows what it takes to provide an import-based
9a8b19be 57interface just like C<Moose.pm>. This recipe builds on
58L<Moose::Cookbook::Extending::Recipe1>. Instead of providing our own
59object base class, we provide our own metaclass class, and we also
60export a sugar subroutine C<has_table()>.
6fa0a13f 61
62Given the above code, you can now replace all instances of C<use
63Moose> with C<use MyApp::Mooseish>. Similarly, C<no Moose> is now
64replaced with C<no MyApp::Mooseish>.
65
66=head1 WARNING
67
68This recipe covers a fairly undocumented and ugly part of Moose, and
69the techniques described here may be deprecated in a future
70release. If this happens, there will be plenty of warning, as a number
71of C<MooseX> modules on CPAN already use these techniques.
72
73=head1 HOW IT IS USED
74
75The purpose of all this code is to provide a Moose-like
76interface. Here's what it would look like in actual use:
77
78 package MyApp::User;
79
80 use MyApp::Mooseish;
81
82 has_table 'User';
83
84 has 'username';
85 has 'password';
86
87 sub login { ... }
88
89 no MyApp::Mooseish;
90
91All of the normal Moose sugar (C<has()>, C<with()>, etc) is available
92when you C<use MyApp::Mooseish>.
93
94=head1 DISSECTION
95
96The first bit of magic is the call to C<Moose::init_meta()>. What this
97does is create a metaclass for the specified class. Normally, this is
98called by C<Moose.pm> in its own C<import()> method. However, we can
99call it first in order to provide an alternate metaclass class. We
100could also provide an alternate base object class to replace
101C<Moose::Object> (see L<Moose::Cookbook::Extending::Recipe1> for an
102example).
103
104The C<Moose::init_meta()> call takes three parameters. The first is
105the class for which we are initializing a metaclass object. The second
106is the base object, which is L<Moose::Object> by default. The third
107argument is the metaclass class, which is C<Moose::Meta::Class> by
108default.
109
110The next bit of magic is this:
111
112 Moose->import( { into => $caller } );
113
114This use of "into" is actually part of the C<Sub::Exporter> API, which
115C<Moose.pm> uses internally to export things like C<has()> and
116C<extends()>.
117
118Finally, we call C<< __PACKAGE__->export_to_level() >>. This method
119actually comes from C<Exporter>.
120
121This is all a bit fragile since it doesn't stack terribly well. You
122can basically only have one Moose-alike module. This may be fixed in
123the still-notional C<MooseX::Exporter> module someday.
124
ce265cc3 125The C<unimport()> subroutine calls the C<remove_keywords> function
126from Moose. This function removes only the keywords exported by
127this module. More precisely, C<remove_keywords> removes from the
128C<package> package the keywords given by the C<keywords> argument
129that were created in the C<source> package.
6fa0a13f 130
131Finally, we have our C<has_table()> subroutine. This provides a bit of
132sugar that looks a lot like C<has()>.
133
134=head1 AUTHOR
135
136Dave Rolsky E<lt>autarch@urth.orgE<gt>
137
138=head1 COPYRIGHT AND LICENSE
139
140Copyright 2006-2008 by Infinity Interactive, Inc.
141
142L<http://www.iinteractive.com>
143
144This library is free software; you can redistribute it and/or modify
145it under the same terms as Perl itself.
146
147=pod