1 package Moose::Exporter;
7 our $XS_VERSION = $VERSION;
8 $VERSION = eval $VERSION;
9 our $AUTHORITY = 'cpan:STEVAN';
12 use List::MoreUtils qw( first_index uniq );
13 use Moose::Util::MetaRole;
14 use Sub::Exporter 0.980;
15 use Sub::Name qw(subname);
19 XSLoader::load( 'Moose', $XS_VERSION );
23 sub setup_import_methods {
24 my ( $class, %args ) = @_;
26 my $exporting_package = $args{exporting_package} ||= caller();
28 $class->build_import_methods(
30 install => [qw(import unimport init_meta)]
34 sub build_import_methods {
35 my ( $class, %args ) = @_;
37 my $exporting_package = $args{exporting_package} ||= caller();
39 $EXPORT_SPEC{$exporting_package} = \%args;
41 my @exports_from = $class->_follow_also($exporting_package);
43 my $export_recorder = {};
46 my $exports = $class->_make_sub_exporter_params(
47 [ @exports_from, $exporting_package ],
52 my $exporter = Sub::Exporter::build_exporter(
55 groups => { default => [':all'] }
60 $methods{import} = $class->_make_import_sub(
65 ($args{init_meta_params} || []),
68 $methods{unimport} = $class->_make_unimport_sub(
75 $methods{init_meta} = $class->_make_init_meta(
80 my $package = Class::MOP::Package->initialize($exporting_package);
81 for my $to_install ( @{ $args{install} || [] } ) {
82 my $symbol = '&' . $to_install;
84 unless $methods{$to_install}
85 && !$package->has_package_symbol($symbol);
86 $package->add_package_symbol( $symbol, $methods{$to_install} );
89 return ( $methods{import}, $methods{unimport}, $methods{init_meta} );
97 my $exporting_package = shift;
99 local %$seen = ( $exporting_package => 1 );
101 return uniq( _follow_also_real($exporting_package) );
104 sub _follow_also_real {
105 my $exporting_package = shift;
107 if ( !exists $EXPORT_SPEC{$exporting_package} ) {
108 my $loaded = Class::MOP::is_class_loaded($exporting_package);
110 die "Package in also ($exporting_package) does not seem to "
111 . "use Moose::Exporter"
112 . ( $loaded ? "" : " (is it loaded?)" );
115 my $also = $EXPORT_SPEC{$exporting_package}{also};
117 return unless defined $also;
119 my @also = ref $also ? @{$also} : $also;
121 for my $package (@also) {
123 "Circular reference in 'also' parameter to Moose::Exporter between $exporting_package and $package"
124 if $seen->{$package};
126 $seen->{$package} = 1;
129 return @also, map { _follow_also_real($_) } @also;
133 sub _make_sub_exporter_params {
135 my $packages = shift;
136 my $export_recorder = shift;
137 my $is_reexport = shift;
141 for my $package ( @{$packages} ) {
142 my $args = $EXPORT_SPEC{$package}
143 or die "The $package package does not use Moose::Exporter\n";
145 for my $name ( @{ $args->{with_meta} } ) {
146 my $sub = $class->_sub_from_package( $package, $name )
149 my $fq_name = $package . '::' . $name;
151 $exports{$name} = $class->_make_wrapped_sub_with_meta(
158 for my $name ( @{ $args->{with_caller} } ) {
159 my $sub = $class->_sub_from_package( $package, $name )
162 my $fq_name = $package . '::' . $name;
164 $exports{$name} = $class->_make_wrapped_sub(
171 for my $name ( @{ $args->{as_is} } ) {
172 my ( $sub, $coderef_name );
178 ( $coderef_pkg, $coderef_name )
179 = Class::MOP::get_code_info($name);
181 if ( $coderef_pkg ne $package ) {
182 $is_reexport->{$coderef_name} = 1;
186 $sub = $class->_sub_from_package( $package, $name )
189 $coderef_name = $name;
192 $export_recorder->{$sub} = 1;
194 $exports{$coderef_name} = sub {$sub};
201 sub _sub_from_package {
208 \&{ $package . '::' . $name };
211 return $sub if defined &$sub;
213 Carp::cluck "Trying to export undefined sub ${package}::${name}";
220 sub _make_wrapped_sub {
224 my $export_recorder = shift;
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
233 my $caller = $CALLER;
235 my $wrapper = $self->_curry_wrapper( $sub, $fq_name, $caller );
237 my $sub = subname( $fq_name => $wrapper );
239 $export_recorder->{$sub} = 1;
245 sub _make_wrapped_sub_with_meta {
249 my $export_recorder = shift;
252 my $caller = $CALLER;
254 my $wrapper = $self->_late_curry_wrapper(
256 sub { Class::MOP::class_of(shift) } => $caller
259 my $sub = subname( $fq_name => $wrapper );
261 $export_recorder->{$sub} = 1;
273 my $wrapper = sub { $sub->( @extra, @_ ) };
274 if ( my $proto = prototype $sub ) {
276 # XXX - Perl's prototype sucks. Use & to make set_prototype
277 # ignore the fact that we're passing "private variables"
278 &Scalar::Util::set_prototype( $wrapper, $proto );
283 sub _late_curry_wrapper {
292 # resolve curried arguments at runtime via this closure
293 my @curry = ( $extra->(@ex_args) );
294 return $sub->( @curry, @_ );
297 if ( my $proto = prototype $sub ) {
299 # XXX - Perl's prototype sucks. Use & to make set_prototype
300 # ignore the fact that we're passing "private variables"
301 &Scalar::Util::set_prototype( $wrapper, $proto );
306 sub _make_import_sub {
308 my $exporting_package = shift;
309 my $exporter = shift;
310 my $exports_from = shift;
311 my $is_reexport = shift;
312 my $init_meta_params = shift;
316 # I think we could use Sub::Exporter's collector feature
317 # to do this, but that would be rather gross, since that
318 # feature isn't really designed to return a value to the
319 # caller of the exporter sub.
321 # Also, this makes sure we preserve backwards compat for
322 # _get_caller, so it always sees the arguments in the
325 ( $traits, @_ ) = _strip_traits(@_);
328 ( $metaclass, @_ ) = _strip_metaclass(@_);
330 = Moose::Util::resolve_metaclass_alias( 'Class' => $metaclass )
331 if defined $metaclass && length $metaclass;
333 # Normally we could look at $_[0], but in some weird cases
334 # (involving goto &Moose::import), $_[0] ends as something
335 # else (like Squirrel).
336 my $class = $exporting_package;
338 $CALLER = _get_caller(@_);
340 # this works because both pragmas set $^H (see perldoc
341 # perlvar) which affects the current compilation -
342 # i.e. the file who use'd us - which is why we don't need
343 # to do anything special to make it affect that file
344 # rather than this one (which is already compiled)
350 # don't want to just force @_ into a hash, since it really actually is
353 if (grep { $_ eq $_[$i] } @$init_meta_params) {
354 $extra_args{$_[$i]} = $_[$i + 1];
360 for my $c ( grep { $_->can('init_meta') } $class, @{$exports_from} ) {
362 # init_meta can apply a role, which when loaded uses
363 # Moose::Exporter, which in turn sets $CALLER, so we need
364 # to protect against that.
365 local $CALLER = $CALLER;
368 for_class => $CALLER,
369 metaclass => $metaclass,
374 if ( $did_init_meta && @{$traits} ) {
376 # The traits will use Moose::Role, which in turn uses
377 # Moose::Exporter, which in turn sets $CALLER, so we need
378 # to protect against that.
379 local $CALLER = $CALLER;
380 _apply_meta_traits( $CALLER, $traits );
382 elsif ( @{$traits} ) {
385 "Cannot provide traits when $class does not have an init_meta() method"
389 my ( undef, @args ) = @_;
390 my $extra = shift @args if ref $args[0] eq 'HASH';
393 if ( !$extra->{into} ) {
394 $extra->{into_level} ||= 0;
395 $extra->{into_level}++;
398 $class->$exporter( $extra, @args );
400 for my $name ( keys %{$is_reexport} ) {
403 _flag_as_reexport( \*{ join q{::}, $CALLER, $name } );
409 my $idx = first_index { $_ eq '-traits' } @_;
411 return ( [], @_ ) unless $idx >= 0 && $#_ >= $idx + 1;
413 my $traits = $_[ $idx + 1 ];
417 $traits = [$traits] unless ref $traits;
419 return ( $traits, @_ );
422 sub _strip_metaclass {
423 my $idx = first_index { $_ eq '-metaclass' } @_;
425 return ( undef, @_ ) unless $idx >= 0 && $#_ >= $idx + 1;
427 my $metaclass = $_[ $idx + 1 ];
431 return ( $metaclass, @_ );
434 sub _apply_meta_traits {
435 my ( $class, $traits ) = @_;
437 return unless @{$traits};
439 my $meta = Class::MOP::class_of($class);
441 my $type = ( split /::/, ref $meta )[-1]
442 or Moose->throw_error(
443 'Cannot determine metaclass type for trait application . Meta isa '
446 my @resolved_traits = map {
449 : Moose::Util::resolve_metatrait_alias( $type => $_ )
452 return unless @resolved_traits;
454 my %args = ( for => $class );
456 if ( $meta->isa('Moose::Meta::Role') ) {
457 $args{role_metaroles} = { role => \@resolved_traits };
460 $args{class_metaroles} = { class => \@resolved_traits };
463 Moose::Util::MetaRole::apply_metaroles(%args);
468 # 1 extra level because it's called by import so there's a layer
473 ( ref $_[1] && defined $_[1]->{into} ) ? $_[1]->{into}
474 : ( ref $_[1] && defined $_[1]->{into_level} )
475 ? caller( $offset + $_[1]->{into_level} )
479 sub _make_unimport_sub {
481 my $exporting_package = shift;
483 my $export_recorder = shift;
484 my $is_reexport = shift;
487 my $caller = scalar caller();
488 Moose::Exporter->_remove_keywords(
490 [ keys %{$exports} ],
497 sub _remove_keywords {
500 my $keywords = shift;
501 my $recorded_exports = shift;
502 my $is_reexport = shift;
506 foreach my $name ( @{$keywords} ) {
507 if ( defined &{ $package . '::' . $name } ) {
508 my $sub = \&{ $package . '::' . $name };
510 # make sure it is from us
511 next unless $recorded_exports->{$sub};
513 if ( $is_reexport->{$name} ) {
516 unless _export_is_flagged(
517 \*{ join q{::} => $package, $name } );
520 # and if it is from us, then undef the slot
521 delete ${ $package . '::' }{$name};
526 sub _make_init_meta {
538 wrapped_method_metaclass
545 $old_style_roles{$role} = $args->{$role}
546 if exists $args->{$role};
549 my %base_class_roles;
550 %base_class_roles = ( roles => $args->{base_class_roles} )
551 if exists $args->{base_class_roles};
553 my %new_style_roles = map { $_ => $args->{$_} }
554 grep { exists $args->{$_} } qw( class_metaroles role_metaroles );
556 return unless %new_style_roles || %old_style_roles || %base_class_roles;
562 return unless Class::MOP::class_of( $options{for_class} );
564 Moose::Util::MetaRole::apply_metaroles(
565 for => $options{for_class},
570 Moose::Util::MetaRole::apply_base_class_roles(
571 for_class => $options{for_class},
574 if Class::MOP::class_of( $options{for_class} )
575 ->isa('Moose::Meta::Class');
577 return Class::MOP::class_of( $options{for_class} );
592 Moose::Exporter - make an import() and unimport() just like Moose.pm
596 package MyApp::Moose;
601 Moose::Exporter->setup_import_methods(
602 with_meta => [ 'has_rw', 'sugar2' ],
603 as_is => [ 'sugar3', \&Some::Random::thing ],
608 my ( $meta, $name, %options ) = @_;
609 $meta->add_attribute(
629 This module encapsulates the exporting of sugar functions in a
630 C<Moose.pm>-like manner. It does this by building custom C<import>,
631 C<unimport>, and C<init_meta> methods for your module, based on a spec you
634 It also lets you "stack" Moose-alike modules so you can export Moose's sugar
635 as well as your own, along with sugar from any random C<MooseX> module, as
636 long as they all use C<Moose::Exporter>. This feature exists to let you bundle
637 a set of MooseX modules into a policy module that developers can use directly
638 instead of using Moose itself.
640 To simplify writing exporter modules, C<Moose::Exporter> also imports
641 C<strict> and C<warnings> into your exporter module, as well as into
646 This module provides two public methods:
650 =item B<< Moose::Exporter->setup_import_methods(...) >>
652 When you call this method, C<Moose::Exporter> builds custom C<import>,
653 C<unimport>, and C<init_meta> methods for your module. The C<import> method
654 will export the functions you specify, and can also re-export functions
655 exported by some other module (like C<Moose.pm>).
657 The C<unimport> method cleans the caller's namespace of all the exported
658 functions. This includes any functions you re-export from other
659 packages. However, if the consumer of your package also imports those
660 functions from the original package, they will I<not> be cleaned.
662 If you pass any parameters for L<Moose::Util::MetaRole>, this method will
663 generate an C<init_meta> for you as well (see below for details). This
664 C<init_meta> will call C<Moose::Util::MetaRole::apply_metaclass_roles> and
665 C<Moose::Util::MetaRole::apply_base_class_roles> as needed.
667 Note that if any of these methods already exist, they will not be
668 overridden, you will have to use C<build_import_methods> to get the
669 coderef that would be installed.
671 This method accepts the following parameters:
675 =item * with_meta => [ ... ]
677 This list of function I<names only> will be wrapped and then exported. The
678 wrapper will pass the metaclass object for the caller as its first argument.
680 Many sugar functions will need to use this metaclass object to do something to
683 =item * as_is => [ ... ]
685 This list of function names or sub references will be exported as-is. You can
686 identify a subroutine by reference, which is handy to re-export some other
687 module's functions directly by reference (C<\&Some::Package::function>).
689 If you do export some other package's function, this function will never be
690 removed by the C<unimport> method. The reason for this is we cannot know if
691 the caller I<also> explicitly imported the sub themselves, and therefore wants
694 =item * also => $name or \@names
696 This is a list of modules which contain functions that the caller
697 wants to export. These modules must also use C<Moose::Exporter>. The
698 most common use case will be to export the functions from C<Moose.pm>.
699 Functions specified by C<with_meta> or C<as_is> take precedence over
700 functions exported by modules specified by C<also>, so that a module
701 can selectively override functions exported by another module.
703 C<Moose::Exporter> also makes sure all these functions get removed
704 when C<unimport> is called.
708 You can also provide parameters for C<Moose::Util::MetaRole::apply_metaroles>
709 and C<Moose::Util::MetaRole::base_class_roles>. Specifically, valid parameters
710 are "class_metaroles", "role_metaroles", and "base_object_roles".
712 =item B<< Moose::Exporter->build_import_methods(...) >>
714 Returns two or three code refs, one for C<import>, one for
715 C<unimport>, and optionally one for C<init_meta>, if the appropriate
716 options are passed in.
718 Accepts the additional C<install> option, which accepts an arrayref of method
719 names to install into your exporting package. The valid options are C<import>,
720 C<unimport>, and C<init_meta>. Calling C<setup_import_methods> is equivalent
721 to calling C<build_import_methods> with C<< install => [qw(import unimport
722 init_meta)] >> except that it doesn't also return the methods.
724 Used by C<setup_import_methods>.
728 =head1 IMPORTING AND init_meta
730 If you want to set an alternative base object class or metaclass class, see
731 above for details on how this module can call L<Moose::Util::MetaRole> for
734 If you want to do something that is not supported by this module, simply
735 define an C<init_meta> method in your class. The C<import> method that
736 C<Moose::Exporter> generates for you will call this method (if it exists). It
737 will always pass the caller to this method via the C<for_class> parameter.
739 Most of the time, your C<init_meta> method will probably just call C<<
740 Moose->init_meta >> to do the real work:
743 shift; # our class name
744 return Moose->init_meta( @_, metaclass => 'My::Metaclass' );
747 Keep in mind that C<build_import_methods> will return an C<init_meta>
748 method for you, which you can also call from within your custom
751 my ( $import, $unimport, $init_meta ) =
752 Moose::Exporter->build_import_methods( ... );
759 $class->$import(...);
764 sub unimport { goto &$unimport }
771 $class->$init_meta(...);
776 =head1 METACLASS TRAITS
778 The C<import> method generated by C<Moose::Exporter> will allow the
779 user of your module to specify metaclass traits in a C<-traits>
780 parameter passed as part of the import:
782 use Moose -traits => 'My::Meta::Trait';
784 use Moose -traits => [ 'My::Meta::Trait', 'My::Other::Trait' ];
786 These traits will be applied to the caller's metaclass
787 instance. Providing traits for an exporting class that does not create
788 a metaclass for the caller is an error.
792 See L<Moose/BUGS> for details on reporting bugs.
796 Dave Rolsky E<lt>autarch@urth.orgE<gt>
798 This is largely a reworking of code in Moose.pm originally written by
799 Stevan Little and others.
801 =head1 COPYRIGHT AND LICENSE
803 Copyright 2009 by Infinity Interactive, Inc.
805 L<http://www.iinteractive.com>
807 This library is free software; you can redistribute it and/or modify
808 it under the same terms as Perl itself.