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