1 package Moose::Exporter;
7 use namespace::clean 0.08 ();
13 sub build_import_methods {
17 my $exporting_package = caller();
19 $EXPORT_SPEC{$exporting_package} = \%args;
21 my $exports = $class->_process_exports(
22 exporting_package => $exporting_package,
26 my $exporter = Sub::Exporter::build_exporter(
29 groups => { default => [':all'] }
33 my $import = $class->_make_import_sub(
35 $args{init_meta_args},
38 my $unimport = $class->_make_unimport_sub( [ keys %{$exports} ] );
41 *{ $exporting_package . '::import' } = $import;
42 *{ $exporting_package . '::unimport' } = $unimport;
45 sub _process_exports {
49 my $exporting_package = $args{exporting_package};
52 for my $name ( @{ $args{with_caller} } ) {
54 = do { no strict 'refs'; \&{ $exporting_package . '::' . $name } };
57 = $class->_make_wrapped_sub( $exporting_package, $name, $sub );
60 for my $name ( @{ $args{as_is} } ) {
65 $name = ( Class::MOP::get_code_info($name) )[1];
70 \&{ $exporting_package . '::' . $name };
74 $exports{$name} = sub {$sub};
81 # This variable gets closed over in each export _generator_. Then
82 # in the generator we grab the value and close over it _again_ in
83 # the real export, so it gets captured each time the generator
86 # In the meantime, we arrange for the import method we generate to
87 # set this variable to the caller each time it is called.
89 # This is all a bit confusing, but it works.
92 sub _make_wrapped_sub {
94 my $exporting_package = shift;
98 # We need to set the package at import time, so that when
99 # package Foo imports has(), we capture "Foo" as the
100 # package. This lets other packages call Foo::has() and get
101 # the right package. This is done for backwards compatibility
102 # with existing production code, not because this is a good
105 my $caller = $CALLER;
106 Class::MOP::subname( $exporting_package . '::'
107 . $name => sub { $sub->( $caller, @_ ) } );
111 sub _make_import_sub {
113 my $exporter = shift;
114 my $init_meta_args = shift;
118 # It's important to leave @_ as-is for the benefit of
122 $CALLER = Moose::Exporter::_get_caller(@_);
124 # this works because both pragmas set $^H (see perldoc
125 # perlvar) which affects the current compilation -
126 # i.e. the file who use'd us - which is why we don't need
127 # to do anything special to make it affect that file
128 # rather than this one (which is already compiled)
133 # we should never export to main
134 if ( $CALLER eq 'main' ) {
136 qq{$class does not export its sugar to the 'main' package.\n};
140 if ( $class->can('_init_meta') ) {
142 for_class => $CALLER,
143 %{ $init_meta_args || {} }
153 # 1 extra level because it's called by import so there's a layer
158 ( ref $_[1] && defined $_[1]->{into} ) ? $_[1]->{into}
159 : ( ref $_[1] && defined $_[1]->{into_level} )
160 ? caller( $offset + $_[1]->{into_level} )
164 sub _make_unimport_sub {
166 my $exported = shift;
168 # [12:24] <mst> yes. that's horrible. I know. but it should work.
170 # This will hopefully be replaced in the future once
171 # namespace::clean has an API for it.
173 @_ = ( 'namespace::clean', @{$exported} );
175 goto &namespace::clean::import;
185 Moose::Exporter - make an import() and unimport() just like Moose.pm
189 package MyApp::Moose;
197 Moose::Exporter->build_export_methods(
198 export => [ 'sugar1', 'sugar2', \&Some::Random::thing ],
199 init_meta_args => { metaclass_class => 'MyApp::Meta::Class' ],
208 sugar1 'do your thing';
215 This module encapsulates the logic to export sugar functions like
216 C<Moose.pm>. It does this by building custom C<import> and C<unimport>
217 methods for your module, based on a spec your provide.
219 It also lets your "stack" Moose-alike modules so you can export
220 Moose's sugar as well as your own, along with sugar from any random
221 C<MooseX> module, as long as they all use C<Moose::Exporter>.
225 This module provides exactly one public method:
227 =head2 Moose::Exporter->build_import_methods(...)
229 When you call this method, C<Moose::Exporter> build custom C<import>
230 and C<unimport> methods for your module. The import method will export
231 the functions you specify, and you can also tell it to export
232 functions exported by some other module (like C<Moose.pm>).
234 The C<unimport> method cleans the callers namespace of all the
237 This method accepts the following parameters:
241 =item * with_caller => [ ... ]
243 This a list of function I<names only> to be exported wrapped and then
244 exported. The wrapper will pass the name of the calling package as the
245 first argument to the function. Many sugar functions need to know
246 their caller so they can get the calling package's metaclass object.
248 =item * as_is => [ ... ]
250 This a list of function names or sub references to be exported
251 as-is. You can identify a subroutine by reference, which is handy to
252 re-export some other module's functions directly by reference
253 (C<\&Some::Package::function>).
255 =item * init_meta_args
263 Dave Rolsky E<lt>autarch@urth.orgE<gt>
265 This is largely a reworking of code in Moose.pm originally written by
266 Stevan Little and others.
268 =head1 COPYRIGHT AND LICENSE
270 Copyright 2008 by Infinity Interactive, Inc.
272 L<http://www.iinteractive.com>
274 This library is free software; you can redistribute it and/or modify
275 it under the same terms as Perl itself.