Checking in changes prior to tagging of version 0.50. Changelog diff is:
[gitmo/Mouse.git] / lib / Mouse / Exporter.pm
CommitLineData
db9fc237 1package Mouse::Exporter;
2use strict;
3use warnings;
4
5use Carp qw(confess);
6
db9fc237 7my %SPEC;
8
99934a1b 9use constant _strict_bits => strict::bits(qw(subs refs vars));
1ff34b4c 10
0eb86915 11# it must be "require", because Mouse::Util depends on Mouse::Exporter,
12# which depends on Mouse::Util::import()
13require Mouse::Util;
14
1bd3c83e 15sub import{
99934a1b 16 $^H |= _strict_bits; # strict->import;
4b55a023 17 ${^WARNING_BITS} |= $warnings::Bits{all}; # warnings->import;
1bd3c83e 18 return;
19}
20
0eb86915 21
db9fc237 22sub setup_import_methods{
23 my($class, %args) = @_;
24
25 my $exporting_package = $args{exporting_package} ||= caller();
26
e6dc493d 27 my($import, $unimport) = $class->build_import_methods(%args);
28
29 no strict 'refs';
30
31 *{$exporting_package . '::import'} = $import;
32 *{$exporting_package . '::unimport'} = $unimport;
33
34 # for backward compatibility
35 *{$exporting_package . '::export_to_level'} = sub{
36 my($package, $level, undef, @args) = @_; # the third argument is redundant
37 $package->import({ into_level => $level + 1 }, @args);
38 };
39 *{$exporting_package . '::export'} = sub{
40 my($package, $into, @args) = @_;
41 $package->import({ into => $into }, @args);
42 };
43 return;
44}
45
46sub build_import_methods{
47 my($class, %args) = @_;
48
49 my $exporting_package = $args{exporting_package} ||= caller();
50
db9fc237 51 $SPEC{$exporting_package} = \%args;
52
53 # canonicalize args
54 my @export_from;
55 if($args{also}){
56 my %seen;
57 my @stack = ($exporting_package);
58
59 while(my $current = shift @stack){
60 push @export_from, $current;
61
62 my $also = $SPEC{$current}{also} or next;
0eb86915 63 push @stack, grep{ !$seen{$_}++ } ref($also) ? @{ $also } : $also;
db9fc237 64 }
65 }
66 else{
67 @export_from = ($exporting_package);
68 }
69
70 {
71 my %exports;
72 my @removables;
4ee3190e 73 my @all;
db9fc237 74
1ff34b4c 75 my @init_meta_methods;
76
db9fc237 77 foreach my $package(@export_from){
78 my $spec = $SPEC{$package} or next;
79
80 if(my $as_is = $spec->{as_is}){
81 foreach my $thingy (@{$as_is}){
4ee3190e 82 my($code_package, $code_name, $code);
db9fc237 83
84 if(ref($thingy)){
db9fc237 85 $code = $thingy;
1bd3c83e 86 ($code_package, $code_name) = Mouse::Util::get_code_info($code);
db9fc237 87 }
88 else{
89 no strict 'refs';
4ee3190e 90 $code_package = $package;
91 $code_name = $thingy;
92 $code = \&{ $code_package . '::' . $code_name };
db9fc237 93 }
94
4ee3190e 95 push @all, $code_name;
96 $exports{$code_name} = $code;
97 if($code_package eq $package){
98 push @removables, $code_name;
99 }
db9fc237 100 }
101 }
1ff34b4c 102
103 if(my $init_meta = $package->can('init_meta')){
104 if(!grep{ $_ == $init_meta } @init_meta_methods){
594f4147 105 push @init_meta_methods, $init_meta;
1ff34b4c 106 }
107 }
db9fc237 108 }
109 $args{EXPORTS} = \%exports;
110 $args{REMOVABLES} = \@removables;
111
1bd3c83e 112 $args{groups}{all} ||= \@all;
1ff34b4c 113
1bd3c83e 114 if(my $default_list = $args{groups}{default}){
1ff34b4c 115 my %default;
116 foreach my $keyword(@{$default_list}){
117 $default{$keyword} = $exports{$keyword}
118 || confess(qq{The $exporting_package package does not export "$keyword"});
119 }
120 $args{DEFAULT} = \%default;
121 }
122 else{
1bd3c83e 123 $args{groups}{default} ||= \@all;
124 $args{DEFAULT} = $args{EXPORTS};
1ff34b4c 125 }
126
127 if(@init_meta_methods){
128 $args{INIT_META} = \@init_meta_methods;
129 }
db9fc237 130 }
131
e6dc493d 132 return (\&do_import, \&do_unimport);
db9fc237 133}
134
db9fc237 135
136# the entity of general import()
137sub do_import {
1ff34b4c 138 my($package, @args) = @_;
db9fc237 139
1ff34b4c 140 my $spec = $SPEC{$package}
141 || confess("The package $package package does not use Mouse::Exporter");
db9fc237 142
143 my $into = _get_caller_package(ref($args[0]) ? shift @args : undef);
144
145 my @exports;
45bbec05 146 my @traits;
8cbcbb47 147
45bbec05 148 while(@args){
149 my $arg = shift @args;
1ff34b4c 150 if($arg =~ s/^-//){
45bbec05 151 if($arg eq 'traits'){
f12892e5 152 push @traits, ref($args[0]) ? @{shift(@args)} : shift(@args);
45bbec05 153 }
154 else {
155 Mouse::Util::not_supported("-$arg");
156 }
1ff34b4c 157 }
158 elsif($arg =~ s/^://){
1bd3c83e 159 my $group = $spec->{groups}{$arg}
1ff34b4c 160 || confess(qq{The $package package does not export the group "$arg"});
db9fc237 161 push @exports, @{$group};
162 }
163 else{
164 push @exports, $arg;
165 }
166 }
167
99934a1b 168 $^H |= _strict_bits; # strict->import;
4b55a023 169 ${^WARNING_BITS} |= $warnings::Bits{all}; # warnings->import;
db9fc237 170
1ff34b4c 171 if($spec->{INIT_META}){
45bbec05 172 my $meta;
1ff34b4c 173 foreach my $init_meta(@{$spec->{INIT_META}}){
134f7bcb 174 $meta = $package->$init_meta(for_class => $into);
1ff34b4c 175 }
db9fc237 176
45bbec05 177 if(@traits){
178 my $type = (split /::/, ref $meta)[-1]; # e.g. "Class" for "My::Meta::Class"
179 @traits =
f12892e5 180 map{
181 ref($_) ? $_
182 : Mouse::Util::resolve_metaclass_alias($type => $_, trait => 1)
183 } @traits;
45bbec05 184
45bbec05 185 require Mouse::Util::MetaRole;
3d7c6ec9 186 Mouse::Util::MetaRole::apply_metaroles(
187 for => $into,
188 Mouse::Util::is_a_metarole($into->meta)
189 ? (role_metaroles => { role => \@traits })
190 : (class_metaroles => { class => \@traits }),
45bbec05 191 );
192 }
db9fc237 193 }
f12892e5 194 elsif(@traits){
195 Carp::confess("Cannot provide traits when $package does not have an init_meta() method");
196 }
db9fc237 197
1ff34b4c 198 if(@exports){
199 foreach my $keyword(@exports){
200 no strict 'refs';
201 *{$into.'::'.$keyword} = $spec->{EXPORTS}{$keyword}
202 || confess(qq{The $package package does not export "$keyword"});
203 }
204 }
205 else{
206 my $default = $spec->{DEFAULT};
207 while(my($keyword, $code) = each %{$default}){
208 no strict 'refs';
209 *{$into.'::'.$keyword} = $code;
210 }
db9fc237 211 }
212 return;
213}
214
215# the entity of general unimport()
216sub do_unimport {
1ff34b4c 217 my($package, $arg) = @_;
db9fc237 218
1ff34b4c 219 my $spec = $SPEC{$package}
220 || confess("The package $package does not use Mouse::Exporter");
db9fc237 221
222 my $from = _get_caller_package($arg);
223
224 my $stash = do{
225 no strict 'refs';
226 \%{$from . '::'}
227 };
228
229 for my $keyword (@{ $spec->{REMOVABLES} }) {
21df8e42 230 next if !exists $stash->{$keyword};
4ee3190e 231 my $gv = \$stash->{$keyword};
232 if(ref($gv) eq 'GLOB' && *{$gv}{CODE} == $spec->{EXPORTS}{$keyword}){ # make sure it is from us
233 delete $stash->{$keyword};
234 }
db9fc237 235 }
236 return;
237}
238
239sub _get_caller_package {
240 my($arg) = @_;
241
7ad9df77 242 # We need one extra level because it's called by import so there's a layer
243 # of indirection
db9fc237 244 if(ref $arg){
245 return defined($arg->{into}) ? $arg->{into}
7ad9df77 246 : defined($arg->{into_level}) ? scalar caller(1 + $arg->{into_level})
247 : scalar caller(1);
db9fc237 248 }
249 else{
7ad9df77 250 return scalar caller(1);
db9fc237 251 }
252}
253
99934a1b 254#sub _spec{ %SPEC }
255
db9fc237 2561;
db9fc237 257__END__
258
259=head1 NAME
260
8cbcbb47 261Mouse::Exporter - make an import() and unimport() just like Mouse.pm
db9fc237 262
a25ca8d6 263=head1 VERSION
264
e9148d13 265This document describes Mouse version 0.50
a25ca8d6 266
db9fc237 267=head1 SYNOPSIS
268
7ad9df77 269 package MyApp::Mouse;
270
271 use Mouse ();
272 use Mouse::Exporter;
273
274 Mouse::Exporter->setup_import_methods(
275 as_is => [ 'has_rw', 'other_sugar', \&Some::Random::thing ],
276 also => 'Mouse',
277 );
278
8cbcbb47 279 sub has_rw {
7ad9df77 280 my $meta = caller->meta;
281 my ( $name, %options ) = @_;
282 $meta->add_attribute(
283 $name,
284 is => 'rw',
285 %options,
286 );
287 }
288
289 # then later ...
290 package MyApp::User;
291
292 use MyApp::Mouse;
293
294 has 'name';
295 has_rw 'size';
296 thing;
297
8cbcbb47 298 no MyApp::Mouse;
db9fc237 299
300=head1 DESCRIPTION
301
7ad9df77 302This module encapsulates the exporting of sugar functions in a
303C<Mouse.pm>-like manner. It does this by building custom C<import>,
304C<unimport> methods for your module, based on a spec you provide.
8cbcbb47 305
306Note that C<Mouse::Exporter> does not provide the C<with_meta> option,
307but you can easily get the metaclass by C<< caller->meta >> as L</SYNOPSIS> shows.
db9fc237 308
e6dc493d 309=head1 METHODS
310
311=head2 C<< setup_import_methods( ARGS ) >>
312
313=head2 C<< build_import_methods( ARGS ) -> (\&import, \&unimport) >>
314
db9fc237 315=head1 SEE ALSO
316
317L<Moose::Exporter>
318
5ea1528f 319=cut
320