1 package Moose::Exporter;
7 use List::MoreUtils qw( uniq );
13 sub build_import_methods {
17 my $exporting_package = caller();
19 $EXPORT_SPEC{$exporting_package} = \%args;
21 my @exports_from = $class->_follow_also( $exporting_package );
24 = $class->_process_exports( $exporting_package, @exports_from );
26 my $exporter = Sub::Exporter::build_exporter(
29 groups => { default => [':all'] }
33 # $args{_export_to_main} exists for backwards compat, because
34 # Moose::Util::TypeConstraints did export to main (unlike Moose &
36 my $import = $class->_make_import_sub( $exporter, \@exports_from, $args{_export_to_main} );
38 my $unimport = $class->_make_unimport_sub( \@exports_from, [ keys %{$exports} ] );
41 *{ $exporting_package . '::import' } = $import;
42 *{ $exporting_package . '::unimport' } = $unimport;
50 my $exporting_package = shift;
52 %seen = ( $exporting_package => 1 );
54 return uniq( _follow_also_real($exporting_package) );
57 sub _follow_also_real {
58 my $exporting_package = shift;
60 die "Package in also ($exporting_package) does not seem to use MooseX::Exporter"
61 unless exists $EXPORT_SPEC{$exporting_package};
63 my $also = $EXPORT_SPEC{$exporting_package}{also};
65 return unless defined $also;
67 my @also = ref $also ? @{$also} : $also;
69 for my $package (@also)
71 die "Circular reference in also parameter to MooseX::Exporter between $exporting_package and $package"
77 return @also, map { _follow_also_real($_) } @also;
81 sub _process_exports {
87 for my $package (@packages) {
88 my $args = $EXPORT_SPEC{$package}
89 or die "The $package package does not use Moose::Exporter\n";
91 for my $name ( @{ $args->{with_caller} } ) {
94 \&{ $package . '::' . $name };
97 $exports{$name} = $class->_make_wrapped_sub(
104 for my $name ( @{ $args->{as_is} } ) {
109 $name = ( Class::MOP::get_code_info($name) )[1];
114 \&{ $package . '::' . $name };
118 $exports{$name} = sub {$sub};
126 # This variable gets closed over in each export _generator_. Then
127 # in the generator we grab the value and close over it _again_ in
128 # the real export, so it gets captured each time the generator
131 # In the meantime, we arrange for the import method we generate to
132 # set this variable to the caller each time it is called.
134 # This is all a bit confusing, but it works.
137 sub _make_wrapped_sub {
139 my $exporting_package = shift;
143 # We need to set the package at import time, so that when
144 # package Foo imports has(), we capture "Foo" as the
145 # package. This lets other packages call Foo::has() and get
146 # the right package. This is done for backwards compatibility
147 # with existing production code, not because this is a good
150 my $caller = $CALLER;
151 Class::MOP::subname( $exporting_package . '::'
152 . $name => sub { $sub->( $caller, @_ ) } );
156 sub _make_import_sub {
158 my $exporter = shift;
159 my $exports_from = shift;
160 my $export_to_main = shift;
164 # It's important to leave @_ as-is for the benefit of
168 $CALLER = Moose::Exporter::_get_caller(@_);
170 # this works because both pragmas set $^H (see perldoc
171 # perlvar) which affects the current compilation -
172 # i.e. the file who use'd us - which is why we don't need
173 # to do anything special to make it affect that file
174 # rather than this one (which is already compiled)
179 # we should never export to main
180 if ( $CALLER eq 'main' && ! $export_to_main ) {
182 qq{$class does not export its sugar to the 'main' package.\n};
186 for my $c (grep { $_->can('init_meta') } $class, @{$exports_from} ) {
188 $c->init_meta( for_class => $CALLER );
197 # 1 extra level because it's called by import so there's a layer
202 ( ref $_[1] && defined $_[1]->{into} ) ? $_[1]->{into}
203 : ( ref $_[1] && defined $_[1]->{into_level} )
204 ? caller( $offset + $_[1]->{into_level} )
208 sub _make_unimport_sub {
211 my $keywords = shift;
215 my $caller = scalar caller();
216 Moose::Exporter->_remove_keywords(
218 [ $class, @{$sources} ],
224 sub _remove_keywords {
228 my $keywords = shift;
230 my %sources = map { $_ => 1 } @{$sources};
234 # loop through the keywords ...
235 foreach my $name ( @{$keywords} ) {
238 if ( defined &{ $package . '::' . $name } ) {
239 my $keyword = \&{ $package . '::' . $name };
241 # make sure it is from us
242 my ($pkg_name) = Class::MOP::get_code_info($keyword);
243 next unless $sources{$pkg_name};
245 # and if it is from us, then undef the slot
246 delete ${ $package . '::' }{$name};
257 Moose::Exporter - make an import() and unimport() just like Moose.pm
261 package MyApp::Moose;
269 Moose::Exporter->build_export_methods(
270 export => [ 'sugar1', 'sugar2', \&Some::Random::thing ],
271 init_meta_args => { metaclass_class => 'MyApp::Meta::Class' ],
280 sugar1 'do your thing';
287 This module encapsulates the logic to export sugar functions like
288 C<Moose.pm>. It does this by building custom C<import> and C<unimport>
289 methods for your module, based on a spec your provide.
291 It also lets your "stack" Moose-alike modules so you can export
292 Moose's sugar as well as your own, along with sugar from any random
293 C<MooseX> module, as long as they all use C<Moose::Exporter>.
297 This module provides exactly one public method:
299 =head2 Moose::Exporter->build_import_methods(...)
301 When you call this method, C<Moose::Exporter> build custom C<import>
302 and C<unimport> methods for your module. The import method will export
303 the functions you specify, and you can also tell it to export
304 functions exported by some other module (like C<Moose.pm>).
306 The C<unimport> method cleans the callers namespace of all the
309 This method accepts the following parameters:
313 =item * with_caller => [ ... ]
315 This a list of function I<names only> to be exported wrapped and then
316 exported. The wrapper will pass the name of the calling package as the
317 first argument to the function. Many sugar functions need to know
318 their caller so they can get the calling package's metaclass object.
320 =item * as_is => [ ... ]
322 This a list of function names or sub references to be exported
323 as-is. You can identify a subroutine by reference, which is handy to
324 re-export some other module's functions directly by reference
325 (C<\&Some::Package::function>).
327 =item * also => $name or \@names
329 This is a list of modules which contain functions that the caller
330 wants to export. These modules must also use C<Moose::Exporter>. The
331 most common use case will be to export the functions from C<Moose.pm>.
333 C<Moose::Exporter> also makes sure all these functions get removed
334 when C<unimport> is called.
338 =head1 IMPORTING AND init_meta
340 If you want to set an alternative base object class or metaclass
341 class, simply define an C<init_meta> method in your class. The
342 C<import> method that C<Moose::Exporter> generates for you will call
343 this method (if it exists). It will always pass the caller to this
344 method via the C<for_class> parameter.
346 Most of the time, your C<init_meta> method will probably just call C<<
347 Moose->init_meta >> to do the real work:
350 shift; # our class name
351 return Moose->init_meta( @_, metaclass => 'My::Metaclass' );
356 Dave Rolsky E<lt>autarch@urth.orgE<gt>
358 This is largely a reworking of code in Moose.pm originally written by
359 Stevan Little and others.
361 =head1 COPYRIGHT AND LICENSE
363 Copyright 2008 by Infinity Interactive, Inc.
365 L<http://www.iinteractive.com>
367 This library is free software; you can redistribute it and/or modify
368 it under the same terms as Perl itself.