1 package Mouse::Exporter;
10 BEGIN{ $strict_bits = strict::bits(qw(subs refs vars)); }
12 my $warnings_extra_bits;
13 BEGIN{ $warnings_extra_bits = warnings::bits(FATAL => 'recursion') }
15 # it must be "require", because Mouse::Util depends on Mouse::Exporter,
16 # which depends on Mouse::Util::import()
22 # warnings->import('all', FATAL => 'recursion');
23 ${^WARNING_BITS} |= $warnings::Bits{all};
24 ${^WARNING_BITS} |= $warnings_extra_bits;
29 sub setup_import_methods{
30 my($class, %args) = @_;
32 my $exporting_package = $args{exporting_package} ||= caller();
34 my($import, $unimport) = $class->build_import_methods(%args);
36 Mouse::Util::install_subroutines($exporting_package,
38 unimport => $unimport,
40 export_to_level => sub {
41 my($package, $level, undef, @args) = @_; # the third argument is redundant
43 Carp::carp("$package->export_to_level has been deprecated."
44 ." Use $package->import({ into_level => LEVEL }) instead");
45 $package->import({ into_level => $level + 1 }, @args);
48 my($package, $into, @args) = @_;
49 Carp::carp("$package->export has been deprecated."
50 ." Use $package->import({ into => PACKAGE }) instead");
51 $package->import({ into => $into }, @args);
57 sub build_import_methods{
58 my($self, %args) = @_;
60 my $exporting_package = $args{exporting_package} ||= caller();
62 $SPEC{$exporting_package} = \%args;
68 my @stack = ($exporting_package);
70 while(my $current = shift @stack){
71 push @export_from, $current;
73 my $also = $SPEC{$current}{also} or next;
74 push @stack, grep{ !$seen{$_}++ } ref($also) ? @{ $also } : $also;
78 @export_from = ($exporting_package);
85 my @init_meta_methods;
87 foreach my $package(@export_from){
88 my $spec = $SPEC{$package} or next;
90 if(my $as_is = $spec->{as_is}){
91 foreach my $thingy (@{$as_is}){
92 my($code_package, $code_name, $code);
96 ($code_package, $code_name) = Mouse::Util::get_code_info($code);
99 $code_package = $package;
100 $code_name = $thingy;
102 $code = \&{ $code_package . '::' . $code_name };
105 push @all, $code_name;
106 $exports{$code_name} = $code;
107 if($code_package eq $package){
108 push @removables, $code_name;
113 if(my $init_meta = $package->can('init_meta')){
114 if(!grep{ $_ == $init_meta } @init_meta_methods){
115 push @init_meta_methods, $init_meta;
119 $args{EXPORTS} = \%exports;
120 $args{REMOVABLES} = \@removables;
122 $args{groups}{all} ||= \@all;
124 if(my $default_list = $args{groups}{default}){
126 foreach my $keyword(@{$default_list}){
127 $default{$keyword} = $exports{$keyword}
128 || confess(qq{The $exporting_package package does not export "$keyword"});
130 $args{DEFAULT} = \%default;
133 $args{groups}{default} ||= \@all;
134 $args{DEFAULT} = $args{EXPORTS};
137 if(@init_meta_methods){
138 $args{INIT_META} = \@init_meta_methods;
141 return (\&do_import, \&do_unimport);
145 # the entity of general import()
147 my($package, @args) = @_;
149 my $spec = $SPEC{$package}
150 || confess("The package $package package does not use Mouse::Exporter");
152 my $into = _get_caller_package(ref($args[0]) ? shift @args : undef);
158 my $arg = shift @args;
160 if($arg eq 'traits'){
161 push @traits, ref($args[0]) ? @{shift(@args)} : shift(@args);
164 Mouse::Util::not_supported("-$arg");
167 elsif($arg =~ s/^://){
168 my $group = $spec->{groups}{$arg}
169 || confess(qq{The $package package does not export the group "$arg"});
170 push @exports, @{$group};
177 $^H |= $strict_bits; # strict->import;
178 # warnings->import('all', FATAL => 'recursion');
179 ${^WARNING_BITS} |= $warnings::Bits{all};
180 ${^WARNING_BITS} |= $warnings_extra_bits;
182 if($spec->{INIT_META}){
184 foreach my $init_meta(@{$spec->{INIT_META}}){
185 $meta = $package->$init_meta(for_class => $into);
189 my $type = (split /::/, ref $meta)[-1]; # e.g. "Class" for "My::Meta::Class"
193 : Mouse::Util::resolve_metaclass_alias($type => $_, trait => 1)
196 require Mouse::Util::MetaRole;
197 Mouse::Util::MetaRole::apply_metaroles(
199 Mouse::Util::is_a_metarole($into->meta)
200 ? (role_metaroles => { role => \@traits })
201 : (class_metaroles => { class => \@traits }),
206 Carp::confess("Cannot provide traits when $package does not have an init_meta() method");
211 foreach my $keyword(@exports){
213 $keyword => ($spec->{EXPORTS}{$keyword}
214 || confess(qq{The $package package does not export "$keyword"})
217 Mouse::Util::install_subroutines($into, @export_table);
220 Mouse::Util::install_subroutines($into, %{$spec->{DEFAULT}});
225 # the entity of general unimport()
227 my($package, $arg) = @_;
229 my $spec = $SPEC{$package}
230 || confess("The package $package does not use Mouse::Exporter");
232 my $from = _get_caller_package($arg);
239 for my $keyword (@{ $spec->{REMOVABLES} }) {
240 next if !exists $stash->{$keyword};
241 my $gv = \$stash->{$keyword};
242 if(ref($gv) eq 'GLOB' && *{$gv}{CODE} == $spec->{EXPORTS}{$keyword}){ # make sure it is from us
243 delete $stash->{$keyword};
249 sub _get_caller_package {
252 # We need one extra level because it's called by import so there's a layer
255 return defined($arg->{into}) ? $arg->{into}
256 : defined($arg->{into_level}) ? scalar caller(1 + $arg->{into_level})
260 return scalar caller(1);
271 Mouse::Exporter - make an import() and unimport() just like Mouse.pm
275 This document describes Mouse version 0.50_08
279 package MyApp::Mouse;
284 Mouse::Exporter->setup_import_methods(
285 as_is => [ 'has_rw', 'other_sugar', \&Some::Random::thing ],
290 my $meta = caller->meta;
291 my ( $name, %options ) = @_;
292 $meta->add_attribute(
312 This module encapsulates the exporting of sugar functions in a
313 C<Mouse.pm>-like manner. It does this by building custom C<import>,
314 C<unimport> methods for your module, based on a spec you provide.
316 Note that C<Mouse::Exporter> does not provide the C<with_meta> option,
317 but you can easily get the metaclass by C<< caller->meta >> as L</SYNOPSIS> shows.
321 =head2 C<< setup_import_methods( ARGS ) >>
323 =head2 C<< build_import_methods( ARGS ) -> (\&import, \&unimport) >>