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