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