1 package Moose::Exporter;
7 use namespace::clean 0.08 ();
11 # This variable gets closed over in each export _generator_. Then in
12 # the generator we grab the value and close over it _again_ in the
13 # real export, so it gets captured each time the generator runs.
15 # In the meantime, we arrange for the import method we generate to set
16 # this variable to the caller each time it is called.
18 # This is all a bit confusing, but it works.
23 sub build_import_methods {
27 my $exporting_package = caller();
29 $EXPORT_SPEC{$exporting_package} = \%args;
31 my $exports = $class->_process_exports(
32 exporting_package => $exporting_package,
36 my $exporter = Sub::Exporter::build_exporter(
39 groups => { default => [':all'] }
43 my $import = $class->_make_import_sub(
45 $args{init_meta_args},
48 my $unimport = $class->_make_unimport_sub( [ keys %{$exports} ] );
51 *{ $exporting_package . '::import' } = $import;
52 *{ $exporting_package . '::unimport' } = $unimport;
55 sub _process_exports {
59 my $exporting_package = $args{exporting_package};
62 for my $name ( @{ $args{with_caller} } ) {
63 my $sub = do { no strict 'refs'; \&{ $exporting_package . '::' . $name } };
65 # We need to set the package at import time, so that when
66 # package Foo imports has(), we capture "Foo" as the
67 # package. This lets other packages call Foo::has() and get
68 # the right package. This is done for backwards compatibility
69 # with existing production code, not because this is a good
71 $exports{$name} = sub {
73 Class::MOP::subname( $exporting_package . '::'
74 . $name => sub { $sub->( $caller, @_ ) } );
78 for my $name ( @{ $args{as_is} } ) {
83 $name = ( Class::MOP::get_code_info($name) )[1];
86 $sub = do { no strict 'refs'; \&{ $exporting_package . '::' . $name } };
89 $exports{$name} = sub { $sub };
95 sub _make_import_sub {
98 my $init_meta_args = shift;
101 # It's important to leave @_ as-is for the benefit of
105 $CALLER = Moose::Exporter::_get_caller(@_);
107 # this works because both pragmas set $^H (see perldoc perlvar)
108 # which affects the current compilation - i.e. the file who use'd
109 # us - which is why we don't need to do anything special to make
110 # it affect that file rather than this one (which is already compiled)
115 # we should never export to main
116 if ( $CALLER eq 'main' ) {
118 qq{$class does not export its sugar to the 'main' package.\n};
122 if ( $class->can('_init_meta') ) {
124 for_class => $CALLER,
125 %{ $init_meta_args || {} }
134 # 1 extra level because it's called by import so there's a layer
139 ( ref $_[1] && defined $_[1]->{into} ) ? $_[1]->{into}
140 : ( ref $_[1] && defined $_[1]->{into_level} )
141 ? caller( $offset + $_[1]->{into_level} )
145 sub _make_unimport_sub {
147 my $exported = shift;
149 # [12:24] <mst> yes. that's horrible. I know. but it should work.
151 # This will hopefully be replaced in the future once
152 # namespace::clean has an API for it.
154 @_ = ( 'namespace::clean', @{$exported} );
156 goto &namespace::clean::import;
166 Moose::Exporter - make an import() and unimport() just like Moose.pm
170 package MyApp::Moose;
178 Moose::Exporter->build_export_methods(
179 export => [ 'sugar1', 'sugar2', \&Some::Random::thing ],
180 init_meta_args => { metaclass_class => 'MyApp::Meta::Class' ],
189 sugar1 'do your thing';
196 This module encapsulates the logic to export sugar functions like
197 C<Moose.pm>. It does this by building custom C<import> and C<unimport>
198 methods for your module, based on a spec your provide.
200 It also lets your "stack" Moose-alike modules so you can export
201 Moose's sugar as well as your own, along with sugar from any random
202 C<MooseX> module, as long as they all use C<Moose::Exporter>.
206 This module provides exactly one public method:
208 =head2 Moose::Exporter->build_import_methods(...)
210 When you call this method, C<Moose::Exporter> build custom C<import>
211 and C<unimport> methods for your module. The import method will export
212 the functions you specify, and you can also tell it to export
213 functions exported by some other module (like C<Moose.pm>).
215 The C<unimport> method cleans the callers namespace of all the
218 This method accepts the following parameters:
222 =item * with_caller => [ ... ]
224 This a list of function I<names only> to be exported wrapped and then
225 exported. The wrapper will pass the name of the calling package as the
226 first argument to the function. Many sugar functions need to know
227 their caller so they can get the calling package's metaclass object.
229 =item * as_is => [ ... ]
231 This a list of function names or sub references to be exported
232 as-is. You can identify a subroutine by reference, which is handy to
233 re-export some other module's functions directly by reference
234 (C<\&Some::Package::function>).
236 =item * init_meta_args
244 Dave Rolsky E<lt>autarch@urth.orgE<gt>
246 This is largely a reworking of code in Moose.pm originally written by
247 Stevan Little and others.
249 =head1 COPYRIGHT AND LICENSE
251 Copyright 2008 by Infinity Interactive, Inc.
253 L<http://www.iinteractive.com>
255 This library is free software; you can redistribute it and/or modify
256 it under the same terms as Perl itself.