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