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