Commit | Line | Data |
e606ae5f |
1 | package Moose::Exporter; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
e606ae5f |
6 | use Class::MOP; |
7 | use List::MoreUtils qw( first_index uniq ); |
8 | use Moose::Util::MetaRole; |
f88cfe7c |
9 | use Scalar::Util qw(reftype); |
091ac4b7 |
10 | use Sub::Exporter 0.980; |
9f2230e9 |
11 | use Sub::Name qw(subname); |
e606ae5f |
12 | |
13 | my %EXPORT_SPEC; |
14 | |
15 | sub setup_import_methods { |
16 | my ( $class, %args ) = @_; |
17 | |
2a591aa7 |
18 | $args{exporting_package} ||= caller(); |
e606ae5f |
19 | |
95056a1e |
20 | $class->build_import_methods( |
21 | %args, |
22 | install => [qw(import unimport init_meta)] |
23 | ); |
e606ae5f |
24 | } |
25 | |
26 | sub build_import_methods { |
27 | my ( $class, %args ) = @_; |
28 | |
29 | my $exporting_package = $args{exporting_package} ||= caller(); |
30 | |
450432c8 |
31 | my $meta_lookup = $args{meta_lookup} || sub { Class::MOP::class_of(shift) }; |
4cfe8f30 |
32 | |
e606ae5f |
33 | $EXPORT_SPEC{$exporting_package} = \%args; |
34 | |
8fa582b1 |
35 | my @exports_from = $class->_follow_also($exporting_package); |
e606ae5f |
36 | |
37 | my $export_recorder = {}; |
8fa582b1 |
38 | my $is_reexport = {}; |
e606ae5f |
39 | |
e0d3eb10 |
40 | my $exports = $class->_make_sub_exporter_params( |
8fa582b1 |
41 | [ @exports_from, $exporting_package ], |
42 | $export_recorder, |
43 | $is_reexport, |
450432c8 |
44 | $meta_lookup, |
e0d3eb10 |
45 | ); |
e606ae5f |
46 | |
4cfe8f30 |
47 | my $exporter = $class->_make_exporter( |
48 | $exports, |
49 | $is_reexport, |
450432c8 |
50 | $meta_lookup, |
4cfe8f30 |
51 | ); |
e606ae5f |
52 | |
95056a1e |
53 | my %methods; |
8fa582b1 |
54 | $methods{import} = $class->_make_import_sub( |
55 | $exporting_package, |
56 | $exporter, |
57 | \@exports_from, |
4cfe8f30 |
58 | $is_reexport, |
450432c8 |
59 | $meta_lookup, |
8fa582b1 |
60 | ); |
e606ae5f |
61 | |
8fa582b1 |
62 | $methods{unimport} = $class->_make_unimport_sub( |
63 | $exporting_package, |
64 | $exports, |
65 | $export_recorder, |
4cfe8f30 |
66 | $is_reexport, |
450432c8 |
67 | $meta_lookup, |
8fa582b1 |
68 | ); |
95056a1e |
69 | |
8fa582b1 |
70 | $methods{init_meta} = $class->_make_init_meta( |
71 | $exporting_package, |
4cfe8f30 |
72 | \%args, |
450432c8 |
73 | $meta_lookup, |
8fa582b1 |
74 | ); |
95056a1e |
75 | |
76 | my $package = Class::MOP::Package->initialize($exporting_package); |
906eabcd |
77 | for my $to_install ( @{ $args{install} || [] } ) { |
95056a1e |
78 | my $symbol = '&' . $to_install; |
906eabcd |
79 | next |
80 | unless $methods{$to_install} |
81 | && !$package->has_package_symbol($symbol); |
82 | $package->add_package_symbol( $symbol, $methods{$to_install} ); |
95056a1e |
83 | } |
e606ae5f |
84 | |
8fa582b1 |
85 | return ( $methods{import}, $methods{unimport}, $methods{init_meta} ); |
e606ae5f |
86 | } |
87 | |
d004fa18 |
88 | sub _make_exporter { |
450432c8 |
89 | my ($class, $exports, $is_reexport, $meta_lookup) = @_; |
d004fa18 |
90 | |
91 | return Sub::Exporter::build_exporter( |
92 | { |
93 | exports => $exports, |
94 | groups => { default => [':all'] }, |
95 | installer => sub { |
96 | my ($arg, $to_export) = @_; |
450432c8 |
97 | my $meta = $meta_lookup->($arg->{into}); |
d004fa18 |
98 | |
99 | goto &Sub::Exporter::default_installer unless $meta; |
100 | |
101 | # don't overwrite existing symbols with our magically flagged |
102 | # version of it if we would install the same sub that's already |
103 | # in the importer |
104 | |
105 | my @filtered_to_export; |
106 | my %installed; |
107 | for (my $i = 0; $i < @{ $to_export }; $i += 2) { |
108 | my ($as, $cv) = @{ $to_export }[$i, $i + 1]; |
109 | |
110 | next if !ref($as) |
111 | && $meta->has_package_symbol('&' . $as) |
112 | && $meta->get_package_symbol('&' . $as) == $cv; |
113 | |
114 | push @filtered_to_export, $as, $cv; |
115 | $installed{$as} = 1 unless ref $as; |
116 | } |
117 | |
118 | Sub::Exporter::default_installer($arg, \@filtered_to_export); |
119 | |
120 | for my $name ( keys %{$is_reexport} ) { |
121 | no strict 'refs'; |
122 | no warnings 'once'; |
123 | next unless exists $installed{$name}; |
124 | _flag_as_reexport( \*{ join q{::}, $arg->{into}, $name } ); |
125 | } |
126 | }, |
127 | } |
128 | ); |
129 | } |
130 | |
e606ae5f |
131 | { |
132 | my $seen = {}; |
133 | |
134 | sub _follow_also { |
135 | my $class = shift; |
136 | my $exporting_package = shift; |
137 | |
138 | local %$seen = ( $exporting_package => 1 ); |
139 | |
f7d62c2e |
140 | return reverse uniq( _follow_also_real($exporting_package) ); |
e606ae5f |
141 | } |
142 | |
143 | sub _follow_also_real { |
144 | my $exporting_package = shift; |
145 | |
8fa582b1 |
146 | if ( !exists $EXPORT_SPEC{$exporting_package} ) { |
ba1a3c2f |
147 | my $loaded = Class::MOP::is_class_loaded($exporting_package); |
148 | |
149 | die "Package in also ($exporting_package) does not seem to " |
8fa582b1 |
150 | . "use Moose::Exporter" |
151 | . ( $loaded ? "" : " (is it loaded?)" ); |
ba1a3c2f |
152 | } |
e606ae5f |
153 | |
154 | my $also = $EXPORT_SPEC{$exporting_package}{also}; |
155 | |
156 | return unless defined $also; |
157 | |
158 | my @also = ref $also ? @{$also} : $also; |
159 | |
8fa582b1 |
160 | for my $package (@also) { |
161 | die |
162 | "Circular reference in 'also' parameter to Moose::Exporter between $exporting_package and $package" |
e606ae5f |
163 | if $seen->{$package}; |
164 | |
165 | $seen->{$package} = 1; |
166 | } |
167 | |
168 | return @also, map { _follow_also_real($_) } @also; |
169 | } |
170 | } |
171 | |
f88cfe7c |
172 | sub _parse_trait_aliases { |
173 | my $class = shift; |
174 | my ($package, $aliases) = @_; |
175 | |
176 | my @ret; |
177 | for my $alias (@$aliases) { |
178 | my $name; |
179 | if (ref($alias)) { |
180 | reftype($alias) eq 'ARRAY' |
181 | or Moose->throw_error(reftype($alias) . " references are not " |
182 | . "valid arguments to the 'trait_aliases' " |
183 | . "option"); |
184 | |
185 | ($alias, $name) = @$alias; |
186 | } |
187 | else { |
188 | ($name = $alias) =~ s/.*:://; |
189 | } |
190 | push @ret, subname "${package}::${name}" => sub () { $alias }; |
191 | } |
192 | |
193 | return @ret; |
194 | } |
195 | |
e606ae5f |
196 | sub _make_sub_exporter_params { |
8fa582b1 |
197 | my $class = shift; |
198 | my $packages = shift; |
199 | my $export_recorder = shift; |
ce5ccc1a |
200 | my $is_reexport = shift; |
450432c8 |
201 | my $meta_lookup = shift; |
e606ae5f |
202 | |
203 | my %exports; |
204 | |
205 | for my $package ( @{$packages} ) { |
206 | my $args = $EXPORT_SPEC{$package} |
207 | or die "The $package package does not use Moose::Exporter\n"; |
208 | |
5ac14e89 |
209 | for my $name ( @{ $args->{with_meta} } ) { |
0dd4228e |
210 | my $sub = $class->_sub_from_package( $package, $name ) |
211 | or next; |
e6a5040f |
212 | |
e606ae5f |
213 | my $fq_name = $package . '::' . $name; |
214 | |
5ac14e89 |
215 | $exports{$name} = $class->_make_wrapped_sub_with_meta( |
e606ae5f |
216 | $fq_name, |
217 | $sub, |
218 | $export_recorder, |
450432c8 |
219 | $meta_lookup, |
e606ae5f |
220 | ); |
221 | } |
222 | |
5ac14e89 |
223 | for my $name ( @{ $args->{with_caller} } ) { |
0dd4228e |
224 | my $sub = $class->_sub_from_package( $package, $name ) |
225 | or next; |
e6a5040f |
226 | |
45975bce |
227 | my $fq_name = $package . '::' . $name; |
228 | |
5ac14e89 |
229 | $exports{$name} = $class->_make_wrapped_sub( |
45975bce |
230 | $fq_name, |
231 | $sub, |
232 | $export_recorder, |
233 | ); |
45975bce |
234 | } |
235 | |
f88cfe7c |
236 | my @extra_exports = $class->_parse_trait_aliases( |
237 | $package, $args->{trait_aliases}, |
238 | ); |
239 | for my $name ( @{ $args->{as_is} }, @extra_exports ) { |
8fa582b1 |
240 | my ( $sub, $coderef_name ); |
e606ae5f |
241 | |
242 | if ( ref $name ) { |
e0d3eb10 |
243 | $sub = $name; |
e05fb8ae |
244 | |
e05fb8ae |
245 | my $coderef_pkg; |
e6a5040f |
246 | ( $coderef_pkg, $coderef_name ) |
247 | = Class::MOP::get_code_info($name); |
e05fb8ae |
248 | |
e0d3eb10 |
249 | if ( $coderef_pkg ne $package ) { |
8fa582b1 |
250 | $is_reexport->{$coderef_name} = 1; |
e0d3eb10 |
251 | } |
e606ae5f |
252 | } |
253 | else { |
0dd4228e |
254 | $sub = $class->_sub_from_package( $package, $name ) |
255 | or next; |
e6a5040f |
256 | |
e6a5040f |
257 | $coderef_name = $name; |
e606ae5f |
258 | } |
259 | |
260 | $export_recorder->{$sub} = 1; |
261 | |
8fa582b1 |
262 | $exports{$coderef_name} = sub {$sub}; |
e606ae5f |
263 | } |
264 | } |
265 | |
e0d3eb10 |
266 | return \%exports; |
e606ae5f |
267 | } |
268 | |
0dd4228e |
269 | sub _sub_from_package { |
8fa582b1 |
270 | my $sclass = shift; |
0dd4228e |
271 | my $package = shift; |
8fa582b1 |
272 | my $name = shift; |
0dd4228e |
273 | |
274 | my $sub = do { |
275 | no strict 'refs'; |
276 | \&{ $package . '::' . $name }; |
277 | }; |
278 | |
279 | return $sub if defined &$sub; |
280 | |
8fa582b1 |
281 | Carp::cluck "Trying to export undefined sub ${package}::${name}"; |
0dd4228e |
282 | |
283 | return; |
284 | } |
285 | |
96bb13ea |
286 | our $CALLER; |
287 | |
288 | sub _make_wrapped_sub { |
b4f00a34 |
289 | my $self = shift; |
96bb13ea |
290 | my $fq_name = shift; |
291 | my $sub = shift; |
292 | my $export_recorder = shift; |
293 | |
294 | # We need to set the package at import time, so that when |
295 | # package Foo imports has(), we capture "Foo" as the |
296 | # package. This lets other packages call Foo::has() and get |
297 | # the right package. This is done for backwards compatibility |
298 | # with existing production code, not because this is a good |
299 | # idea ;) |
300 | return sub { |
301 | my $caller = $CALLER; |
302 | |
8fa582b1 |
303 | my $wrapper = $self->_curry_wrapper( $sub, $fq_name, $caller ); |
b4f00a34 |
304 | |
8fa582b1 |
305 | my $sub = subname( $fq_name => $wrapper ); |
96bb13ea |
306 | |
307 | $export_recorder->{$sub} = 1; |
308 | |
309 | return $sub; |
310 | }; |
311 | } |
e606ae5f |
312 | |
45975bce |
313 | sub _make_wrapped_sub_with_meta { |
314 | my $self = shift; |
315 | my $fq_name = shift; |
316 | my $sub = shift; |
317 | my $export_recorder = shift; |
450432c8 |
318 | my $meta_lookup = shift; |
45975bce |
319 | |
320 | return sub { |
321 | my $caller = $CALLER; |
322 | |
8fa582b1 |
323 | my $wrapper = $self->_late_curry_wrapper( |
324 | $sub, $fq_name, |
450432c8 |
325 | $meta_lookup => $caller |
8fa582b1 |
326 | ); |
45975bce |
327 | |
8fa582b1 |
328 | my $sub = subname( $fq_name => $wrapper ); |
45975bce |
329 | |
330 | $export_recorder->{$sub} = 1; |
331 | |
332 | return $sub; |
333 | }; |
334 | } |
335 | |
6de00734 |
336 | sub _curry_wrapper { |
badbc528 |
337 | my $class = shift; |
b4f00a34 |
338 | my $sub = shift; |
339 | my $fq_name = shift; |
6de00734 |
340 | my @extra = @_; |
b4f00a34 |
341 | |
8fa582b1 |
342 | my $wrapper = sub { $sub->( @extra, @_ ) }; |
343 | if ( my $proto = prototype $sub ) { |
344 | |
2d7e979b |
345 | # XXX - Perl's prototype sucks. Use & to make set_prototype |
6de00734 |
346 | # ignore the fact that we're passing "private variables" |
8fa582b1 |
347 | &Scalar::Util::set_prototype( $wrapper, $proto ); |
badbc528 |
348 | } |
349 | return $wrapper; |
b4f00a34 |
350 | } |
351 | |
45975bce |
352 | sub _late_curry_wrapper { |
353 | my $class = shift; |
354 | my $sub = shift; |
355 | my $fq_name = shift; |
356 | my $extra = shift; |
357 | my @ex_args = @_; |
358 | |
359 | my $wrapper = sub { |
8fa582b1 |
360 | |
45975bce |
361 | # resolve curried arguments at runtime via this closure |
8fa582b1 |
362 | my @curry = ( $extra->(@ex_args) ); |
363 | return $sub->( @curry, @_ ); |
45975bce |
364 | }; |
365 | |
8fa582b1 |
366 | if ( my $proto = prototype $sub ) { |
367 | |
45975bce |
368 | # XXX - Perl's prototype sucks. Use & to make set_prototype |
369 | # ignore the fact that we're passing "private variables" |
8fa582b1 |
370 | &Scalar::Util::set_prototype( $wrapper, $proto ); |
45975bce |
371 | } |
372 | return $wrapper; |
373 | } |
374 | |
96bb13ea |
375 | sub _make_import_sub { |
376 | shift; |
377 | my $exporting_package = shift; |
378 | my $exporter = shift; |
379 | my $exports_from = shift; |
ce5ccc1a |
380 | my $is_reexport = shift; |
450432c8 |
381 | my $meta_lookup = shift; |
96bb13ea |
382 | |
383 | return sub { |
384 | |
385 | # I think we could use Sub::Exporter's collector feature |
386 | # to do this, but that would be rather gross, since that |
387 | # feature isn't really designed to return a value to the |
388 | # caller of the exporter sub. |
389 | # |
390 | # Also, this makes sure we preserve backwards compat for |
391 | # _get_caller, so it always sees the arguments in the |
392 | # expected order. |
393 | my $traits; |
394 | ( $traits, @_ ) = _strip_traits(@_); |
395 | |
8f30b86e |
396 | my $metaclass; |
397 | ( $metaclass, @_ ) = _strip_metaclass(@_); |
8fa582b1 |
398 | $metaclass |
399 | = Moose::Util::resolve_metaclass_alias( 'Class' => $metaclass ) |
400 | if defined $metaclass && length $metaclass; |
8f30b86e |
401 | |
2937ed18 |
402 | my $meta_name; |
403 | ( $meta_name, @_ ) = _strip_meta_name(@_); |
d65bfd76 |
404 | |
96bb13ea |
405 | # Normally we could look at $_[0], but in some weird cases |
406 | # (involving goto &Moose::import), $_[0] ends as something |
407 | # else (like Squirrel). |
408 | my $class = $exporting_package; |
409 | |
410 | $CALLER = _get_caller(@_); |
411 | |
412 | # this works because both pragmas set $^H (see perldoc |
413 | # perlvar) which affects the current compilation - |
414 | # i.e. the file who use'd us - which is why we don't need |
415 | # to do anything special to make it affect that file |
416 | # rather than this one (which is already compiled) |
417 | |
418 | strict->import; |
419 | warnings->import; |
420 | |
96bb13ea |
421 | my $did_init_meta; |
422 | for my $c ( grep { $_->can('init_meta') } $class, @{$exports_from} ) { |
8fa582b1 |
423 | |
816208bc |
424 | # init_meta can apply a role, which when loaded uses |
425 | # Moose::Exporter, which in turn sets $CALLER, so we need |
426 | # to protect against that. |
fdeb8354 |
427 | local $CALLER = $CALLER; |
d65bfd76 |
428 | $c->init_meta( |
429 | for_class => $CALLER, |
430 | metaclass => $metaclass, |
2937ed18 |
431 | meta_name => $meta_name, |
d65bfd76 |
432 | ); |
96bb13ea |
433 | $did_init_meta = 1; |
434 | } |
e606ae5f |
435 | |
96bb13ea |
436 | if ( $did_init_meta && @{$traits} ) { |
8fa582b1 |
437 | |
96bb13ea |
438 | # The traits will use Moose::Role, which in turn uses |
439 | # Moose::Exporter, which in turn sets $CALLER, so we need |
440 | # to protect against that. |
441 | local $CALLER = $CALLER; |
450432c8 |
442 | _apply_meta_traits( $CALLER, $traits, $meta_lookup ); |
96bb13ea |
443 | } |
444 | elsif ( @{$traits} ) { |
70ea9161 |
445 | require Moose; |
96bb13ea |
446 | Moose->throw_error( |
447 | "Cannot provide traits when $class does not have an init_meta() method" |
448 | ); |
449 | } |
e606ae5f |
450 | |
8fa582b1 |
451 | my ( undef, @args ) = @_; |
452 | my $extra = shift @args if ref $args[0] eq 'HASH'; |
453 | |
454 | $extra ||= {}; |
455 | if ( !$extra->{into} ) { |
456 | $extra->{into_level} ||= 0; |
457 | $extra->{into_level}++; |
458 | } |
459 | |
460 | $class->$exporter( $extra, @args ); |
96bb13ea |
461 | }; |
e606ae5f |
462 | } |
463 | |
464 | sub _strip_traits { |
1c3304e6 |
465 | my $idx = first_index { ( $_ || '' ) eq '-traits' } @_; |
e606ae5f |
466 | |
467 | return ( [], @_ ) unless $idx >= 0 && $#_ >= $idx + 1; |
468 | |
469 | my $traits = $_[ $idx + 1 ]; |
470 | |
471 | splice @_, $idx, 2; |
472 | |
8fa582b1 |
473 | $traits = [$traits] unless ref $traits; |
e606ae5f |
474 | |
475 | return ( $traits, @_ ); |
476 | } |
477 | |
8f30b86e |
478 | sub _strip_metaclass { |
1c3304e6 |
479 | my $idx = first_index { ( $_ || '' ) eq '-metaclass' } @_; |
8f30b86e |
480 | |
481 | return ( undef, @_ ) unless $idx >= 0 && $#_ >= $idx + 1; |
482 | |
483 | my $metaclass = $_[ $idx + 1 ]; |
484 | |
485 | splice @_, $idx, 2; |
486 | |
487 | return ( $metaclass, @_ ); |
488 | } |
489 | |
2937ed18 |
490 | sub _strip_meta_name { |
1c3304e6 |
491 | my $idx = first_index { ( $_ || '' ) eq '-meta_name' } @_; |
d65bfd76 |
492 | |
2937ed18 |
493 | return ( 'meta', @_ ) unless $idx >= 0 && $#_ >= $idx + 1; |
d65bfd76 |
494 | |
2937ed18 |
495 | my $meta_name = $_[ $idx + 1 ]; |
d65bfd76 |
496 | |
497 | splice @_, $idx, 2; |
498 | |
2937ed18 |
499 | return ( $meta_name, @_ ); |
d65bfd76 |
500 | } |
501 | |
e606ae5f |
502 | sub _apply_meta_traits { |
450432c8 |
503 | my ( $class, $traits, $meta_lookup ) = @_; |
e606ae5f |
504 | |
505 | return unless @{$traits}; |
506 | |
450432c8 |
507 | my $meta = $meta_lookup->($class); |
e606ae5f |
508 | |
509 | my $type = ( split /::/, ref $meta )[-1] |
c245d69b |
510 | or Moose->throw_error( |
e606ae5f |
511 | 'Cannot determine metaclass type for trait application . Meta isa ' |
8fa582b1 |
512 | . ref $meta ); |
e606ae5f |
513 | |
8fa582b1 |
514 | my @resolved_traits = map { |
515 | ref $_ |
516 | ? $_ |
517 | : Moose::Util::resolve_metatrait_alias( $type => $_ ) |
518 | } @$traits; |
e606ae5f |
519 | |
520 | return unless @resolved_traits; |
521 | |
f785aad8 |
522 | my %args = ( for => $class ); |
523 | |
524 | if ( $meta->isa('Moose::Meta::Role') ) { |
525 | $args{role_metaroles} = { role => \@resolved_traits }; |
526 | } |
527 | else { |
528 | $args{class_metaroles} = { class => \@resolved_traits }; |
529 | } |
530 | |
531 | Moose::Util::MetaRole::apply_metaroles(%args); |
e606ae5f |
532 | } |
533 | |
534 | sub _get_caller { |
8fa582b1 |
535 | |
e606ae5f |
536 | # 1 extra level because it's called by import so there's a layer |
537 | # of indirection |
538 | my $offset = 1; |
539 | |
540 | return |
541 | ( ref $_[1] && defined $_[1]->{into} ) ? $_[1]->{into} |
542 | : ( ref $_[1] && defined $_[1]->{into_level} ) |
543 | ? caller( $offset + $_[1]->{into_level} ) |
544 | : caller($offset); |
545 | } |
546 | |
547 | sub _make_unimport_sub { |
548 | shift; |
549 | my $exporting_package = shift; |
550 | my $exports = shift; |
551 | my $export_recorder = shift; |
ce5ccc1a |
552 | my $is_reexport = shift; |
450432c8 |
553 | my $meta_lookup = shift; |
e606ae5f |
554 | |
555 | return sub { |
556 | my $caller = scalar caller(); |
557 | Moose::Exporter->_remove_keywords( |
558 | $caller, |
559 | [ keys %{$exports} ], |
560 | $export_recorder, |
8fa582b1 |
561 | $is_reexport, |
e606ae5f |
562 | ); |
563 | }; |
564 | } |
565 | |
566 | sub _remove_keywords { |
567 | shift; |
568 | my $package = shift; |
569 | my $keywords = shift; |
570 | my $recorded_exports = shift; |
ce5ccc1a |
571 | my $is_reexport = shift; |
e606ae5f |
572 | |
573 | no strict 'refs'; |
574 | |
8fa582b1 |
575 | foreach my $name ( @{$keywords} ) { |
e606ae5f |
576 | if ( defined &{ $package . '::' . $name } ) { |
577 | my $sub = \&{ $package . '::' . $name }; |
578 | |
579 | # make sure it is from us |
580 | next unless $recorded_exports->{$sub}; |
581 | |
8fa582b1 |
582 | if ( $is_reexport->{$name} ) { |
583 | no strict 'refs'; |
584 | next |
585 | unless _export_is_flagged( |
586 | \*{ join q{::} => $package, $name } ); |
587 | } |
588 | |
e606ae5f |
589 | # and if it is from us, then undef the slot |
590 | delete ${ $package . '::' }{$name}; |
591 | } |
592 | } |
593 | } |
594 | |
95056a1e |
595 | sub _make_init_meta { |
596 | shift; |
4cfe8f30 |
597 | my $class = shift; |
598 | my $args = shift; |
450432c8 |
599 | my $meta_lookup = shift; |
95056a1e |
600 | |
f785aad8 |
601 | my %old_style_roles; |
906eabcd |
602 | for my $role ( |
603 | map {"${_}_roles"} |
f785aad8 |
604 | qw( |
605 | metaclass |
906eabcd |
606 | attribute_metaclass |
607 | method_metaclass |
608 | wrapped_method_metaclass |
609 | instance_metaclass |
610 | constructor_class |
611 | destructor_class |
612 | error_class |
f785aad8 |
613 | ) |
906eabcd |
614 | ) { |
f785aad8 |
615 | $old_style_roles{$role} = $args->{$role} |
616 | if exists $args->{$role}; |
95056a1e |
617 | } |
618 | |
619 | my %base_class_roles; |
906eabcd |
620 | %base_class_roles = ( roles => $args->{base_class_roles} ) |
95056a1e |
621 | if exists $args->{base_class_roles}; |
622 | |
f785aad8 |
623 | my %new_style_roles = map { $_ => $args->{$_} } |
624 | grep { exists $args->{$_} } qw( class_metaroles role_metaroles ); |
625 | |
626 | return unless %new_style_roles || %old_style_roles || %base_class_roles; |
95056a1e |
627 | |
628 | return sub { |
629 | shift; |
630 | my %options = @_; |
906eabcd |
631 | |
450432c8 |
632 | return unless $meta_lookup->( $options{for_class} ); |
906eabcd |
633 | |
3b400403 |
634 | if ( %new_style_roles || %old_style_roles ) { |
635 | Moose::Util::MetaRole::apply_metaroles( |
636 | for => $options{for_class}, |
637 | %new_style_roles, |
638 | %old_style_roles, |
639 | ); |
640 | } |
906eabcd |
641 | |
95056a1e |
642 | Moose::Util::MetaRole::apply_base_class_roles( |
643 | for_class => $options{for_class}, |
644 | %base_class_roles, |
906eabcd |
645 | ) |
450432c8 |
646 | if $meta_lookup->( $options{for_class} ) |
906eabcd |
647 | ->isa('Moose::Meta::Class'); |
648 | |
450432c8 |
649 | return $meta_lookup->( $options{for_class} ); |
95056a1e |
650 | }; |
651 | } |
652 | |
e2fa092d |
653 | sub import { |
654 | strict->import; |
655 | warnings->import; |
656 | } |
657 | |
e606ae5f |
658 | 1; |
659 | |
ad46f524 |
660 | # ABSTRACT: make an import() and unimport() just like Moose.pm |
e606ae5f |
661 | |
ad46f524 |
662 | __END__ |
e606ae5f |
663 | |
664 | =head1 SYNOPSIS |
665 | |
666 | package MyApp::Moose; |
667 | |
e606ae5f |
668 | use Moose (); |
669 | use Moose::Exporter; |
670 | |
671 | Moose::Exporter->setup_import_methods( |
5ac14e89 |
672 | with_meta => [ 'has_rw', 'sugar2' ], |
673 | as_is => [ 'sugar3', \&Some::Random::thing ], |
674 | also => 'Moose', |
e606ae5f |
675 | ); |
676 | |
82ad7804 |
677 | sub has_rw { |
5ac14e89 |
678 | my ( $meta, $name, %options ) = @_; |
679 | $meta->add_attribute( |
680 | $name, |
82ad7804 |
681 | is => 'rw', |
682 | %options, |
683 | ); |
684 | } |
685 | |
e606ae5f |
686 | # then later ... |
687 | package MyApp::User; |
688 | |
689 | use MyApp::Moose; |
690 | |
691 | has 'name'; |
6daad0b9 |
692 | has_rw 'size'; |
e606ae5f |
693 | thing; |
694 | |
695 | no MyApp::Moose; |
696 | |
697 | =head1 DESCRIPTION |
698 | |
fd7ab111 |
699 | This module encapsulates the exporting of sugar functions in a |
95056a1e |
700 | C<Moose.pm>-like manner. It does this by building custom C<import>, |
37e4fe95 |
701 | C<unimport>, and C<init_meta> methods for your module, based on a spec you |
702 | provide. |
e606ae5f |
703 | |
37e4fe95 |
704 | It also lets you "stack" Moose-alike modules so you can export Moose's sugar |
705 | as well as your own, along with sugar from any random C<MooseX> module, as |
706 | long as they all use C<Moose::Exporter>. This feature exists to let you bundle |
707 | a set of MooseX modules into a policy module that developers can use directly |
708 | instead of using Moose itself. |
e606ae5f |
709 | |
10e0127a |
710 | To simplify writing exporter modules, C<Moose::Exporter> also imports |
711 | C<strict> and C<warnings> into your exporter module, as well as into |
712 | modules that use it. |
713 | |
e606ae5f |
714 | =head1 METHODS |
715 | |
716 | This module provides two public methods: |
717 | |
4b68e0de |
718 | =over 4 |
719 | |
720 | =item B<< Moose::Exporter->setup_import_methods(...) >> |
e606ae5f |
721 | |
95056a1e |
722 | When you call this method, C<Moose::Exporter> builds custom C<import>, |
37e4fe95 |
723 | C<unimport>, and C<init_meta> methods for your module. The C<import> method |
724 | will export the functions you specify, and can also re-export functions |
725 | exported by some other module (like C<Moose.pm>). |
e606ae5f |
726 | |
37e4fe95 |
727 | The C<unimport> method cleans the caller's namespace of all the exported |
cee38bb4 |
728 | functions. This includes any functions you re-export from other |
729 | packages. However, if the consumer of your package also imports those |
730 | functions from the original package, they will I<not> be cleaned. |
e606ae5f |
731 | |
37e4fe95 |
732 | If you pass any parameters for L<Moose::Util::MetaRole>, this method will |
733 | generate an C<init_meta> for you as well (see below for details). This |
ff526a65 |
734 | C<init_meta> will call C<Moose::Util::MetaRole::apply_metaroles> and |
37e4fe95 |
735 | C<Moose::Util::MetaRole::apply_base_class_roles> as needed. |
95056a1e |
736 | |
737 | Note that if any of these methods already exist, they will not be |
738 | overridden, you will have to use C<build_import_methods> to get the |
739 | coderef that would be installed. |
e606ae5f |
740 | |
741 | This method accepts the following parameters: |
742 | |
4b68e0de |
743 | =over 8 |
e606ae5f |
744 | |
5ac14e89 |
745 | =item * with_meta => [ ... ] |
e606ae5f |
746 | |
37e4fe95 |
747 | This list of function I<names only> will be wrapped and then exported. The |
5ac14e89 |
748 | wrapper will pass the metaclass object for the caller as its first argument. |
749 | |
750 | Many sugar functions will need to use this metaclass object to do something to |
751 | the calling package. |
e606ae5f |
752 | |
753 | =item * as_is => [ ... ] |
754 | |
37e4fe95 |
755 | This list of function names or sub references will be exported as-is. You can |
756 | identify a subroutine by reference, which is handy to re-export some other |
757 | module's functions directly by reference (C<\&Some::Package::function>). |
e606ae5f |
758 | |
37e4fe95 |
759 | If you do export some other package's function, this function will never be |
760 | removed by the C<unimport> method. The reason for this is we cannot know if |
761 | the caller I<also> explicitly imported the sub themselves, and therefore wants |
762 | to keep it. |
e05fb8ae |
763 | |
f88cfe7c |
764 | =item * trait_aliases => [ ... ] |
765 | |
bcbb1f7b |
766 | This is a list of package names which should have shortened aliases exported, |
f88cfe7c |
767 | similar to the functionality of L<aliased>. Each element in the list can be |
768 | either a package name, in which case the export will be named as the last |
769 | namespace component of the package, or an arrayref, whose first element is the |
770 | package to alias to, and second element is the alias to export. |
771 | |
e606ae5f |
772 | =item * also => $name or \@names |
773 | |
774 | This is a list of modules which contain functions that the caller |
775 | wants to export. These modules must also use C<Moose::Exporter>. The |
776 | most common use case will be to export the functions from C<Moose.pm>. |
5ac14e89 |
777 | Functions specified by C<with_meta> or C<as_is> take precedence over |
ae8817b6 |
778 | functions exported by modules specified by C<also>, so that a module |
779 | can selectively override functions exported by another module. |
e606ae5f |
780 | |
781 | C<Moose::Exporter> also makes sure all these functions get removed |
782 | when C<unimport> is called. |
783 | |
1365431e |
784 | =item * meta_lookup => sub { ... } |
785 | |
4f45bde8 |
786 | This is a function which will be called to provide the metaclass |
787 | to be operated upon by the exporter. This is an advanced feature |
1365431e |
788 | intended for use by package generator modules in the vein of |
4f45bde8 |
789 | L<MooseX::Role::Parameterized> in order to simplify reusing sugar |
790 | from other modules that use C<Moose::Exporter>. This function is |
791 | used, for example, to select the metaclass to bind to functions |
792 | that are exported using the C<with_meta> option. |
793 | |
794 | This function will receive one parameter: the class name into which |
795 | the sugar is being exported. The default implementation is: |
796 | |
797 | sub { Class::MOP::class_of(shift) } |
798 | |
799 | Accordingly, this function is expected to return a metaclass. |
1365431e |
800 | |
e606ae5f |
801 | =back |
802 | |
f785aad8 |
803 | You can also provide parameters for C<Moose::Util::MetaRole::apply_metaroles> |
804 | and C<Moose::Util::MetaRole::base_class_roles>. Specifically, valid parameters |
15851b87 |
805 | are "class_metaroles", "role_metaroles", and "base_class_roles". |
95056a1e |
806 | |
4b68e0de |
807 | =item B<< Moose::Exporter->build_import_methods(...) >> |
e606ae5f |
808 | |
95056a1e |
809 | Returns two or three code refs, one for C<import>, one for |
810 | C<unimport>, and optionally one for C<init_meta>, if the appropriate |
811 | options are passed in. |
812 | |
37e4fe95 |
813 | Accepts the additional C<install> option, which accepts an arrayref of method |
814 | names to install into your exporting package. The valid options are C<import>, |
815 | C<unimport>, and C<init_meta>. Calling C<setup_import_methods> is equivalent |
816 | to calling C<build_import_methods> with C<< install => [qw(import unimport |
817 | init_meta)] >> except that it doesn't also return the methods. |
e606ae5f |
818 | |
4df96f52 |
819 | The C<import> method is built using L<Sub::Exporter>. This means that it can |
820 | take a hashref of the form C<< { into => $package } >> to specify the package |
b360ed32 |
821 | it operates on. |
822 | |
e606ae5f |
823 | Used by C<setup_import_methods>. |
824 | |
4b68e0de |
825 | =back |
826 | |
e606ae5f |
827 | =head1 IMPORTING AND init_meta |
828 | |
37e4fe95 |
829 | If you want to set an alternative base object class or metaclass class, see |
830 | above for details on how this module can call L<Moose::Util::MetaRole> for |
831 | you. |
832 | |
833 | If you want to do something that is not supported by this module, simply |
834 | define an C<init_meta> method in your class. The C<import> method that |
835 | C<Moose::Exporter> generates for you will call this method (if it exists). It |
836 | will always pass the caller to this method via the C<for_class> parameter. |
e606ae5f |
837 | |
838 | Most of the time, your C<init_meta> method will probably just call C<< |
839 | Moose->init_meta >> to do the real work: |
840 | |
841 | sub init_meta { |
842 | shift; # our class name |
843 | return Moose->init_meta( @_, metaclass => 'My::Metaclass' ); |
844 | } |
845 | |
95056a1e |
846 | Keep in mind that C<build_import_methods> will return an C<init_meta> |
847 | method for you, which you can also call from within your custom |
37e4fe95 |
848 | C<init_meta>: |
849 | |
b183ca44 |
850 | my ( $import, $unimport, $init_meta ) |
851 | = Moose::Exporter->build_import_methods(...); |
37e4fe95 |
852 | |
853 | sub import { |
b183ca44 |
854 | my $class = shift; |
37e4fe95 |
855 | |
b183ca44 |
856 | ... |
37e4fe95 |
857 | |
b183ca44 |
858 | # You can either pass an explicit package to import into ... |
859 | $class->$import( { into => scalar(caller) }, ... ); |
37e4fe95 |
860 | |
b183ca44 |
861 | ...; |
37e4fe95 |
862 | } |
863 | |
4df96f52 |
864 | # ... or you can use 'goto' to provide the correct caller info to the |
865 | # generated method |
37e4fe95 |
866 | sub unimport { goto &$unimport } |
867 | |
868 | sub init_meta { |
b183ca44 |
869 | my $class = shift; |
37e4fe95 |
870 | |
b183ca44 |
871 | ... |
37e4fe95 |
872 | |
b183ca44 |
873 | $class->$init_meta(...); |
37e4fe95 |
874 | |
b183ca44 |
875 | ... |
37e4fe95 |
876 | } |
95056a1e |
877 | |
e606ae5f |
878 | =head1 METACLASS TRAITS |
879 | |
880 | The C<import> method generated by C<Moose::Exporter> will allow the |
881 | user of your module to specify metaclass traits in a C<-traits> |
882 | parameter passed as part of the import: |
883 | |
884 | use Moose -traits => 'My::Meta::Trait'; |
885 | |
886 | use Moose -traits => [ 'My::Meta::Trait', 'My::Other::Trait' ]; |
887 | |
888 | These traits will be applied to the caller's metaclass |
889 | instance. Providing traits for an exporting class that does not create |
890 | a metaclass for the caller is an error. |
891 | |
c5fc2c21 |
892 | =head1 BUGS |
893 | |
894 | See L<Moose/BUGS> for details on reporting bugs. |
895 | |
e606ae5f |
896 | =cut |