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