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