File Coverage

blib/lib/Any/Moose.pm
Criterion Covered Total %
statement 87 95 91.5
branch 29 36 80.5
condition 10 14 71.4
subroutine 15 16 93.7
pod 0 4 0.0
total 141 165 85.4


line stmt bran cond sub pod time code
1             package Any::Moose; # git description: v0.26-20-g4c2f951
2             # ABSTRACT: (DEPRECATED) use Moo instead!
3             # KEYWORDS: deprecated Moose Mouse abstraction layer object-oriented
4             # vim: set ts=8 sts=4 sw=4 tw=115 et :
5              
6 15     15   3014498 use 5.006_002;
  15         61  
7 15     15   81 use strict;
  15         37  
  15         428  
8 15     15   70 use warnings;
  15         39  
  15         6035  
9              
10             our $VERSION = '0.27';
11              
12             warnings::warnif('deprecated', 'Any::Moose is deprecated. Please use Moo instead');
13              
14             # decide which backend to use
15             our $PREFERRED;
16             do {
17             local $@;
18             if ($ENV{ANY_MOOSE}) {
19             $PREFERRED = $ENV{'ANY_MOOSE'};
20             warn "ANY_MOOSE is not set to Moose or Mouse"
21             unless $PREFERRED eq 'Moose'
22             || $PREFERRED eq 'Mouse';
23              
24             # if we die here, then perl gives "unknown error" which doesn't tell
25             # you what the problem is at all. argh.
26             if ($PREFERRED eq 'Moose' && !eval { require Moose }) {
27             warn "\$ANY_MOOSE is set to Moose but we cannot load it";
28             }
29             elsif ($PREFERRED eq 'Mouse' && !eval { require Mouse }) {
30             warn "\$ANY_MOOSE is set to Mouse but we cannot load it";
31             }
32             }
33             elsif ( $INC{'Moo.pm'} && eval { require Moose } ) {
34             $PREFERRED = 'Moose';
35             }
36             elsif (_is_moose_loaded()) {
37             $PREFERRED = 'Moose';
38             }
39             elsif (eval { require Mouse; require Mouse::Util; Mouse::Util->can('get_metaclass_by_name') }) {
40             $PREFERRED = 'Mouse';
41             }
42             elsif (eval { require Moose }) {
43             $PREFERRED = 'Moose';
44             }
45             else {
46             require Carp;
47             warn "Unable to locate Mouse or Moose in INC";
48             }
49             };
50              
51             sub import {
52 26     26   7926 my $self = shift;
53 26         61 my $pkg = caller;
54              
55             # Any::Moose gives you strict and warnings
56 26         171 strict->import;
57 26         250 warnings->import;
58              
59             # first options are for Mo*se
60 26 100 66     227 unshift @_, 'Moose' if !@_ || ref($_[0]);
61              
62 26         90 while (my $module = shift) {
63 26 100 66     132 my $options = @_ && ref($_[0]) ? shift : [];
64              
65 26         85 $options = $self->_canonicalize_options(
66             module => $module,
67             options => $options,
68             package => $pkg,
69             );
70              
71 26         86 $self->_install_module($options);
72             }
73              
74             # give them any_moose too
75 15     15   85 no strict 'refs';
  15         27  
  15         12018  
76 26         54 *{$pkg.'::any_moose'} = \&any_moose;
  26         17534  
77             }
78              
79             sub unimport {
80 7     7   490 my $sel = shift;
81 7         13 my $pkg = caller;
82 7         10 my $module;
83              
84 7 100       25 if(@_){
85 2         5 $module = any_moose(shift, $pkg);
86             }
87             else {
88 5         12 $module = _backer_of($pkg);
89             }
90 7         13 my $e = do{
91 7         21 local $@;
92 7         456 eval "package $pkg;\n"
93             . '$module->unimport();';
94 7         726 $@;
95             };
96              
97 7 50       26 if ($e) {
98 0         0 require Carp;
99 0         0 Carp::croak("Cannot unimport Any::Moose: $e");
100             }
101              
102 7         8253 return;
103             }
104              
105             sub _backer_of {
106 44     44   58 my $pkg = shift;
107              
108 44 100 66     357 if(exists $INC{'Mouse.pm'}
109             and Mouse::Util->can('get_metaclass_by_name')
110             ){
111 25         105 my $meta = Mouse::Util::get_metaclass_by_name($pkg);
112 25 100       133 if ($meta) {
113 11 50       90 return 'Mouse::Role' if $meta->isa('Mouse::Meta::Role');
114 11 50       69 return 'Mouse' if $meta->isa('Mouse::Meta::Class');
115             }
116             }
117              
118 33 100       71 if (_is_moose_loaded()) {
119 19         59 my $meta = Class::MOP::get_metaclass_by_name($pkg);
120 19 100       93 if ($meta) {
121 10 50       72 return 'Moose::Role' if $meta->isa('Moose::Meta::Role');
122 10 50       52 return 'Moose' if $meta->isa('Moose::Meta::Class');
123             }
124             }
125              
126 23         138 return undef;
127             }
128              
129             sub _canonicalize_options {
130 26     26   39 my $self = shift;
131 26         102 my %args = @_;
132              
133 26         53 my %options;
134 26 50       90 if (ref($args{options}) eq 'HASH') {
135 0         0 %options = %{ $args{options} };
  0         0  
136             }
137             else {
138             %options = (
139             imports => $args{options},
140 26         87 );
141             }
142              
143 26         52 $options{package} = $args{package};
144 26         71 $options{module} = any_moose($args{module}, $args{package});
145              
146 26         89 return \%options;
147             }
148              
149             sub _install_module {
150 26     26   53 my $self = shift;
151 26         35 my $options = shift;
152              
153 26         39 my $module = $options->{module};
154 26         93 (my $file = $module . '.pm') =~ s{::}{/}g;
155              
156 26         1487 require $file;
157              
158 26         6618 my $e = do {
159 26         35 local $@;
160 26         2446 eval "package $options->{package};\n"
161             . '$module->import(@{ $options->{imports} });';
162 26         48700 $@;
163             };
164 26 50       91 if ($e) {
165 0         0 require Carp;
166 0         0 Carp::croak("Cannot import Any::Moose: $e");
167             }
168 26         145 return;
169             }
170              
171             sub any_moose {
172 39     39 0 413073 my $fragment = _canonicalize_fragment(shift);
173 39   66     150 my $package = shift || caller;
174              
175             # Mouse gets first dibs because it doesn't introspect existing classes
176              
177 39   100     106 my $backer = _backer_of($package) || '';
178              
179 39 100       157 if ($backer =~ /^Mouse/) {
180 8         35 $fragment =~ s/^Moose/Mouse/;
181 8         38 return $fragment;
182             }
183              
184 31 100       131 return $fragment if $backer =~ /^Moose/;
185              
186 23 100       51 $fragment =~ s/^Moose/Mouse/ if mouse_is_preferred();
187 23         65 return $fragment;
188             }
189              
190             for my $name (qw/
191             load_class
192             is_class_loaded
193             class_of
194             get_metaclass_by_name
195             get_all_metaclass_instances
196             get_all_metaclass_names
197             load_first_existing_class
198             /) {
199 15     15   95 no strict 'refs';
  15         24  
  15         5193  
200             *{__PACKAGE__."::$name"} = moose_is_preferred()
201             ? *{"Class::MOP::$name"}
202             : *{"Mouse::Util::$name"};
203             }
204              
205 105     105 0 228 sub moose_is_preferred { $PREFERRED eq 'Moose' }
206 23     23 0 129 sub mouse_is_preferred { $PREFERRED eq 'Mouse' }
207              
208 50     50   576077 sub _is_moose_loaded { exists $INC{'Moose.pm'} }
209              
210             sub is_moose_loaded {
211 0     0 0 0 warnings::warnif('deprecated', "Any::Moose::is_moose_loaded is deprecated. Please use Any::Moose::moose_is_preferred instead");
212 0         0 goto \&_is_moose_loaded;
213             }
214              
215             sub _canonicalize_fragment {
216 52     52   6337 my $fragment = shift;
217              
218 52 100       149 return 'Moose' if !$fragment;
219              
220             # any_moose("X::Types") -> any_moose("MooseX::Types")
221 44         94 $fragment =~ s/^X::/MooseX::/;
222              
223             # any_moose("::Util") -> any_moose("Moose::Util")
224 44         124 $fragment =~ s/^::/Moose::/;
225              
226             # any_moose("Mouse::Util") -> any_moose("Moose::Util")
227 44         98 $fragment =~ s/^Mouse(X?)\b/Moose$1/;
228              
229             # any_moose("Util") -> any_moose("Moose::Util")
230 44         119 $fragment =~ s/^(?!Moose)/Moose::/;
231              
232 44         83 return $fragment;
233             }
234              
235             1;
236              
237             __END__
238              
239             =pod
240              
241             =encoding UTF-8
242              
243             =head1 NAME
244              
245             Any::Moose - (DEPRECATED) use Moo instead!
246              
247             =head1 VERSION
248              
249             version 0.27
250              
251             =head1 DEPRECATION
252              
253             日本語翻訳: http://blog.64p.org/entry/2013/02/06/094906
254              
255             Please use L<Moo> instead of Any::Moose for new code.
256              
257             Moo classes and roles will transparently and correctly upgrade to
258             Moose when needed. This is a fundamentally better design than what
259             Any::Moose offers. Mouse metaclasses do not interact with Moose
260             metaclasses which leaks abstractions all over the place.
261              
262             Any::Moose had a good run. It was a simplistic but expedient answer
263             for getting Moose syntax on the cheap but with the transparent
264             upgrade path to Moose when you needed it. But really, please don't
265             use it any more. :)
266              
267             You may find L<MooX::late> useful for porting your code from
268             Any::Moose to Moo.
269              
270             For the sparse documentation Any::Moose used to include, see
271             L<https://metacpan.org/module/SARTAK/Any-Moose-0.18/lib/Any/Moose.pm>
272              
273             =head1 SUPPORT
274              
275             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Any-Moose>
276             (or L<bug-Any-Moose@rt.cpan.org|mailto:bug-Any-Moose@rt.cpan.org>).
277              
278             There is also a mailing list available for users of this distribution, at
279             L<http://lists.perl.org/list/moose.html>.
280              
281             There is also an irc channel available for users of this distribution, at
282             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
283              
284             =head1 AUTHORS
285              
286             =over 4
287              
288             =item *
289              
290             Shawn M Moore <code@sartak.org>
291              
292             =item *
293              
294             Florian Ragwitz <rafl@debian.org>
295              
296             =item *
297              
298             Stevan Little <stevan@iinteractive.com>
299              
300             =item *
301              
302             è—¤ 吾郎 (Fuji Goro) <gfuji@cpan.org>
303              
304             =back
305              
306             =head1 CONTRIBUTORS
307              
308             =for stopwords Karen Etheridge Tokuhiro Matsuno Ricardo Signes Graham Knop Stevan Little Chris 'BinGOs' Williams
309              
310             =over 4
311              
312             =item *
313              
314             Karen Etheridge <ether@cpan.org>
315              
316             =item *
317              
318             Tokuhiro Matsuno <tokuhirom@gmail.com>
319              
320             =item *
321              
322             Ricardo Signes <rjbs@cpan.org>
323              
324             =item *
325              
326             Graham Knop <haarg@haarg.org>
327              
328             =item *
329              
330             Stevan Little <stevan.little@iinteractive.com>
331              
332             =item *
333              
334             Chris 'BinGOs' Williams <chris@bingosnet.co.uk>
335              
336             =back
337              
338             =head1 COPYRIGHT AND LICENSE
339              
340             This software is copyright (c) 2009 by Best Practical Solutions.
341              
342             This is free software; you can redistribute it and/or modify it under
343             the same terms as the Perl 5 programming language system itself.
344              
345             =cut