Work around RT #55048 for Mouse::PurePerl
[gitmo/Mouse.git] / lib / Mouse / Exporter.pm
CommitLineData
db9fc237 1package Mouse::Exporter;
2use strict;
3use warnings;
4
5use Carp qw(confess);
6
db9fc237 7my %SPEC;
8
57633577 9my $strict_bits;
10BEGIN{ $strict_bits = strict::bits(qw(subs refs vars)); }
1ff34b4c 11
0eb86915 12# it must be "require", because Mouse::Util depends on Mouse::Exporter,
13# which depends on Mouse::Util::import()
14require Mouse::Util;
15
1bd3c83e 16sub import{
57633577 17 $^H |= $strict_bits; # strict->import;
4b55a023 18 ${^WARNING_BITS} |= $warnings::Bits{all}; # warnings->import;
1bd3c83e 19 return;
20}
21
0eb86915 22
db9fc237 23sub setup_import_methods{
24 my($class, %args) = @_;
25
26 my $exporting_package = $args{exporting_package} ||= caller();
27
e6dc493d 28 my($import, $unimport) = $class->build_import_methods(%args);
29
1194aede 30 Mouse::Util::install_subroutines($exporting_package,
31 import => $import,
32 unimport => $unimport,
33
34 export_to_level => sub {
35 my($package, $level, undef, @args) = @_; # the third argument is redundant
e57a5068 36
37 Carp::carp("$package->export_to_level has been deprecated."
38 ." Use $package->import({ into_level => LEVEL }) instead");
1194aede 39 $package->import({ into_level => $level + 1 }, @args);
40 },
41 export => sub {
42 my($package, $into, @args) = @_;
e57a5068 43 Carp::carp("$package->export has been deprecated."
44 ." Use $package->import({ into => PACKAGE }) instead");
1194aede 45 $package->import({ into => $into }, @args);
46 },
47 );
e6dc493d 48 return;
49}
50
51sub build_import_methods{
52 my($class, %args) = @_;
53
54 my $exporting_package = $args{exporting_package} ||= caller();
55
db9fc237 56 $SPEC{$exporting_package} = \%args;
57
58 # canonicalize args
59 my @export_from;
60 if($args{also}){
61 my %seen;
62 my @stack = ($exporting_package);
63
64 while(my $current = shift @stack){
65 push @export_from, $current;
66
67 my $also = $SPEC{$current}{also} or next;
0eb86915 68 push @stack, grep{ !$seen{$_}++ } ref($also) ? @{ $also } : $also;
db9fc237 69 }
70 }
71 else{
72 @export_from = ($exporting_package);
73 }
74
72dc2c9d 75 my %exports;
76 my @removables;
77 my @all;
78
79 my @init_meta_methods;
80
81 foreach my $package(@export_from){
82 my $spec = $SPEC{$package} or next;
1ff34b4c 83
72dc2c9d 84 if(my $as_is = $spec->{as_is}){
85 foreach my $thingy (@{$as_is}){
86 my($code_package, $code_name, $code);
87
88 if(ref($thingy)){
89 $code = $thingy;
90 ($code_package, $code_name) = Mouse::Util::get_code_info($code);
91 }
92 else{
72dc2c9d 93 $code_package = $package;
94 $code_name = $thingy;
1194aede 95 no strict 'refs';
72dc2c9d 96 $code = \&{ $code_package . '::' . $code_name };
97 }
98
99 push @all, $code_name;
100 $exports{$code_name} = $code;
101 if($code_package eq $package){
102 push @removables, $code_name;
1ff34b4c 103 }
104 }
db9fc237 105 }
1ff34b4c 106
72dc2c9d 107 if(my $init_meta = $package->can('init_meta')){
108 if(!grep{ $_ == $init_meta } @init_meta_methods){
109 push @init_meta_methods, $init_meta;
1ff34b4c 110 }
1ff34b4c 111 }
72dc2c9d 112 }
113 $args{EXPORTS} = \%exports;
114 $args{REMOVABLES} = \@removables;
1ff34b4c 115
72dc2c9d 116 $args{groups}{all} ||= \@all;
117
118 if(my $default_list = $args{groups}{default}){
119 my %default;
120 foreach my $keyword(@{$default_list}){
121 $default{$keyword} = $exports{$keyword}
122 || confess(qq{The $exporting_package package does not export "$keyword"});
1ff34b4c 123 }
72dc2c9d 124 $args{DEFAULT} = \%default;
125 }
126 else{
127 $args{groups}{default} ||= \@all;
128 $args{DEFAULT} = $args{EXPORTS};
129 }
130
131 if(@init_meta_methods){
132 $args{INIT_META} = \@init_meta_methods;
db9fc237 133 }
134
e6dc493d 135 return (\&do_import, \&do_unimport);
db9fc237 136}
137
db9fc237 138
139# the entity of general import()
140sub do_import {
1ff34b4c 141 my($package, @args) = @_;
db9fc237 142
1ff34b4c 143 my $spec = $SPEC{$package}
144 || confess("The package $package package does not use Mouse::Exporter");
db9fc237 145
146 my $into = _get_caller_package(ref($args[0]) ? shift @args : undef);
147
148 my @exports;
45bbec05 149 my @traits;
8cbcbb47 150
45bbec05 151 while(@args){
152 my $arg = shift @args;
1ff34b4c 153 if($arg =~ s/^-//){
45bbec05 154 if($arg eq 'traits'){
f12892e5 155 push @traits, ref($args[0]) ? @{shift(@args)} : shift(@args);
45bbec05 156 }
157 else {
158 Mouse::Util::not_supported("-$arg");
159 }
1ff34b4c 160 }
161 elsif($arg =~ s/^://){
1bd3c83e 162 my $group = $spec->{groups}{$arg}
1ff34b4c 163 || confess(qq{The $package package does not export the group "$arg"});
db9fc237 164 push @exports, @{$group};
165 }
166 else{
167 push @exports, $arg;
168 }
169 }
170
57633577 171 $^H |= $strict_bits; # strict->import;
4b55a023 172 ${^WARNING_BITS} |= $warnings::Bits{all}; # warnings->import;
db9fc237 173
1ff34b4c 174 if($spec->{INIT_META}){
45bbec05 175 my $meta;
1ff34b4c 176 foreach my $init_meta(@{$spec->{INIT_META}}){
134f7bcb 177 $meta = $package->$init_meta(for_class => $into);
1ff34b4c 178 }
db9fc237 179
45bbec05 180 if(@traits){
181 my $type = (split /::/, ref $meta)[-1]; # e.g. "Class" for "My::Meta::Class"
182 @traits =
f12892e5 183 map{
184 ref($_) ? $_
185 : Mouse::Util::resolve_metaclass_alias($type => $_, trait => 1)
186 } @traits;
45bbec05 187
45bbec05 188 require Mouse::Util::MetaRole;
3d7c6ec9 189 Mouse::Util::MetaRole::apply_metaroles(
190 for => $into,
191 Mouse::Util::is_a_metarole($into->meta)
192 ? (role_metaroles => { role => \@traits })
193 : (class_metaroles => { class => \@traits }),
45bbec05 194 );
195 }
db9fc237 196 }
f12892e5 197 elsif(@traits){
198 Carp::confess("Cannot provide traits when $package does not have an init_meta() method");
199 }
db9fc237 200
1ff34b4c 201 if(@exports){
1194aede 202 my @export_table;
1ff34b4c 203 foreach my $keyword(@exports){
1194aede 204 push @export_table,
205 $keyword => ($spec->{EXPORTS}{$keyword}
206 || confess(qq{The $package package does not export "$keyword"})
207 );
1ff34b4c 208 }
1194aede 209 Mouse::Util::install_subroutines($into, @export_table);
1ff34b4c 210 }
211 else{
1194aede 212 Mouse::Util::install_subroutines($into, %{$spec->{DEFAULT}});
db9fc237 213 }
214 return;
215}
216
217# the entity of general unimport()
218sub do_unimport {
1ff34b4c 219 my($package, $arg) = @_;
db9fc237 220
1ff34b4c 221 my $spec = $SPEC{$package}
222 || confess("The package $package does not use Mouse::Exporter");
db9fc237 223
224 my $from = _get_caller_package($arg);
225
226 my $stash = do{
227 no strict 'refs';
228 \%{$from . '::'}
229 };
230
231 for my $keyword (@{ $spec->{REMOVABLES} }) {
21df8e42 232 next if !exists $stash->{$keyword};
4ee3190e 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};
236 }
db9fc237 237 }
238 return;
239}
240
241sub _get_caller_package {
242 my($arg) = @_;
243
7ad9df77 244 # We need one extra level because it's called by import so there's a layer
245 # of indirection
db9fc237 246 if(ref $arg){
247 return defined($arg->{into}) ? $arg->{into}
7ad9df77 248 : defined($arg->{into_level}) ? scalar caller(1 + $arg->{into_level})
249 : scalar caller(1);
db9fc237 250 }
251 else{
7ad9df77 252 return scalar caller(1);
db9fc237 253 }
254}
255
99934a1b 256#sub _spec{ %SPEC }
257
db9fc237 2581;
db9fc237 259__END__
260
261=head1 NAME
262
8cbcbb47 263Mouse::Exporter - make an import() and unimport() just like Mouse.pm
db9fc237 264
a25ca8d6 265=head1 VERSION
266
98a8fd12 267This document describes Mouse version 0.50_05
a25ca8d6 268
db9fc237 269=head1 SYNOPSIS
270
7ad9df77 271 package MyApp::Mouse;
272
273 use Mouse ();
274 use Mouse::Exporter;
275
276 Mouse::Exporter->setup_import_methods(
277 as_is => [ 'has_rw', 'other_sugar', \&Some::Random::thing ],
278 also => 'Mouse',
279 );
280
8cbcbb47 281 sub has_rw {
7ad9df77 282 my $meta = caller->meta;
283 my ( $name, %options ) = @_;
284 $meta->add_attribute(
285 $name,
286 is => 'rw',
287 %options,
288 );
289 }
290
291 # then later ...
292 package MyApp::User;
293
294 use MyApp::Mouse;
295
296 has 'name';
297 has_rw 'size';
298 thing;
299
8cbcbb47 300 no MyApp::Mouse;
db9fc237 301
302=head1 DESCRIPTION
303
7ad9df77 304This module encapsulates the exporting of sugar functions in a
305C<Mouse.pm>-like manner. It does this by building custom C<import>,
306C<unimport> methods for your module, based on a spec you provide.
8cbcbb47 307
308Note that C<Mouse::Exporter> does not provide the C<with_meta> option,
309but you can easily get the metaclass by C<< caller->meta >> as L</SYNOPSIS> shows.
db9fc237 310
e6dc493d 311=head1 METHODS
312
313=head2 C<< setup_import_methods( ARGS ) >>
314
315=head2 C<< build_import_methods( ARGS ) -> (\&import, \&unimport) >>
316
db9fc237 317=head1 SEE ALSO
318
319L<Moose::Exporter>
320
5ea1528f 321=cut
322