1 package Mouse::Exporter;
10 BEGIN{ $strict_bits = strict::bits(qw(subs refs vars)); }
12 # it must be "require", because Mouse::Util depends on Mouse::Exporter,
13 # which depends on Mouse::Util::import()
17 $^H |= $strict_bits; # strict->import;
18 ${^WARNING_BITS} |= $warnings::Bits{all}; # warnings->import;
23 sub setup_import_methods{
24 my($class, %args) = @_;
26 my $exporting_package = $args{exporting_package} ||= caller();
28 my($import, $unimport) = $class->build_import_methods(%args);
30 Mouse::Util::install_subroutines($exporting_package,
32 unimport => $unimport,
34 export_to_level => sub {
35 my($package, $level, undef, @args) = @_; # the third argument is redundant
37 Carp::carp("$package->export_to_level has been deprecated."
38 ." Use $package->import({ into_level => LEVEL }) instead");
39 $package->import({ into_level => $level + 1 }, @args);
42 my($package, $into, @args) = @_;
43 Carp::carp("$package->export has been deprecated."
44 ." Use $package->import({ into => PACKAGE }) instead");
45 $package->import({ into => $into }, @args);
51 sub build_import_methods{
52 my($class, %args) = @_;
54 my $exporting_package = $args{exporting_package} ||= caller();
56 $SPEC{$exporting_package} = \%args;
62 my @stack = ($exporting_package);
64 while(my $current = shift @stack){
65 push @export_from, $current;
67 my $also = $SPEC{$current}{also} or next;
68 push @stack, grep{ !$seen{$_}++ } ref($also) ? @{ $also } : $also;
72 @export_from = ($exporting_package);
79 my @init_meta_methods;
81 foreach my $package(@export_from){
82 my $spec = $SPEC{$package} or next;
84 if(my $as_is = $spec->{as_is}){
85 foreach my $thingy (@{$as_is}){
86 my($code_package, $code_name, $code);
90 ($code_package, $code_name) = Mouse::Util::get_code_info($code);
93 $code_package = $package;
96 $code = \&{ $code_package . '::' . $code_name };
99 push @all, $code_name;
100 $exports{$code_name} = $code;
101 if($code_package eq $package){
102 push @removables, $code_name;
107 if(my $init_meta = $package->can('init_meta')){
108 if(!grep{ $_ == $init_meta } @init_meta_methods){
109 push @init_meta_methods, $init_meta;
113 $args{EXPORTS} = \%exports;
114 $args{REMOVABLES} = \@removables;
116 $args{groups}{all} ||= \@all;
118 if(my $default_list = $args{groups}{default}){
120 foreach my $keyword(@{$default_list}){
121 $default{$keyword} = $exports{$keyword}
122 || confess(qq{The $exporting_package package does not export "$keyword"});
124 $args{DEFAULT} = \%default;
127 $args{groups}{default} ||= \@all;
128 $args{DEFAULT} = $args{EXPORTS};
131 if(@init_meta_methods){
132 $args{INIT_META} = \@init_meta_methods;
135 return (\&do_import, \&do_unimport);
139 # the entity of general import()
141 my($package, @args) = @_;
143 my $spec = $SPEC{$package}
144 || confess("The package $package package does not use Mouse::Exporter");
146 my $into = _get_caller_package(ref($args[0]) ? shift @args : undef);
152 my $arg = shift @args;
154 if($arg eq 'traits'){
155 push @traits, ref($args[0]) ? @{shift(@args)} : shift(@args);
158 Mouse::Util::not_supported("-$arg");
161 elsif($arg =~ s/^://){
162 my $group = $spec->{groups}{$arg}
163 || confess(qq{The $package package does not export the group "$arg"});
164 push @exports, @{$group};
171 $^H |= $strict_bits; # strict->import;
172 ${^WARNING_BITS} |= $warnings::Bits{all}; # warnings->import;
174 if($spec->{INIT_META}){
176 foreach my $init_meta(@{$spec->{INIT_META}}){
177 $meta = $package->$init_meta(for_class => $into);
181 my $type = (split /::/, ref $meta)[-1]; # e.g. "Class" for "My::Meta::Class"
185 : Mouse::Util::resolve_metaclass_alias($type => $_, trait => 1)
188 require Mouse::Util::MetaRole;
189 Mouse::Util::MetaRole::apply_metaroles(
191 Mouse::Util::is_a_metarole($into->meta)
192 ? (role_metaroles => { role => \@traits })
193 : (class_metaroles => { class => \@traits }),
198 Carp::confess("Cannot provide traits when $package does not have an init_meta() method");
203 foreach my $keyword(@exports){
205 $keyword => ($spec->{EXPORTS}{$keyword}
206 || confess(qq{The $package package does not export "$keyword"})
209 Mouse::Util::install_subroutines($into, @export_table);
212 Mouse::Util::install_subroutines($into, %{$spec->{DEFAULT}});
217 # the entity of general unimport()
219 my($package, $arg) = @_;
221 my $spec = $SPEC{$package}
222 || confess("The package $package does not use Mouse::Exporter");
224 my $from = _get_caller_package($arg);
231 for my $keyword (@{ $spec->{REMOVABLES} }) {
232 next if !exists $stash->{$keyword};
233 my $gv = \$stash->{$keyword};
234 if(ref($gv) eq 'GLOB' && *{$gv}{CODE} == $spec->{EXPORTS}{$keyword}){ # make sure it is from us
235 delete $stash->{$keyword};
241 sub _get_caller_package {
244 # We need one extra level because it's called by import so there's a layer
247 return defined($arg->{into}) ? $arg->{into}
248 : defined($arg->{into_level}) ? scalar caller(1 + $arg->{into_level})
252 return scalar caller(1);
263 Mouse::Exporter - make an import() and unimport() just like Mouse.pm
267 This document describes Mouse version 0.50_06
271 package MyApp::Mouse;
276 Mouse::Exporter->setup_import_methods(
277 as_is => [ 'has_rw', 'other_sugar', \&Some::Random::thing ],
282 my $meta = caller->meta;
283 my ( $name, %options ) = @_;
284 $meta->add_attribute(
304 This module encapsulates the exporting of sugar functions in a
305 C<Mouse.pm>-like manner. It does this by building custom C<import>,
306 C<unimport> methods for your module, based on a spec you provide.
308 Note that C<Mouse::Exporter> does not provide the C<with_meta> option,
309 but you can easily get the metaclass by C<< caller->meta >> as L</SYNOPSIS> shows.
313 =head2 C<< setup_import_methods( ARGS ) >>
315 =head2 C<< build_import_methods( ARGS ) -> (\&import, \&unimport) >>