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