1 package Mouse::Exporter;
9 my $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 $SPEC{$exporting_package} = \%args;
33 my @stack = ($exporting_package);
35 while(my $current = shift @stack){
36 push @export_from, $current;
38 my $also = $SPEC{$current}{also} or next;
39 push @stack, grep{ !$seen{$_}++ } ref($also) ? @{ $also } : $also;
43 @export_from = ($exporting_package);
51 my @init_meta_methods;
53 foreach my $package(@export_from){
54 my $spec = $SPEC{$package} or next;
56 if(my $as_is = $spec->{as_is}){
57 foreach my $thingy (@{$as_is}){
58 my($code_package, $code_name, $code);
62 ($code_package, $code_name) = Mouse::Util::get_code_info($code);
66 $code_package = $package;
68 $code = \&{ $code_package . '::' . $code_name };
71 push @all, $code_name;
72 $exports{$code_name} = $code;
73 if($code_package eq $package){
74 push @removables, $code_name;
79 if(my $init_meta = $package->can('init_meta')){
80 if(!grep{ $_ == $init_meta } @init_meta_methods){
81 unshift @init_meta_methods, $init_meta;
85 $args{EXPORTS} = \%exports;
86 $args{REMOVABLES} = \@removables;
88 $args{groups}{all} ||= \@all;
90 if(my $default_list = $args{groups}{default}){
92 foreach my $keyword(@{$default_list}){
93 $default{$keyword} = $exports{$keyword}
94 || confess(qq{The $exporting_package package does not export "$keyword"});
96 $args{DEFAULT} = \%default;
99 $args{groups}{default} ||= \@all;
100 $args{DEFAULT} = $args{EXPORTS};
103 if(@init_meta_methods){
104 $args{INIT_META} = \@init_meta_methods;
110 *{$exporting_package . '::import'} = \&do_import;
111 *{$exporting_package . '::unimport'} = \&do_unimport;
117 # the entity of general import()
119 my($package, @args) = @_;
121 my $spec = $SPEC{$package}
122 || confess("The package $package package does not use Mouse::Exporter");
124 my $into = _get_caller_package(ref($args[0]) ? shift @args : undef);
127 foreach my $arg(@args){
129 Mouse::Util::not_supported("-$arg");
131 elsif($arg =~ s/^://){
132 my $group = $spec->{groups}{$arg}
133 || confess(qq{The $package package does not export the group "$arg"});
134 push @exports, @{$group};
141 $^H |= $strict_bits; # strict->import;
142 ${^WARNING_BITS} = $warnings::Bits{all}; # warnings->import;
144 if($into eq 'main' && !$spec->{_export_to_main}){
145 warn qq{$package does not export its sugar to the 'main' package.\n};
149 if($spec->{INIT_META}){
150 foreach my $init_meta(@{$spec->{INIT_META}}){
151 $into->$init_meta(for_class => $into);
154 # _apply_meta_traits($into); # TODO
158 foreach my $keyword(@exports){
160 *{$into.'::'.$keyword} = $spec->{EXPORTS}{$keyword}
161 || confess(qq{The $package package does not export "$keyword"});
165 my $default = $spec->{DEFAULT};
166 while(my($keyword, $code) = each %{$default}){
168 *{$into.'::'.$keyword} = $code;
174 # the entity of general unimport()
176 my($package, $arg) = @_;
178 my $spec = $SPEC{$package}
179 || confess("The package $package does not use Mouse::Exporter");
181 my $from = _get_caller_package($arg);
188 for my $keyword (@{ $spec->{REMOVABLES} }) {
189 my $gv = \$stash->{$keyword};
190 if(ref($gv) eq 'GLOB' && *{$gv}{CODE} == $spec->{EXPORTS}{$keyword}){ # make sure it is from us
191 delete $stash->{$keyword};
197 sub _get_caller_package {
200 # 2 extra level because it's called by import so there's a layer
\r
205 return defined($arg->{into}) ? $arg->{into}
206 : defined($arg->{into_level}) ? scalar caller($offset + $arg->{into_level})
207 : scalar caller($offset);
210 return scalar caller($offset);
220 Mouse - The Mouse Exporter
227 Mouse::Exporter->setup_import_methods(