1 package Mouse::Exporter;
9 use constant _strict_bits => strict::bits(qw(subs refs vars));
11 # it must be "require", because Mouse::Util depends on Mouse::Exporter,
12 # which depends on Mouse::Util::import()
16 $^H |= _strict_bits; # strict->import;
17 ${^WARNING_BITS} |= $warnings::Bits{all}; # warnings->import;
22 sub setup_import_methods{
23 my($class, %args) = @_;
25 my $exporting_package = $args{exporting_package} ||= caller();
27 my($import, $unimport) = $class->build_import_methods(%args);
31 *{$exporting_package . '::import'} = $import;
32 *{$exporting_package . '::unimport'} = $unimport;
34 # for backward compatibility
35 *{$exporting_package . '::export_to_level'} = sub{
36 my($package, $level, undef, @args) = @_; # the third argument is redundant
37 $package->import({ into_level => $level + 1 }, @args);
39 *{$exporting_package . '::export'} = sub{
40 my($package, $into, @args) = @_;
41 $package->import({ into => $into }, @args);
46 sub build_import_methods{
47 my($class, %args) = @_;
49 my $exporting_package = $args{exporting_package} ||= caller();
51 $SPEC{$exporting_package} = \%args;
57 my @stack = ($exporting_package);
59 while(my $current = shift @stack){
60 push @export_from, $current;
62 my $also = $SPEC{$current}{also} or next;
63 push @stack, grep{ !$seen{$_}++ } ref($also) ? @{ $also } : $also;
67 @export_from = ($exporting_package);
75 my @init_meta_methods;
77 foreach my $package(@export_from){
78 my $spec = $SPEC{$package} or next;
80 if(my $as_is = $spec->{as_is}){
81 foreach my $thingy (@{$as_is}){
82 my($code_package, $code_name, $code);
86 ($code_package, $code_name) = Mouse::Util::get_code_info($code);
90 $code_package = $package;
92 $code = \&{ $code_package . '::' . $code_name };
95 push @all, $code_name;
96 $exports{$code_name} = $code;
97 if($code_package eq $package){
98 push @removables, $code_name;
103 if(my $init_meta = $package->can('init_meta')){
104 if(!grep{ $_ == $init_meta } @init_meta_methods){
105 push @init_meta_methods, $init_meta;
109 $args{EXPORTS} = \%exports;
110 $args{REMOVABLES} = \@removables;
112 $args{groups}{all} ||= \@all;
114 if(my $default_list = $args{groups}{default}){
116 foreach my $keyword(@{$default_list}){
117 $default{$keyword} = $exports{$keyword}
118 || confess(qq{The $exporting_package package does not export "$keyword"});
120 $args{DEFAULT} = \%default;
123 $args{groups}{default} ||= \@all;
124 $args{DEFAULT} = $args{EXPORTS};
127 if(@init_meta_methods){
128 $args{INIT_META} = \@init_meta_methods;
132 return (\&do_import, \&do_unimport);
136 # the entity of general import()
138 my($package, @args) = @_;
140 my $spec = $SPEC{$package}
141 || confess("The package $package package does not use Mouse::Exporter");
143 my $into = _get_caller_package(ref($args[0]) ? shift @args : undef);
149 my $arg = shift @args;
151 if($arg eq 'traits'){
152 push @traits, @{shift(@args)};
155 Mouse::Util::not_supported("-$arg");
158 elsif($arg =~ s/^://){
159 my $group = $spec->{groups}{$arg}
160 || confess(qq{The $package package does not export the group "$arg"});
161 push @exports, @{$group};
168 $^H |= _strict_bits; # strict->import;
169 ${^WARNING_BITS} |= $warnings::Bits{all}; # warnings->import;
171 if($spec->{INIT_META}){
173 foreach my $init_meta(@{$spec->{INIT_META}}){
174 $meta = $into->$init_meta(for_class => $into);
178 my $type = (split /::/, ref $meta)[-1]; # e.g. "Class" for "My::Meta::Class"
180 map{ ref($_) ? $_ : Mouse::Util::resolve_metaclass_alias($type => $_, trait => 1) }
183 not_supported('-traits');
184 require Mouse::Util::MetaRole;
185 Mouse::Util::MetaRole::apply_metaclass_roles(
187 metaclass_roles => \@traits,
193 foreach my $keyword(@exports){
195 *{$into.'::'.$keyword} = $spec->{EXPORTS}{$keyword}
196 || confess(qq{The $package package does not export "$keyword"});
200 my $default = $spec->{DEFAULT};
201 while(my($keyword, $code) = each %{$default}){
203 *{$into.'::'.$keyword} = $code;
209 # the entity of general unimport()
211 my($package, $arg) = @_;
213 my $spec = $SPEC{$package}
214 || confess("The package $package does not use Mouse::Exporter");
216 my $from = _get_caller_package($arg);
223 for my $keyword (@{ $spec->{REMOVABLES} }) {
224 my $gv = \$stash->{$keyword};
225 if(ref($gv) eq 'GLOB' && *{$gv}{CODE} == $spec->{EXPORTS}{$keyword}){ # make sure it is from us
226 delete $stash->{$keyword};
232 # 1 extra level because it's called by import so there's a layer
\r
236 sub _get_caller_package {
240 return defined($arg->{into}) ? $arg->{into}
241 : defined($arg->{into_level}) ? scalar caller(_LEVEL + $arg->{into_level})
242 : scalar caller(_LEVEL);
245 return scalar caller(_LEVEL);
257 Mouse::Exporter - make an import() and unimport() just like Mouse.pm
261 This document describes Mouse version 0.40_05
265 package MyApp::Mouse;
\r
268 use Mouse::Exporter;
\r
270 Mouse::Exporter->setup_import_methods(
\r
271 as_is => [ 'has_rw', 'other_sugar', \&Some::Random::thing ],
\r
276 my $meta = caller->meta;
\r
277 my ( $name, %options ) = @_;
\r
278 $meta->add_attribute(
\r
286 package MyApp::User;
\r
298 This module encapsulates the exporting of sugar functions in a
\r
299 C<Mouse.pm>-like manner. It does this by building custom C<import>,
\r
300 C<unimport> methods for your module, based on a spec you provide.
\r
302 Note that C<Mouse::Exporter> does not provide the C<with_meta> option,
303 but you can easily get the metaclass by C<< caller->meta >> as L</SYNOPSIS> shows.
307 =head2 C<< setup_import_methods( ARGS ) >>
309 =head2 C<< build_import_methods( ARGS ) -> (\&import, \&unimport) >>