1 package Mouse::Exporter;
9 my $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 $SPEC{$exporting_package} = \%args;
33 my @stack = ($exporting_package);
35 while(my $current = shift @stack){
36 push @export_from, $current;
38 my $also = $SPEC{$current}{also} or next;
39 push @stack, grep{ !$seen{$_}++ } ref($also) ? @{ $also } : $also;
43 @export_from = ($exporting_package);
51 my @init_meta_methods;
53 foreach my $package(@export_from){
54 my $spec = $SPEC{$package} or next;
56 if(my $as_is = $spec->{as_is}){
57 foreach my $thingy (@{$as_is}){
58 my($code_package, $code_name, $code);
62 ($code_package, $code_name) = Mouse::Util::get_code_info($code);
66 $code_package = $package;
68 $code = \&{ $code_package . '::' . $code_name };
71 push @all, $code_name;
72 $exports{$code_name} = $code;
73 if($code_package eq $package){
74 push @removables, $code_name;
79 if(my $init_meta = $package->can('init_meta')){
80 if(!grep{ $_ == $init_meta } @init_meta_methods){
81 unshift @init_meta_methods, $init_meta;
85 $args{EXPORTS} = \%exports;
86 $args{REMOVABLES} = \@removables;
88 $args{groups}{all} ||= \@all;
90 if(my $default_list = $args{groups}{default}){
92 foreach my $keyword(@{$default_list}){
93 $default{$keyword} = $exports{$keyword}
94 || confess(qq{The $exporting_package package does not export "$keyword"});
96 $args{DEFAULT} = \%default;
99 $args{groups}{default} ||= \@all;
100 $args{DEFAULT} = $args{EXPORTS};
103 if(@init_meta_methods){
104 $args{INIT_META} = \@init_meta_methods;
110 *{$exporting_package . '::import'} = \&do_import;
111 *{$exporting_package . '::unimport'} = \&do_unimport;
113 # for backward compatibility
115 *{$exporting_package . '::export_to_level'} = sub{
116 my($package, $level, undef, @args) = @_; # the third argument is redundant
117 do_import($package, { into_level => $level + 1 }, @args);
119 *{$exporting_package . '::export'} = sub{
120 my($package, $into, @args) = @_;
121 do_import($package, { into => $into }, @args);
128 # the entity of general import()
130 my($package, @args) = @_;
132 my $spec = $SPEC{$package}
133 || confess("The package $package package does not use Mouse::Exporter");
135 my $into = _get_caller_package(ref($args[0]) ? shift @args : undef);
139 foreach my $arg(@args){
141 Mouse::Util::not_supported("-$arg");
143 elsif($arg =~ s/^://){
144 my $group = $spec->{groups}{$arg}
145 || confess(qq{The $package package does not export the group "$arg"});
146 push @exports, @{$group};
153 $^H |= $strict_bits; # strict->import;
154 ${^WARNING_BITS} = $warnings::Bits{all}; # warnings->import;
156 if($into eq 'main' && !$spec->{_export_to_main}){
157 warn qq{$package does not export its sugar to the 'main' package.\n};
161 if($spec->{INIT_META}){
162 foreach my $init_meta(@{$spec->{INIT_META}}){
163 $into->$init_meta(for_class => $into);
166 # _apply_meta_traits($into); # TODO
170 foreach my $keyword(@exports){
172 *{$into.'::'.$keyword} = $spec->{EXPORTS}{$keyword}
173 || confess(qq{The $package package does not export "$keyword"});
177 my $default = $spec->{DEFAULT};
178 while(my($keyword, $code) = each %{$default}){
180 *{$into.'::'.$keyword} = $code;
186 # the entity of general unimport()
188 my($package, $arg) = @_;
190 my $spec = $SPEC{$package}
191 || confess("The package $package does not use Mouse::Exporter");
193 my $from = _get_caller_package($arg);
200 for my $keyword (@{ $spec->{REMOVABLES} }) {
201 my $gv = \$stash->{$keyword};
202 if(ref($gv) eq 'GLOB' && *{$gv}{CODE} == $spec->{EXPORTS}{$keyword}){ # make sure it is from us
203 delete $stash->{$keyword};
209 # 1 extra level because it's called by import so there's a layer
\r
213 sub _get_caller_package {
217 return defined($arg->{into}) ? $arg->{into}
218 : defined($arg->{into_level}) ? scalar caller(_LEVEL + $arg->{into_level})
219 : scalar caller(_LEVEL);
222 return scalar caller(_LEVEL);
232 Mouse::Exporter - make an import() and unimport() just like Mouse.pm
236 package MyApp::Mouse;
\r
239 use Mouse::Exporter;
\r
241 Mouse::Exporter->setup_import_methods(
\r
242 as_is => [ 'has_rw', 'other_sugar', \&Some::Random::thing ],
\r
247 my $meta = caller->meta;
\r
248 my ( $name, %options ) = @_;
\r
249 $meta->add_attribute(
\r
257 package MyApp::User;
\r
269 This module encapsulates the exporting of sugar functions in a
\r
270 C<Mouse.pm>-like manner. It does this by building custom C<import>,
\r
271 C<unimport> methods for your module, based on a spec you provide.
\r
273 Note that C<Mouse::Exporter> does not provide the C<with_meta> option,
274 but you can easily get the metaclass by C<< caller->meta >> as L</SYNOPSIS> shows.