1 package Mouse::Exporter;
10 BEGIN{ $strict_bits = strict::bits(qw(subs refs vars)); }
12 my $warnings_extra_bits;
13 BEGIN{ $warnings_extra_bits = warnings::bits(FATAL => 'recursion') }
15 # it must be "require", because Mouse::Util depends on Mouse::Exporter,
16 # which depends on Mouse::Util::import()
22 # warnings->import('all', FATAL => 'recursion');
23 ${^WARNING_BITS} |= $warnings::Bits{all};
24 ${^WARNING_BITS} |= $warnings_extra_bits;
29 sub setup_import_methods{
30 my($class, %args) = @_;
32 my $exporting_package = $args{exporting_package} ||= caller();
34 my($import, $unimport) = $class->build_import_methods(%args);
36 Mouse::Util::install_subroutines($exporting_package,
38 unimport => $unimport,
40 export_to_level => sub {
41 my($package, $level, undef, @args) = @_; # the third argument is redundant
42 $package->import({ into_level => $level + 1 }, @args);
45 my($package, $into, @args) = @_;
46 $package->import({ into => $into }, @args);
52 sub build_import_methods{
53 my($self, %args) = @_;
55 my $exporting_package = $args{exporting_package} ||= caller();
57 $SPEC{$exporting_package} = \%args;
63 my @stack = ($exporting_package);
65 while(my $current = shift @stack){
66 push @export_from, $current;
68 my $also = $SPEC{$current}{also} or next;
69 push @stack, grep{ !$seen{$_}++ } ref($also) ? @{ $also } : $also;
73 @export_from = ($exporting_package);
80 my @init_meta_methods;
82 foreach my $package(@export_from){
83 my $spec = $SPEC{$package} or next;
85 if(my $as_is = $spec->{as_is}){
86 foreach my $thingy (@{$as_is}){
87 my($code_package, $code_name, $code);
91 ($code_package, $code_name) = Mouse::Util::get_code_info($code);
94 $code_package = $package;
97 $code = \&{ $code_package . '::' . $code_name };
100 push @all, $code_name;
101 $exports{$code_name} = $code;
102 if($code_package eq $package){
103 push @removables, $code_name;
108 if(my $init_meta = $package->can('init_meta')){
109 if(!grep{ $_ == $init_meta } @init_meta_methods){
110 push @init_meta_methods, $init_meta;
114 $args{EXPORTS} = \%exports;
115 $args{REMOVABLES} = \@removables;
117 $args{groups}{all} ||= \@all;
119 if(my $default_list = $args{groups}{default}){
121 foreach my $keyword(@{$default_list}){
122 $default{$keyword} = $exports{$keyword}
123 || Carp::confess(qq{The $exporting_package package does not export "$keyword"});
125 $args{DEFAULT} = \%default;
128 $args{groups}{default} ||= \@all;
129 $args{DEFAULT} = $args{EXPORTS};
132 if(@init_meta_methods){
133 $args{INIT_META} = \@init_meta_methods;
136 return (\&do_import, \&do_unimport);
140 # the entity of general import()
142 my($package, @args) = @_;
144 my $spec = $SPEC{$package}
145 || Carp::confess("The package $package package does not use Mouse::Exporter");
147 my $into = _get_caller_package(ref($args[0]) ? shift @args : undef);
153 my $arg = shift @args;
155 if($arg eq 'traits'){
156 push @traits, ref($args[0]) ? @{shift(@args)} : shift(@args);
159 Mouse::Util::not_supported("-$arg");
162 elsif($arg =~ s/^://){
163 my $group = $spec->{groups}{$arg}
164 || Carp::confess(qq{The $package package does not export the group "$arg"});
165 push @exports, @{$group};
174 # warnings->import('all', FATAL => 'recursion');
175 ${^WARNING_BITS} |= $warnings::Bits{all};
176 ${^WARNING_BITS} |= $warnings_extra_bits;
178 if($spec->{INIT_META}){
180 foreach my $init_meta(@{$spec->{INIT_META}}){
181 $meta = $package->$init_meta(for_class => $into);
185 my $type = (split /::/, ref $meta)[-1]; # e.g. "Class" for "My::Meta::Class"
189 : Mouse::Util::resolve_metaclass_alias($type => $_, trait => 1)
192 require Mouse::Util::MetaRole;
193 Mouse::Util::MetaRole::apply_metaroles(
195 Mouse::Util::is_a_metarole($into->meta)
196 ? (role_metaroles => { role => \@traits })
197 : (class_metaroles => { class => \@traits }),
202 Carp::confess("Cannot provide traits when $package does not have an init_meta() method");
207 foreach my $keyword(@exports){
209 $keyword => ($spec->{EXPORTS}{$keyword}
210 || Carp::confess(qq{The $package package does not export "$keyword"})
213 Mouse::Util::install_subroutines($into, @export_table);
216 Mouse::Util::install_subroutines($into, %{$spec->{DEFAULT}});
221 # the entity of general unimport()
223 my($package, $arg) = @_;
225 my $spec = $SPEC{$package}
226 || Carp::confess("The package $package does not use Mouse::Exporter");
228 my $from = _get_caller_package($arg);
235 for my $keyword (@{ $spec->{REMOVABLES} }) {
236 next if !exists $stash->{$keyword};
237 my $gv = \$stash->{$keyword};
238 if(ref($gv) eq 'GLOB' && *{$gv}{CODE} == $spec->{EXPORTS}{$keyword}){ # make sure it is from us
239 delete $stash->{$keyword};
245 sub _get_caller_package {
248 # We need one extra level because it's called by import so there's a layer
251 return defined($arg->{into}) ? $arg->{into}
252 : defined($arg->{into_level}) ? scalar caller(1 + $arg->{into_level})
256 return scalar caller(1);
267 Mouse::Exporter - make an import() and unimport() just like Mouse.pm
271 This document describes Mouse version 0.70
275 package MyApp::Mouse;
280 Mouse::Exporter->setup_import_methods(
281 as_is => [ 'has_rw', 'other_sugar', \&Some::Random::thing ],
286 my $meta = caller->meta;
287 my ( $name, %options ) = @_;
288 $meta->add_attribute(
308 This module encapsulates the exporting of sugar functions in a
309 C<Mouse.pm>-like manner. It does this by building custom C<import>,
310 C<unimport> methods for your module, based on a spec you provide.
312 Note that C<Mouse::Exporter> does not provide the C<with_meta> option,
313 but you can easily get the metaclass by C<< caller->meta >> as L</SYNOPSIS> shows.
317 =head2 C<< setup_import_methods( ARGS ) >>
319 =head2 C<< build_import_methods( ARGS ) -> (\&import, \&unimport) >>