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);
29 Mouse::Util::install_subroutines($exporting_package,
31 unimport => $unimport,
33 export_to_level => sub {
34 my($package, $level, undef, @args) = @_; # the third argument is redundant
35 $package->import({ into_level => $level + 1 }, @args);
38 my($package, $into, @args) = @_;
39 $package->import({ into => $into }, @args);
45 sub build_import_methods{
46 my($class, %args) = @_;
48 my $exporting_package = $args{exporting_package} ||= caller();
50 $SPEC{$exporting_package} = \%args;
56 my @stack = ($exporting_package);
58 while(my $current = shift @stack){
59 push @export_from, $current;
61 my $also = $SPEC{$current}{also} or next;
62 push @stack, grep{ !$seen{$_}++ } ref($also) ? @{ $also } : $also;
66 @export_from = ($exporting_package);
73 my @init_meta_methods;
75 foreach my $package(@export_from){
76 my $spec = $SPEC{$package} or next;
78 if(my $as_is = $spec->{as_is}){
79 foreach my $thingy (@{$as_is}){
80 my($code_package, $code_name, $code);
84 ($code_package, $code_name) = Mouse::Util::get_code_info($code);
87 $code_package = $package;
90 $code = \&{ $code_package . '::' . $code_name };
93 push @all, $code_name;
94 $exports{$code_name} = $code;
95 if($code_package eq $package){
96 push @removables, $code_name;
101 if(my $init_meta = $package->can('init_meta')){
102 if(!grep{ $_ == $init_meta } @init_meta_methods){
103 push @init_meta_methods, $init_meta;
107 $args{EXPORTS} = \%exports;
108 $args{REMOVABLES} = \@removables;
110 $args{groups}{all} ||= \@all;
112 if(my $default_list = $args{groups}{default}){
114 foreach my $keyword(@{$default_list}){
115 $default{$keyword} = $exports{$keyword}
116 || confess(qq{The $exporting_package package does not export "$keyword"});
118 $args{DEFAULT} = \%default;
121 $args{groups}{default} ||= \@all;
122 $args{DEFAULT} = $args{EXPORTS};
125 if(@init_meta_methods){
126 $args{INIT_META} = \@init_meta_methods;
129 return (\&do_import, \&do_unimport);
133 # the entity of general import()
135 my($package, @args) = @_;
137 my $spec = $SPEC{$package}
138 || confess("The package $package package does not use Mouse::Exporter");
140 my $into = _get_caller_package(ref($args[0]) ? shift @args : undef);
146 my $arg = shift @args;
148 if($arg eq 'traits'){
149 push @traits, ref($args[0]) ? @{shift(@args)} : shift(@args);
152 Mouse::Util::not_supported("-$arg");
155 elsif($arg =~ s/^://){
156 my $group = $spec->{groups}{$arg}
157 || confess(qq{The $package package does not export the group "$arg"});
158 push @exports, @{$group};
165 $^H |= _strict_bits; # strict->import;
166 ${^WARNING_BITS} |= $warnings::Bits{all}; # warnings->import;
168 if($spec->{INIT_META}){
170 foreach my $init_meta(@{$spec->{INIT_META}}){
171 $meta = $package->$init_meta(for_class => $into);
175 my $type = (split /::/, ref $meta)[-1]; # e.g. "Class" for "My::Meta::Class"
179 : Mouse::Util::resolve_metaclass_alias($type => $_, trait => 1)
182 require Mouse::Util::MetaRole;
183 Mouse::Util::MetaRole::apply_metaroles(
185 Mouse::Util::is_a_metarole($into->meta)
186 ? (role_metaroles => { role => \@traits })
187 : (class_metaroles => { class => \@traits }),
192 Carp::confess("Cannot provide traits when $package does not have an init_meta() method");
197 foreach my $keyword(@exports){
199 $keyword => ($spec->{EXPORTS}{$keyword}
200 || confess(qq{The $package package does not export "$keyword"})
203 Mouse::Util::install_subroutines($into, @export_table);
206 Mouse::Util::install_subroutines($into, %{$spec->{DEFAULT}});
211 # the entity of general unimport()
213 my($package, $arg) = @_;
215 my $spec = $SPEC{$package}
216 || confess("The package $package does not use Mouse::Exporter");
218 my $from = _get_caller_package($arg);
225 for my $keyword (@{ $spec->{REMOVABLES} }) {
226 next if !exists $stash->{$keyword};
227 my $gv = \$stash->{$keyword};
228 if(ref($gv) eq 'GLOB' && *{$gv}{CODE} == $spec->{EXPORTS}{$keyword}){ # make sure it is from us
229 delete $stash->{$keyword};
235 sub _get_caller_package {
238 # We need one extra level because it's called by import so there's a layer
241 return defined($arg->{into}) ? $arg->{into}
242 : defined($arg->{into_level}) ? scalar caller(1 + $arg->{into_level})
246 return scalar caller(1);
257 Mouse::Exporter - make an import() and unimport() just like Mouse.pm
261 This document describes Mouse version 0.50_03
265 package MyApp::Mouse;
270 Mouse::Exporter->setup_import_methods(
271 as_is => [ 'has_rw', 'other_sugar', \&Some::Random::thing ],
276 my $meta = caller->meta;
277 my ( $name, %options ) = @_;
278 $meta->add_attribute(
298 This module encapsulates the exporting of sugar functions in a
299 C<Mouse.pm>-like manner. It does this by building custom C<import>,
300 C<unimport> methods for your module, based on a spec you provide.
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) >>