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