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);
74 my @init_meta_methods;
76 foreach my $package(@export_from){
77 my $spec = $SPEC{$package} or next;
79 if(my $as_is = $spec->{as_is}){
80 foreach my $thingy (@{$as_is}){
81 my($code_package, $code_name, $code);
85 ($code_package, $code_name) = Mouse::Util::get_code_info($code);
89 $code_package = $package;
91 $code = \&{ $code_package . '::' . $code_name };
94 push @all, $code_name;
95 $exports{$code_name} = $code;
96 if($code_package eq $package){
97 push @removables, $code_name;
102 if(my $init_meta = $package->can('init_meta')){
103 if(!grep{ $_ == $init_meta } @init_meta_methods){
104 push @init_meta_methods, $init_meta;
108 $args{EXPORTS} = \%exports;
109 $args{REMOVABLES} = \@removables;
111 $args{groups}{all} ||= \@all;
113 if(my $default_list = $args{groups}{default}){
115 foreach my $keyword(@{$default_list}){
116 $default{$keyword} = $exports{$keyword}
117 || confess(qq{The $exporting_package package does not export "$keyword"});
119 $args{DEFAULT} = \%default;
122 $args{groups}{default} ||= \@all;
123 $args{DEFAULT} = $args{EXPORTS};
126 if(@init_meta_methods){
127 $args{INIT_META} = \@init_meta_methods;
130 return (\&do_import, \&do_unimport);
134 # the entity of general import()
136 my($package, @args) = @_;
138 my $spec = $SPEC{$package}
139 || confess("The package $package package does not use Mouse::Exporter");
141 my $into = _get_caller_package(ref($args[0]) ? shift @args : undef);
147 my $arg = shift @args;
149 if($arg eq 'traits'){
150 push @traits, ref($args[0]) ? @{shift(@args)} : shift(@args);
153 Mouse::Util::not_supported("-$arg");
156 elsif($arg =~ s/^://){
157 my $group = $spec->{groups}{$arg}
158 || confess(qq{The $package package does not export the group "$arg"});
159 push @exports, @{$group};
166 $^H |= _strict_bits; # strict->import;
167 ${^WARNING_BITS} |= $warnings::Bits{all}; # warnings->import;
169 if($spec->{INIT_META}){
171 foreach my $init_meta(@{$spec->{INIT_META}}){
172 $meta = $package->$init_meta(for_class => $into);
176 my $type = (split /::/, ref $meta)[-1]; # e.g. "Class" for "My::Meta::Class"
180 : Mouse::Util::resolve_metaclass_alias($type => $_, trait => 1)
183 require Mouse::Util::MetaRole;
184 Mouse::Util::MetaRole::apply_metaroles(
186 Mouse::Util::is_a_metarole($into->meta)
187 ? (role_metaroles => { role => \@traits })
188 : (class_metaroles => { class => \@traits }),
193 Carp::confess("Cannot provide traits when $package does not have an init_meta() method");
197 foreach my $keyword(@exports){
199 *{$into.'::'.$keyword} = $spec->{EXPORTS}{$keyword}
200 || confess(qq{The $package package does not export "$keyword"});
204 my $default = $spec->{DEFAULT};
205 while(my($keyword, $code) = each %{$default}){
207 *{$into.'::'.$keyword} = $code;
213 # the entity of general unimport()
215 my($package, $arg) = @_;
217 my $spec = $SPEC{$package}
218 || confess("The package $package does not use Mouse::Exporter");
220 my $from = _get_caller_package($arg);
227 for my $keyword (@{ $spec->{REMOVABLES} }) {
228 next if !exists $stash->{$keyword};
229 my $gv = \$stash->{$keyword};
230 if(ref($gv) eq 'GLOB' && *{$gv}{CODE} == $spec->{EXPORTS}{$keyword}){ # make sure it is from us
231 delete $stash->{$keyword};
237 sub _get_caller_package {
240 # We need one extra level because it's called by import so there's a layer
243 return defined($arg->{into}) ? $arg->{into}
244 : defined($arg->{into_level}) ? scalar caller(1 + $arg->{into_level})
248 return scalar caller(1);
259 Mouse::Exporter - make an import() and unimport() just like Mouse.pm
263 This document describes Mouse version 0.50_03
267 package MyApp::Mouse;
272 Mouse::Exporter->setup_import_methods(
273 as_is => [ 'has_rw', 'other_sugar', \&Some::Random::thing ],
278 my $meta = caller->meta;
279 my ( $name, %options ) = @_;
280 $meta->add_attribute(
300 This module encapsulates the exporting of sugar functions in a
301 C<Mouse.pm>-like manner. It does this by building custom C<import>,
302 C<unimport> methods for your module, based on a spec you provide.
304 Note that C<Mouse::Exporter> does not provide the C<with_meta> option,
305 but you can easily get the metaclass by C<< caller->meta >> as L</SYNOPSIS> shows.
309 =head2 C<< setup_import_methods( ARGS ) >>
311 =head2 C<< build_import_methods( ARGS ) -> (\&import, \&unimport) >>