1 package Mouse::Exporter;
7 use Mouse::Util qw(get_code_info not_supported);
11 my $strict_bits = strict::bits(qw(subs refs vars));
13 sub setup_import_methods{
14 my($class, %args) = @_;
16 my $exporting_package = $args{exporting_package} ||= caller();
18 $SPEC{$exporting_package} = \%args;
24 my @stack = ($exporting_package);
26 while(my $current = shift @stack){
27 push @export_from, $current;
29 my $also = $SPEC{$current}{also} or next;
30 push @stack, grep{ !$seen{$_}++ } @{ $also };
34 @export_from = ($exporting_package);
42 my @init_meta_methods;
44 foreach my $package(@export_from){
45 my $spec = $SPEC{$package} or next;
47 if(my $as_is = $spec->{as_is}){
48 foreach my $thingy (@{$as_is}){
49 my($code_package, $code_name, $code);
53 ($code_package, $code_name) = get_code_info($code);
57 $code_package = $package;
59 $code = \&{ $code_package . '::' . $code_name };
62 push @all, $code_name;
63 $exports{$code_name} = $code;
64 if($code_package eq $package){
65 push @removables, $code_name;
70 if(my $init_meta = $package->can('init_meta')){
71 if(!grep{ $_ == $init_meta } @init_meta_methods){
72 unshift @init_meta_methods, $init_meta;
76 $args{EXPORTS} = \%exports;
77 $args{REMOVABLES} = \@removables;
79 $args{group}{all} ||= \@all;
81 if(my $default_list = $args{group}{default}){
83 foreach my $keyword(@{$default_list}){
84 $default{$keyword} = $exports{$keyword}
85 || confess(qq{The $exporting_package package does not export "$keyword"});
87 $args{DEFAULT} = \%default;
90 $args{group}{default} ||= \@all;
91 $args{DEFAULT} = $args{EXPORTS};
94 if(@init_meta_methods){
95 $args{INIT_META} = \@init_meta_methods;
101 *{$exporting_package . '::import'} = \&do_import;
102 *{$exporting_package . '::unimport'} = \&do_unimport;
108 # the entity of general import()
110 my($package, @args) = @_;
112 my $spec = $SPEC{$package}
113 || confess("The package $package package does not use Mouse::Exporter");
115 my $into = _get_caller_package(ref($args[0]) ? shift @args : undef);
118 foreach my $arg(@args){
120 not_supported "-$arg";
122 elsif($arg =~ s/^://){
123 my $group = $spec->{group}{$arg}
124 || confess(qq{The $package package does not export the group "$arg"});
125 push @exports, @{$group};
132 $^H |= $strict_bits; # strict->import;
133 ${^WARNING_BITS} = $warnings::Bits{all}; # warnings->import;
135 if($into eq 'main' && !$spec->{_not_export_to_main}){
136 warn qq{$package does not export its sugar to the 'main' package.\n};
140 if($spec->{INIT_META}){
141 foreach my $init_meta(@{$spec->{INIT_META}}){
142 $into->$init_meta(for_class => $into);
145 # _apply_meta_traits($into); # TODO
149 foreach my $keyword(@exports){
151 *{$into.'::'.$keyword} = $spec->{EXPORTS}{$keyword}
152 || confess(qq{The $package package does not export "$keyword"});
156 my $default = $spec->{DEFAULT};
157 while(my($keyword, $code) = each %{$default}){
159 *{$into.'::'.$keyword} = $code;
165 # the entity of general unimport()
167 my($package, $arg) = @_;
169 my $spec = $SPEC{$package}
170 || confess("The package $package does not use Mouse::Exporter");
172 my $from = _get_caller_package($arg);
179 for my $keyword (@{ $spec->{REMOVABLES} }) {
180 my $gv = \$stash->{$keyword};
181 if(ref($gv) eq 'GLOB' && *{$gv}{CODE} == $spec->{EXPORTS}{$keyword}){ # make sure it is from us
182 delete $stash->{$keyword};
188 sub _get_caller_package {
191 # 2 extra level because it's called by import so there's a layer
\r
196 return defined($arg->{into}) ? $arg->{into}
197 : defined($arg->{into_level}) ? scalar caller($offset + $arg->{into_level})
198 : scalar caller($offset);
201 return scalar caller($offset);
211 Mouse - The Mouse Exporter
218 Mouse::Exporter->setup_import_methods(