Add regexp handles
[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;
1bd3c83e 17 ${^WARNING_BITS} = $warnings::Bits{all}; # warnings->import;
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;
8cbcbb47 146
db9fc237 147 foreach my $arg(@args){
1ff34b4c 148 if($arg =~ s/^-//){
1bd3c83e 149 Mouse::Util::not_supported("-$arg");
1ff34b4c 150 }
151 elsif($arg =~ s/^://){
1bd3c83e 152 my $group = $spec->{groups}{$arg}
1ff34b4c 153 || confess(qq{The $package package does not export the group "$arg"});
db9fc237 154 push @exports, @{$group};
155 }
156 else{
157 push @exports, $arg;
158 }
159 }
160
99934a1b 161 $^H |= _strict_bits; # strict->import;
1ff34b4c 162 ${^WARNING_BITS} = $warnings::Bits{all}; # warnings->import;
db9fc237 163
0eb86915 164 if($into eq 'main' && !$spec->{_export_to_main}){
1ff34b4c 165 warn qq{$package does not export its sugar to the 'main' package.\n};
db9fc237 166 return;
167 }
168
1ff34b4c 169 if($spec->{INIT_META}){
170 foreach my $init_meta(@{$spec->{INIT_META}}){
171 $into->$init_meta(for_class => $into);
172 }
db9fc237 173
1ff34b4c 174 # _apply_meta_traits($into); # TODO
db9fc237 175 }
176
1ff34b4c 177 if(@exports){
178 foreach my $keyword(@exports){
179 no strict 'refs';
180 *{$into.'::'.$keyword} = $spec->{EXPORTS}{$keyword}
181 || confess(qq{The $package package does not export "$keyword"});
182 }
183 }
184 else{
185 my $default = $spec->{DEFAULT};
186 while(my($keyword, $code) = each %{$default}){
187 no strict 'refs';
188 *{$into.'::'.$keyword} = $code;
189 }
db9fc237 190 }
191 return;
192}
193
194# the entity of general unimport()
195sub do_unimport {
1ff34b4c 196 my($package, $arg) = @_;
db9fc237 197
1ff34b4c 198 my $spec = $SPEC{$package}
199 || confess("The package $package does not use Mouse::Exporter");
db9fc237 200
201 my $from = _get_caller_package($arg);
202
203 my $stash = do{
204 no strict 'refs';
205 \%{$from . '::'}
206 };
207
208 for my $keyword (@{ $spec->{REMOVABLES} }) {
4ee3190e 209 my $gv = \$stash->{$keyword};
210 if(ref($gv) eq 'GLOB' && *{$gv}{CODE} == $spec->{EXPORTS}{$keyword}){ # make sure it is from us
211 delete $stash->{$keyword};
212 }
db9fc237 213 }
214 return;
215}
216
8cbcbb47 217# 1 extra level because it's called by import so there's a layer\r
218# of indirection\r
219sub _LEVEL(){ 1 }
220
db9fc237 221sub _get_caller_package {
222 my($arg) = @_;
223
db9fc237 224 if(ref $arg){
225 return defined($arg->{into}) ? $arg->{into}
8cbcbb47 226 : defined($arg->{into_level}) ? scalar caller(_LEVEL + $arg->{into_level})
227 : scalar caller(_LEVEL);
db9fc237 228 }
229 else{
8cbcbb47 230 return scalar caller(_LEVEL);
db9fc237 231 }
232}
233
99934a1b 234#sub _spec{ %SPEC }
235
db9fc237 2361;
237
238__END__
239
240=head1 NAME
241
8cbcbb47 242Mouse::Exporter - make an import() and unimport() just like Mouse.pm
db9fc237 243
244=head1 SYNOPSIS
245
8cbcbb47 246 package MyApp::Mouse;\r
247\r
248 use Mouse ();\r
249 use Mouse::Exporter;\r
250\r
251 Mouse::Exporter->setup_import_methods(\r
252 as_is => [ 'has_rw', 'other_sugar', \&Some::Random::thing ],\r
253 also => 'Mouse',\r
254 );\r
255\r
256 sub has_rw {
257 my $meta = caller->meta;\r
258 my ( $name, %options ) = @_;\r
259 $meta->add_attribute(\r
260 $name,\r
261 is => 'rw',\r
262 %options,\r
263 );\r
264 }\r
265\r
266 # then later ...\r
267 package MyApp::User;\r
268
269 use MyApp::Mouse;\r
270\r
271 has 'name';\r
272 has_rw 'size';\r
273 thing;\r
274\r
275 no MyApp::Mouse;
db9fc237 276
277=head1 DESCRIPTION
278
8cbcbb47 279This module encapsulates the exporting of sugar functions in a\r
280C<Mouse.pm>-like manner. It does this by building custom C<import>,\r
281C<unimport> methods for your module, based on a spec you provide.\r
282
283Note that C<Mouse::Exporter> does not provide the C<with_meta> option,
284but you can easily get the metaclass by C<< caller->meta >> as L</SYNOPSIS> shows.
db9fc237 285
e6dc493d 286=head1 METHODS
287
288=head2 C<< setup_import_methods( ARGS ) >>
289
290=head2 C<< build_import_methods( ARGS ) -> (\&import, \&unimport) >>
291
db9fc237 292=head1 SEE ALSO
293
294L<Moose::Exporter>
295
5ea1528f 296=cut
297