Commit | Line | Data |
db9fc237 |
1 | package Mouse::Exporter; |
2 | use strict; |
3 | use warnings; |
4 | |
5 | use Carp qw(confess); |
6 | |
db9fc237 |
7 | my %SPEC; |
8 | |
1ff34b4c |
9 | my $strict_bits = strict::bits(qw(subs refs vars)); |
10 | |
0eb86915 |
11 | # it must be "require", because Mouse::Util depends on Mouse::Exporter, |
12 | # which depends on Mouse::Util::import() |
13 | require Mouse::Util; |
14 | |
1bd3c83e |
15 | sub import{ |
16 | $^H |= $strict_bits; # strict->import; |
17 | ${^WARNING_BITS} = $warnings::Bits{all}; # warnings->import; |
18 | return; |
19 | } |
20 | |
0eb86915 |
21 | |
db9fc237 |
22 | sub setup_import_methods{ |
23 | my($class, %args) = @_; |
24 | |
25 | my $exporting_package = $args{exporting_package} ||= caller(); |
26 | |
27 | $SPEC{$exporting_package} = \%args; |
28 | |
29 | # canonicalize args |
30 | my @export_from; |
31 | if($args{also}){ |
32 | my %seen; |
33 | my @stack = ($exporting_package); |
34 | |
35 | while(my $current = shift @stack){ |
36 | push @export_from, $current; |
37 | |
38 | my $also = $SPEC{$current}{also} or next; |
0eb86915 |
39 | push @stack, grep{ !$seen{$_}++ } ref($also) ? @{ $also } : $also; |
db9fc237 |
40 | } |
41 | } |
42 | else{ |
43 | @export_from = ($exporting_package); |
44 | } |
45 | |
46 | { |
47 | my %exports; |
48 | my @removables; |
4ee3190e |
49 | my @all; |
db9fc237 |
50 | |
1ff34b4c |
51 | my @init_meta_methods; |
52 | |
db9fc237 |
53 | foreach my $package(@export_from){ |
54 | my $spec = $SPEC{$package} or next; |
55 | |
56 | if(my $as_is = $spec->{as_is}){ |
57 | foreach my $thingy (@{$as_is}){ |
4ee3190e |
58 | my($code_package, $code_name, $code); |
db9fc237 |
59 | |
60 | if(ref($thingy)){ |
db9fc237 |
61 | $code = $thingy; |
1bd3c83e |
62 | ($code_package, $code_name) = Mouse::Util::get_code_info($code); |
db9fc237 |
63 | } |
64 | else{ |
65 | no strict 'refs'; |
4ee3190e |
66 | $code_package = $package; |
67 | $code_name = $thingy; |
68 | $code = \&{ $code_package . '::' . $code_name }; |
db9fc237 |
69 | } |
70 | |
4ee3190e |
71 | push @all, $code_name; |
72 | $exports{$code_name} = $code; |
73 | if($code_package eq $package){ |
74 | push @removables, $code_name; |
75 | } |
db9fc237 |
76 | } |
77 | } |
1ff34b4c |
78 | |
79 | if(my $init_meta = $package->can('init_meta')){ |
80 | if(!grep{ $_ == $init_meta } @init_meta_methods){ |
81 | unshift @init_meta_methods, $init_meta; |
82 | } |
83 | } |
db9fc237 |
84 | } |
85 | $args{EXPORTS} = \%exports; |
86 | $args{REMOVABLES} = \@removables; |
87 | |
1bd3c83e |
88 | $args{groups}{all} ||= \@all; |
1ff34b4c |
89 | |
1bd3c83e |
90 | if(my $default_list = $args{groups}{default}){ |
1ff34b4c |
91 | my %default; |
92 | foreach my $keyword(@{$default_list}){ |
93 | $default{$keyword} = $exports{$keyword} |
94 | || confess(qq{The $exporting_package package does not export "$keyword"}); |
95 | } |
96 | $args{DEFAULT} = \%default; |
97 | } |
98 | else{ |
1bd3c83e |
99 | $args{groups}{default} ||= \@all; |
100 | $args{DEFAULT} = $args{EXPORTS}; |
1ff34b4c |
101 | } |
102 | |
103 | if(@init_meta_methods){ |
104 | $args{INIT_META} = \@init_meta_methods; |
105 | } |
db9fc237 |
106 | } |
107 | |
108 | no strict 'refs'; |
109 | |
110 | *{$exporting_package . '::import'} = \&do_import; |
111 | *{$exporting_package . '::unimport'} = \&do_unimport; |
112 | |
db9fc237 |
113 | return; |
114 | } |
115 | |
db9fc237 |
116 | |
117 | # the entity of general import() |
118 | sub do_import { |
1ff34b4c |
119 | my($package, @args) = @_; |
db9fc237 |
120 | |
1ff34b4c |
121 | my $spec = $SPEC{$package} |
122 | || confess("The package $package package does not use Mouse::Exporter"); |
db9fc237 |
123 | |
124 | my $into = _get_caller_package(ref($args[0]) ? shift @args : undef); |
125 | |
126 | my @exports; |
8cbcbb47 |
127 | |
db9fc237 |
128 | foreach my $arg(@args){ |
1ff34b4c |
129 | if($arg =~ s/^-//){ |
1bd3c83e |
130 | Mouse::Util::not_supported("-$arg"); |
1ff34b4c |
131 | } |
132 | elsif($arg =~ s/^://){ |
1bd3c83e |
133 | my $group = $spec->{groups}{$arg} |
1ff34b4c |
134 | || confess(qq{The $package package does not export the group "$arg"}); |
db9fc237 |
135 | push @exports, @{$group}; |
136 | } |
137 | else{ |
138 | push @exports, $arg; |
139 | } |
140 | } |
141 | |
1ff34b4c |
142 | $^H |= $strict_bits; # strict->import; |
143 | ${^WARNING_BITS} = $warnings::Bits{all}; # warnings->import; |
db9fc237 |
144 | |
0eb86915 |
145 | if($into eq 'main' && !$spec->{_export_to_main}){ |
1ff34b4c |
146 | warn qq{$package does not export its sugar to the 'main' package.\n}; |
db9fc237 |
147 | return; |
148 | } |
149 | |
1ff34b4c |
150 | if($spec->{INIT_META}){ |
151 | foreach my $init_meta(@{$spec->{INIT_META}}){ |
152 | $into->$init_meta(for_class => $into); |
153 | } |
db9fc237 |
154 | |
1ff34b4c |
155 | # _apply_meta_traits($into); # TODO |
db9fc237 |
156 | } |
157 | |
1ff34b4c |
158 | if(@exports){ |
159 | foreach my $keyword(@exports){ |
160 | no strict 'refs'; |
161 | *{$into.'::'.$keyword} = $spec->{EXPORTS}{$keyword} |
162 | || confess(qq{The $package package does not export "$keyword"}); |
163 | } |
164 | } |
165 | else{ |
166 | my $default = $spec->{DEFAULT}; |
167 | while(my($keyword, $code) = each %{$default}){ |
168 | no strict 'refs'; |
169 | *{$into.'::'.$keyword} = $code; |
170 | } |
db9fc237 |
171 | } |
172 | return; |
173 | } |
174 | |
175 | # the entity of general unimport() |
176 | sub do_unimport { |
1ff34b4c |
177 | my($package, $arg) = @_; |
db9fc237 |
178 | |
1ff34b4c |
179 | my $spec = $SPEC{$package} |
180 | || confess("The package $package does not use Mouse::Exporter"); |
db9fc237 |
181 | |
182 | my $from = _get_caller_package($arg); |
183 | |
184 | my $stash = do{ |
185 | no strict 'refs'; |
186 | \%{$from . '::'} |
187 | }; |
188 | |
189 | for my $keyword (@{ $spec->{REMOVABLES} }) { |
4ee3190e |
190 | my $gv = \$stash->{$keyword}; |
191 | if(ref($gv) eq 'GLOB' && *{$gv}{CODE} == $spec->{EXPORTS}{$keyword}){ # make sure it is from us |
192 | delete $stash->{$keyword}; |
193 | } |
db9fc237 |
194 | } |
195 | return; |
196 | } |
197 | |
8cbcbb47 |
198 | # 1 extra level because it's called by import so there's a layer\r |
199 | # of indirection\r |
200 | sub _LEVEL(){ 1 } |
201 | |
db9fc237 |
202 | sub _get_caller_package { |
203 | my($arg) = @_; |
204 | |
db9fc237 |
205 | if(ref $arg){ |
206 | return defined($arg->{into}) ? $arg->{into} |
8cbcbb47 |
207 | : defined($arg->{into_level}) ? scalar caller(_LEVEL + $arg->{into_level}) |
208 | : scalar caller(_LEVEL); |
db9fc237 |
209 | } |
210 | else{ |
8cbcbb47 |
211 | return scalar caller(_LEVEL); |
db9fc237 |
212 | } |
213 | } |
214 | |
215 | 1; |
216 | |
217 | __END__ |
218 | |
219 | =head1 NAME |
220 | |
8cbcbb47 |
221 | Mouse::Exporter - make an import() and unimport() just like Mouse.pm |
db9fc237 |
222 | |
223 | =head1 SYNOPSIS |
224 | |
8cbcbb47 |
225 | package MyApp::Mouse;\r |
226 | \r |
227 | use Mouse ();\r |
228 | use Mouse::Exporter;\r |
229 | \r |
230 | Mouse::Exporter->setup_import_methods(\r |
231 | as_is => [ 'has_rw', 'other_sugar', \&Some::Random::thing ],\r |
232 | also => 'Mouse',\r |
233 | );\r |
234 | \r |
235 | sub has_rw { |
236 | my $meta = caller->meta;\r |
237 | my ( $name, %options ) = @_;\r |
238 | $meta->add_attribute(\r |
239 | $name,\r |
240 | is => 'rw',\r |
241 | %options,\r |
242 | );\r |
243 | }\r |
244 | \r |
245 | # then later ...\r |
246 | package MyApp::User;\r |
247 | |
248 | use MyApp::Mouse;\r |
249 | \r |
250 | has 'name';\r |
251 | has_rw 'size';\r |
252 | thing;\r |
253 | \r |
254 | no MyApp::Mouse; |
db9fc237 |
255 | |
256 | =head1 DESCRIPTION |
257 | |
8cbcbb47 |
258 | This module encapsulates the exporting of sugar functions in a\r |
259 | C<Mouse.pm>-like manner. It does this by building custom C<import>,\r |
260 | C<unimport> methods for your module, based on a spec you provide.\r |
261 | |
262 | Note that C<Mouse::Exporter> does not provide the C<with_meta> option, |
263 | but you can easily get the metaclass by C<< caller->meta >> as L</SYNOPSIS> shows. |
db9fc237 |
264 | |
265 | =head1 SEE ALSO |
266 | |
267 | L<Moose::Exporter> |
268 | |
269 | =cut |