1 package Moose::Exporter;
7 use namespace::clean 0.08 ();
8 use List::MoreUtils qw( uniq );
14 sub build_import_methods {
18 my $exporting_package = caller();
20 $EXPORT_SPEC{$exporting_package} = \%args;
22 my @exports_from = $class->_follow_also( $exporting_package );
25 = $class->_process_exports( $exporting_package, @exports_from );
27 my $exporter = Sub::Exporter::build_exporter(
30 groups => { default => [':all'] }
34 my $import = $class->_make_import_sub($exporter);
36 my $unimport = $class->_make_unimport_sub( [ keys %{$exports} ] );
39 *{ $exporting_package . '::import' } = $import;
40 *{ $exporting_package . '::unimport' } = $unimport;
48 my $exporting_package = shift;
50 %seen = ( $exporting_package => 1 );
52 return uniq( _follow_also_real($exporting_package) );
55 sub _follow_also_real {
56 my $exporting_package = shift;
58 die "Package in also ($exporting_package) does not seem to use MooseX::Exporter"
59 unless exists $EXPORT_SPEC{$exporting_package};
61 my $also = $EXPORT_SPEC{$exporting_package}{also};
63 return unless defined $also;
65 my @also = ref $also ? @{$also} : $also;
67 for my $package (@also)
69 die "Circular reference in also parameter to MooseX::Exporter between $exporting_package and $package"
75 return @also, map { _follow_also_real($_) } @also;
79 sub _process_exports {
85 for my $package (@packages) {
86 my $args = $EXPORT_SPEC{$package}
87 or die "The $package package does not use Moose::Exporter\n";
89 for my $name ( @{ $args->{with_caller} } ) {
92 \&{ $package . '::' . $name };
95 $exports{$name} = $class->_make_wrapped_sub(
102 for my $name ( @{ $args->{as_is} } ) {
107 $name = ( Class::MOP::get_code_info($name) )[1];
112 \&{ $package . '::' . $name };
116 $exports{$name} = sub {$sub};
124 # This variable gets closed over in each export _generator_. Then
125 # in the generator we grab the value and close over it _again_ in
126 # the real export, so it gets captured each time the generator
129 # In the meantime, we arrange for the import method we generate to
130 # set this variable to the caller each time it is called.
132 # This is all a bit confusing, but it works.
135 sub _make_wrapped_sub {
137 my $exporting_package = shift;
141 # We need to set the package at import time, so that when
142 # package Foo imports has(), we capture "Foo" as the
143 # package. This lets other packages call Foo::has() and get
144 # the right package. This is done for backwards compatibility
145 # with existing production code, not because this is a good
148 my $caller = $CALLER;
149 Class::MOP::subname( $exporting_package . '::'
150 . $name => sub { $sub->( $caller, @_ ) } );
154 sub _make_import_sub {
156 my $exporter = shift;
160 # It's important to leave @_ as-is for the benefit of
164 $CALLER = Moose::Exporter::_get_caller(@_);
166 # this works because both pragmas set $^H (see perldoc
167 # perlvar) which affects the current compilation -
168 # i.e. the file who use'd us - which is why we don't need
169 # to do anything special to make it affect that file
170 # rather than this one (which is already compiled)
175 # we should never export to main
176 if ( $CALLER eq 'main' ) {
178 qq{$class does not export its sugar to the 'main' package.\n};
182 if ( $class->can('init_meta') ) {
184 for_class => $CALLER,
194 # 1 extra level because it's called by import so there's a layer
199 ( ref $_[1] && defined $_[1]->{into} ) ? $_[1]->{into}
200 : ( ref $_[1] && defined $_[1]->{into_level} )
201 ? caller( $offset + $_[1]->{into_level} )
205 sub _make_unimport_sub {
207 my $exported = shift;
209 # [12:24] <mst> yes. that's horrible. I know. but it should work.
211 # This will hopefully be replaced in the future once
212 # namespace::clean has an API for it.
214 @_ = ( 'namespace::clean', @{$exported} );
216 goto &namespace::clean::import;
226 Moose::Exporter - make an import() and unimport() just like Moose.pm
230 package MyApp::Moose;
238 Moose::Exporter->build_export_methods(
239 export => [ 'sugar1', 'sugar2', \&Some::Random::thing ],
240 init_meta_args => { metaclass_class => 'MyApp::Meta::Class' ],
249 sugar1 'do your thing';
256 This module encapsulates the logic to export sugar functions like
257 C<Moose.pm>. It does this by building custom C<import> and C<unimport>
258 methods for your module, based on a spec your provide.
260 It also lets your "stack" Moose-alike modules so you can export
261 Moose's sugar as well as your own, along with sugar from any random
262 C<MooseX> module, as long as they all use C<Moose::Exporter>.
266 This module provides exactly one public method:
268 =head2 Moose::Exporter->build_import_methods(...)
270 When you call this method, C<Moose::Exporter> build custom C<import>
271 and C<unimport> methods for your module. The import method will export
272 the functions you specify, and you can also tell it to export
273 functions exported by some other module (like C<Moose.pm>).
275 The C<unimport> method cleans the callers namespace of all the
278 This method accepts the following parameters:
282 =item * with_caller => [ ... ]
284 This a list of function I<names only> to be exported wrapped and then
285 exported. The wrapper will pass the name of the calling package as the
286 first argument to the function. Many sugar functions need to know
287 their caller so they can get the calling package's metaclass object.
289 =item * as_is => [ ... ]
291 This a list of function names or sub references to be exported
292 as-is. You can identify a subroutine by reference, which is handy to
293 re-export some other module's functions directly by reference
294 (C<\&Some::Package::function>).
296 =item * init_meta_args
304 Dave Rolsky E<lt>autarch@urth.orgE<gt>
306 This is largely a reworking of code in Moose.pm originally written by
307 Stevan Little and others.
309 =head1 COPYRIGHT AND LICENSE
311 Copyright 2008 by Infinity Interactive, Inc.
313 L<http://www.iinteractive.com>
315 This library is free software; you can redistribute it and/or modify
316 it under the same terms as Perl itself.