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 # We need to set the package at import time, so that when
51 # package Foo imports has(), we capture "Foo" as the
52 # package. This lets other packages call Foo::has() and get
53 # the right package. This is done for backwards compatibility
54 # with existing production code, not because this is a good
56 $exports{$name} = sub {
62 $caller = scalar caller($x++)
64 while ( $caller eq 'Sub::Exporter' );
66 Class::MOP::subname( $exporting_package . '::'
67 . $name => sub { $sub->( $caller, @_ ) } );
70 push @exported_names, $name;
73 for my $name ( @{ $args{as_is} } ) {
78 $name = ( Class::MOP::get_code_info($name) )[1];
81 $sub = do { no strict 'refs'; \&{ $exporting_package . '::' . $name } };
83 push @exported_names, $name;
86 $exports{$name} = sub { $sub };
89 my $exporter = Sub::Exporter::build_exporter(
92 groups => { default => [':all'] }
96 return $exporter, \@exported_names;
99 sub _make_import_sub {
101 my $exporting_package = shift;
102 my $init_meta_args = shift;
103 my $exporter = shift;
106 my $caller = Moose::Exporter->_get_caller(@_);
108 # this works because both pragmas set $^H (see perldoc perlvar)
109 # which affects the current compilation - i.e. the file who use'd
110 # us - which is why we don't need to do anything special to make
111 # it affect that file rather than this one (which is already compiled)
116 # we should never export to main
117 if ( $caller eq 'main' ) {
119 qq{$exporting_package does not export its sugar to the 'main' package.\n};
123 if ( $exporting_package->can('_init_meta') ) {
124 $exporting_package->_init_meta(
125 for_class => $caller,
126 %{ $init_meta_args || {} }
135 # 1 extra level because it's called by import so there's a layer
140 ( ref $_[1] && defined $_[1]->{into} ) ? $_[1]->{into}
141 : ( ref $_[1] && defined $_[1]->{into_level} )
142 ? caller( $offset + $_[1]->{into_level} )
146 sub _make_unimport_sub {
148 my $exported = shift;
150 # [12:24] <mst> yes. that's horrible. I know. but it should work.
152 # This will hopefully be replaced in the future once
153 # namespace::clean has an API for it.
155 @_ = ( 'namespace::clean', @{$exported} );
157 goto &namespace::clean::import;
167 Moose::Exporter - make an import() and unimport() just like Moose.pm
171 package MyApp::Moose;
179 Moose::Exporter->build_export_methods(
180 export => [ 'sugar1', 'sugar2', \&Some::Random::thing ],
181 init_meta_args => { metaclass_class => 'MyApp::Meta::Class' ],
190 sugar1 'do your thing';
197 This module encapsulates the logic to export sugar functions like
198 C<Moose.pm>. It does this by building custom C<import> and C<unimport>
199 methods for your module, based on a spec your provide.
201 It also lets your "stack" Moose-alike modules so you can export
202 Moose's sugar as well as your own, along with sugar from any random
203 C<MooseX> module, as long as they all use C<Moose::Exporter>.
207 This module provides exactly one public method:
209 =head2 Moose::Exporter->build_import_methods(...)
211 When you call this method, C<Moose::Exporter> build custom C<import>
212 and C<unimport> methods for your module. The import method will export
213 the functions you specify, and you can also tell it to export
214 functions exported by some other module (like C<Moose.pm>).
216 The C<unimport> method cleans the callers namespace of all the
219 This method accepts the following parameters:
223 =item * with_caller => [ ... ]
225 This a list of function I<names only> to be exported wrapped and then
226 exported. The wrapper will pass the name of the calling package as the
227 first argument to the function. Many sugar functions need to know
228 their caller so they can get the calling package's metaclass object.
230 =item * as_is => [ ... ]
232 This a list of function names or sub references to be exported
233 as-is. You can identify a subroutine by reference, which is handy to
234 re-export some other module's functions directly by reference
235 (C<\&Some::Package::function>).
237 =item * init_meta_args
245 Dave Rolsky E<lt>autarch@urth.orgE<gt>
247 This is largely a reworking of code in Moose.pm originally written by
248 Stevan Little and others.
250 =head1 COPYRIGHT AND LICENSE
252 Copyright 2008 by Infinity Interactive, Inc.
254 L<http://www.iinteractive.com>
256 This library is free software; you can redistribute it and/or modify
257 it under the same terms as Perl itself.