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