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
1
2 =pod
3
4 =head1 NAME
5
6 Moose::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
39       Moose::remove_keywords(
40           source   => __PACKAGE__,
41           package  => $caller,
42           keywords => \@EXPORT,
43       );
44
45       Moose->unimport( { into_level => 1 } );
46   }
47
48   sub has_table {
49       my $caller = caller();
50
51       $caller->meta()->table(shift);
52   }
53
54 =head1 DESCRIPTION
55
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()>.
61
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>.
65
66 =head1 WARNING
67
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.
72
73 =head1 HOW IT IS USED
74
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:
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
91 All of the normal Moose sugar (C<has()>, C<with()>, etc) is available
92 when you C<use MyApp::Mooseish>.
93
94 =head1 DISSECTION
95
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
102 example).
103
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
108 default.
109
110 The next bit of magic is this:
111
112   Moose->import( { into => $caller } );
113
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
116 C<extends()>.
117
118 Finally, we call C<< __PACKAGE__->export_to_level() >>. This method
119 actually comes from C<Exporter>.
120
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.
124
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.
130
131 Finally, we have our C<has_table()> subroutine. This provides a bit of
132 sugar that looks a lot like C<has()>.
133
134 =head1 AUTHOR
135
136 Dave Rolsky E<lt>autarch@urth.orgE<gt>
137
138 =head1 COPYRIGHT AND LICENSE
139
140 Copyright 2006-2008 by Infinity Interactive, Inc.
141
142 L<http://www.iinteractive.com>
143
144 This library is free software; you can redistribute it and/or modify
145 it under the same terms as Perl itself.
146
147 =pod