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 ( $exporter, $exported ) = $class->_build_exporter(
22 exporting_package => $exporting_package,
26 my $import = $class->_make_import_sub(
27 $exporting_package, $args{init_meta_args},
31 my $unimport = $class->_make_unimport_sub($exported);
34 *{ $exporting_package . '::import' } = $import;
35 *{ $exporting_package . '::unimport' } = $unimport;
43 my $exporting_package = $args{exporting_package};
47 for my $name ( @{ $args{with_caller} } ) {
48 my $sub = do { no strict 'refs'; \&{ $exporting_package . '::' . $name } };
50 my $wrapped = Class::MOP::subname(
51 $exporting_package . '::' . $name => sub { $sub->( scalar caller(), @_ ) } );
53 $exports{$name} = sub { $wrapped };
55 push @exported_names, $name;
58 for my $name ( @{ $args{as_is} } ) {
63 $name = ( Class::MOP::get_code_info($name) )[1];
66 $sub = do { no strict 'refs'; \&{ $exporting_package . '::' . $name } };
68 push @exported_names, $name;
71 $exports{$name} = sub { $sub };
74 my $exporter = Sub::Exporter::build_exporter(
77 groups => { default => [':all'] }
81 return $exporter, \@exported_names;
84 sub _make_import_sub {
86 my $exporting_package = shift;
87 my $init_meta_args = shift;
91 my $caller = Moose::Exporter->_get_caller(@_);
93 # this works because both pragmas set $^H (see perldoc perlvar)
94 # which affects the current compilation - i.e. the file who use'd
95 # us - which is why we don't need to do anything special to make
96 # it affect that file rather than this one (which is already compiled)
101 # we should never export to main
102 if ( $caller eq 'main' ) {
104 qq{$exporting_package does not export its sugar to the 'main' package.\n};
108 if ( $exporting_package->can('_init_meta') ) {
109 $exporting_package->_init_meta(
110 for_class => $caller,
111 %{ $init_meta_args || {} }
120 # 1 extra level because it's called by import so there's a layer
125 ( ref $_[1] && defined $_[1]->{into} ) ? $_[1]->{into}
126 : ( ref $_[1] && defined $_[1]->{into_level} )
127 ? caller( $offset + $_[1]->{into_level} )
131 sub _make_unimport_sub {
133 my $exported = shift;
135 # [12:24] <mst> yes. that's horrible. I know. but it should work.
137 # This will hopefully be replaced in the future once
138 # namespace::clean has an API for it.
140 @_ = ( 'namespace::clean', @{$exported} );
142 goto &namespace::clean::import;
152 Moose::Exporter - make an import() and unimport() just like Moose.pm
156 package MyApp::Moose;
164 Moose::Exporter->build_export_methods(
165 export => [ 'sugar1', 'sugar2', \&Some::Random::thing ],
166 init_meta_args => { metaclass_class => 'MyApp::Meta::Class' ],
175 sugar1 'do your thing';
182 This module encapsulates the logic to export sugar functions like
183 C<Moose.pm>. It does this by building custom C<import> and C<unimport>
184 methods for your module, based on a spec your provide.
186 It also lets your "stack" Moose-alike modules so you can export
187 Moose's sugar as well as your own, along with sugar from any random
188 C<MooseX> module, as long as they all use C<Moose::Exporter>.
192 This module provides exactly one public method:
194 =head2 Moose::Exporter->build_import_methods(...)
196 When you call this method, C<Moose::Exporter> build custom C<import>
197 and C<unimport> methods for your module. The import method will export
198 the functions you specify, and you can also tell it to export
199 functions exported by some other module (like C<Moose.pm>).
201 The C<unimport> method cleans the callers namespace of all the
204 This method accepts the following parameters:
208 =item * with_caller => [ ... ]
210 This a list of function I<names only> to be exported wrapped and then
211 exported. The wrapper will pass the name of the calling package as the
212 first argument to the function. Many sugar functions need to know
213 their caller so they can get the calling package's metaclass object.
215 =item * as_is => [ ... ]
217 This a list of function names or sub references to be exported
218 as-is. You can identify a subroutine by reference, which is handy to
219 re-export some other module's functions directly by reference
220 (C<\&Some::Package::function>).
222 =item * init_meta_args
230 Dave Rolsky E<lt>autarch@urth.orgE<gt>
232 This is largely a reworking of code in Moose.pm originally written by
233 Stevan Little and others.
235 =head1 COPYRIGHT AND LICENSE
237 Copyright 2008 by Infinity Interactive, Inc.
239 L<http://www.iinteractive.com>
241 This library is free software; you can redistribute it and/or modify
242 it under the same terms as Perl itself.