Codebase list libany-moose-perl / 598a953
[svn-upgrade] Integrating new upstream version, libany-moose-perl (0.12) Jonathan Yu 14 years ago
14 changed file(s) with 292 addition(s) and 110 deletion(s). Raw diff Collapse all Expand all
0 0.12 Fri 02 Apr 2010
1 * t/000-version.t for better diagnostics (tokuhirom)
2 * Slight performance improvements for is_class_loaded,
3 lazily loading Carp, etc (Sartak)
4 * Start some real documentation (Sartak)
5 * Document $ENV{ANY_MOOSE} (Sartak)
6 - fixes [rt.cpan.org #52339]
7 * Test that Moose is loaded, not CMOP (Sartak)
8 - fixes [rt.cpan.org #56093]
9 * Alias class_of and more functions (Sartak)
10 - requested by [rt.cpan.org #52275]
11
012 0.11 Sun 19 Dec 2009
113 * Support "use Any::Moose '::Util::TypeConstraints'" (gfx)
214 * Support "no Any::Moose '::Util::TypeConstraints'" (gfx)
0 This software is copyright (c) 2009 by Best Practical Solutions.
0 This software is copyright (c) 2010 by Best Practical Solutions.
11
22 This is free software; you can redistribute it and/or modify it under
3 the same terms as perl itself.
4
5 Terms of Perl itself
3 the same terms as the Perl 5 programming language system itself.
4
5 Terms of the Perl programming language system itself
66
77 a) the GNU General Public License as published by the Free
88 Software Foundation; either version 1, or (at your option) any
1111
1212 --- The GNU General Public License, Version 1, February 1989 ---
1313
14 This software is Copyright (c) 2009 by Best Practical Solutions.
14 This software is Copyright (c) 2010 by Best Practical Solutions.
1515
1616 This is free software, licensed under:
1717
269269
270270 --- The Artistic License 1.0 ---
271271
272 This software is Copyright (c) 2009 by Best Practical Solutions.
272 This software is Copyright (c) 2010 by Best Practical Solutions.
273273
274274 This is free software, licensed under:
275275
55 README
66 dist.ini
77 lib/Any/Moose.pm
8 t/000-version.t
89 t/001-basic-moose.t
910 t/001-basic-mouse.t
1011 t/002-other-modules-moose.t
1213 t/003-is_moose_loaded.t
1314 t/004-x-modules-moose.t
1415 t/004-x-modules-mouse.t
15 t/005-is_class_loaded-moose.t
16 t/005-is_class_loaded-mouse.t
16 t/005-aliases-moose.t
17 t/005-aliases-mouse.t
1718 t/010-use_mouse_roles.t
1819 t/011-use_moose_roles.t
1920 t/012-use_moose_util.t
2122 t/020-strict.t
2223 t/100-canonicalize-fragments.t
2324 t/101-localize-errvar.t
25 t/lib/AnyMooseTest.pm
2426 t/release-pod-coverage.t
2527 t/release-pod-syntax.t
55 - 'Stevan Little <stevan@iinteractive.com>'
66 - 'Tokuhiro Matsuno <tokuhirom@gmail.com>'
77 - 'Goro Fuji <gfuji@cpan.org>'
8 generated_by: 'Dist::Zilla version 1.093400'
8 build_requires: {}
9 configure_requires:
10 ExtUtils::MakeMaker: 6.31
11 generated_by: 'Dist::Zilla version 2.100921'
912 license: perl
1013 meta-spec:
1114 url: http://module-build.sourceforge.net/META-spec-v1.4.html
1215 version: 1.4
1316 name: Any-Moose
17 recommends: {}
1418 requires:
1519 Mouse: 0.40
1620 perl: 5.006_002
17 version: 0.11
21 version: 0.12
33
44 BEGIN { require 5.006_002; }
55
6 use ExtUtils::MakeMaker;
6 use ExtUtils::MakeMaker 6.31;
77
8 WriteMakefile(
9 DISTNAME => 'Any-Moose',
10 NAME => 'Any::Moose',
11 AUTHOR => "Shawn\ M\ Moore\ \<sartak\@bestpractical\.com\>\,\ Florian\ Ragwitz\ \<rafl\@debian\.org\>\,\ Stevan\ Little\ \<stevan\@iinteractive\.com\>\,\ Tokuhiro\ Matsuno\ \<tokuhirom\@gmail\.com\>\,\ Goro\ Fuji\ \<gfuji\@cpan\.org\>",
12 ABSTRACT => "use\ Moose\ or\ Mouse\ modules",
13 VERSION => '0.11',
14 EXE_FILES => [ qw() ],
15 (eval { ExtUtils::MakeMaker->VERSION(6.31) } ? (LICENSE => 'perl') : ()),
16 PREREQ_PM => {
17 "Mouse" => '0.40',
18 },
19 test => {TESTS => 't/*.t'}
20 );
218
9
10 my %WriteMakefileArgs = (
11 'test' => {
12 'TESTS' => 't/*.t'
13 },
14 'NAME' => 'Any::Moose',
15 'DISTNAME' => 'Any-Moose',
16 'CONFIGURE_REQUIRES' => {
17 'ExtUtils::MakeMaker' => '6.31'
18 },
19 'AUTHOR' => 'Shawn M Moore <sartak@bestpractical.com>, Florian Ragwitz <rafl@debian.org>, Stevan Little <stevan@iinteractive.com>, Tokuhiro Matsuno <tokuhirom@gmail.com>, Goro Fuji <gfuji@cpan.org>',
20 'BUILD_REQUIRES' => {},
21 'ABSTRACT' => 'use Moose or Mouse modules',
22 'EXE_FILES' => [],
23 'VERSION' => '0.12',
24 'PREREQ_PM' => {
25 'Mouse' => '0.40'
26 },
27 'LICENSE' => 'perl'
28 );
29
30
31 delete $WriteMakefileArgs{BUILD_REQUIRES}
32 unless eval { ExtUtils::MakeMaker->VERSION(6.56) };
33
34 delete $WriteMakefileArgs{CONFIGURE_REQUIRES}
35 unless eval { ExtUtils::MakeMaker->VERSION(6.52) };
36
37 WriteMakefile(%WriteMakefileArgs);
38
39
40
00
11
2 This archive contains the distribution Any-Moose, version
3 0.11:
2 This archive contains the distribution Any-Moose,
3 version 0.12:
44
55 use Moose or Mouse modules
66
7 This software is copyright (c) 2009 by Best Practical Solutions.
7 This software is copyright (c) 2010 by Best Practical Solutions.
88
99 This is free software; you can redistribute it and/or modify it under
10 the same terms as perl itself.
10 the same terms as the Perl 5 programming language system itself.
1111
12
00 name = Any-Moose
1 version = 0.11
1 version = 0.12
22 author = Shawn M Moore <sartak@bestpractical.com>
33 author = Florian Ragwitz <rafl@debian.org>
44 author = Stevan Little <stevan@iinteractive.com>
00 package Any::Moose;
1 our $VERSION = '0.11';
1 BEGIN {
2 $Any::Moose::VERSION = '0.12';
3 }
24 # ABSTRACT: use Moose or Mouse modules
35
46 use 5.006_002;
57 use strict;
68 use warnings;
79
8 use Carp ();
9
10 our $PREFERRED = $ENV{'ANY_MOOSE'};
10 # decide which backend to use
11 our $PREFERRED;
12 do {
13 local $@;
14 if ($ENV{ANY_MOOSE}) {
15 $PREFERRED = $ENV{'ANY_MOOSE'};
16 warn "ANY_MOOSE is not set to Moose or Mouse"
17 unless $PREFERRED eq 'Moose'
18 || $PREFERRED eq 'Mouse';
19
20 # if we die here, then perl gives "unknown error" which doesn't tell
21 # you what the problem is at all. argh.
22 if ($PREFERRED eq 'Moose' && !eval { require Moose }) {
23 warn "\$ANY_MOOSE is set to Moose but we cannot load it";
24 }
25 elsif ($PREFERRED eq 'Mouse' && !eval { require Mouse }) {
26 warn "\$ANY_MOOSE is set to Mouse but we cannot load it";
27 }
28 }
29 elsif (_is_moose_loaded()) {
30 $PREFERRED = 'Moose';
31 }
32 elsif (eval { require Mouse }) {
33 $PREFERRED = 'Mouse';
34 }
35 elsif (eval { require Moose }) {
36 $PREFERRED = 'Moose';
37 }
38 else {
39 require Carp;
40 warn "Unable to locate Mouse or Moose in INC";
41 }
42 };
1143
1244 sub import {
1345 my $self = shift;
5486 . '$module->unimport();';
5587 $@;
5688 };
57 Carp::croak("Cannot unimport Any::Moose: $e") if $e;
89
90 if ($e) {
91 require Carp;
92 Carp::croak("Cannot unimport Any::Moose: $e");
93 }
94
5895 return;
5996 }
6097
115152 . '$module->import(@{ $options->{imports} });';
116153 $@;
117154 };
118 Carp::croak("Cannot import Any::Moose: $e") if $e;
155 if ($e) {
156 require Carp;
157 Carp::croak("Cannot import Any::Moose: $e");
158 }
119159 return;
120160 }
121161
134174
135175 return $fragment if $backer =~ /^Moose/;
136176
137 if (!$PREFERRED) {
138 local $@;
139 if (_is_moose_loaded()) {
140 $PREFERRED = 'Moose';
141 }
142 elsif (eval { require Mouse }) {
143 $PREFERRED = 'Mouse';
144 }
145 elsif (eval { require Moose }) {
146 $PREFERRED = 'Moose';
147 }
148 else {
149 require Carp;
150 Carp::confess("Unable to locate Mouse or Moose in INC");
151 }
152 }
153
154177 $fragment =~ s/^Moose/Mouse/ if mouse_is_preferred();
155178 return $fragment;
156179 }
157180
158 sub load_class {
159 my ($class_name) = @_;
160 return Class::MOP::load_class($class_name) if moose_is_preferred();
161 return Mouse::load_class($class_name);
162 }
163
164 sub is_class_loaded {
165 my ($class_name) = @_;
166 return Class::MOP::is_class_loaded($class_name) if moose_is_preferred();
167 return Mouse::is_class_loaded($class_name);
181 for my $name (qw/load_class is_class_loaded class_of get_metaclass_by_name get_all_metaclass_instances get_all_metaclass_names/) {
182 no strict 'refs';
183 *{__PACKAGE__."::$name"} = moose_is_preferred()
184 ? *{"Class::MOP::$name"}
185 : *{"Mouse::Util::$name"};
168186 }
169187
170188 sub moose_is_preferred { $PREFERRED eq 'Moose' }
171189 sub mouse_is_preferred { $PREFERRED eq 'Mouse' }
172190
173 sub _is_moose_loaded { exists $INC{'Class/MOP.pm'} }
191 sub _is_moose_loaded { exists $INC{'Moose.pm'} }
174192
175193 sub is_moose_loaded {
194 require Carp;
176195 Carp::carp("Any::Moose::is_moose_loaded is deprecated. Please use Any::Moose::moose_is_preferred instead");
177196 goto \&_is_moose_loaded;
178197 }
208227
209228 =head1 VERSION
210229
211 version 0.11
230 version 0.12
212231
213232 =head1 SYNOPSIS
214233
234
215235 =head2 BASIC
216236
237
217238 package Class;
218239
219 # uses Moose if it's loaded, Mouse otherwise
240 # uses Moose if it's loaded or demanded, Mouse otherwise
220241 use Any::Moose;
221242
222243 # cleans the namespace up
223244 no Any::Moose;
224245
225246 =head2 OTHER MODULES
247
226248
227249 package Other::Class;
228250 use Any::Moose;
231253 # Mouse::Util::TypeConstraints otherwise.
232254 use Any::Moose '::Util::TypeConstraints';
233255
256 =head2 ROLES
257
258
259 package My::Sorter;
260 use Any::Moose 'Role';
261
262 requires 'cmp';
263
234264 =head2 COMPLEX USAGE
265
235266
236267 package My::Meta::Class;
237268 use Any::Moose;
244275 '::Util' => ['does_role'],
245276 );
246277
247 # uses MouseX::Types
278 # uses MouseX::Types or MooseX::Types
248279 use Any::Moose 'X::Types';
249280
250281 # gives you the right class name depending on which Mo*se was loaded
252283
253284 =head1 DESCRIPTION
254285
255 Actual documentation is forthcoming, once we solidify all the bits of the API.
256 The examples above are very likely to continue working.
286
287 Though we recommend that people generally use L<Moose>, we accept that Moose
288 cannot yet be used for everything everywhere. People generally like the Moose
289 sugar, so many people use L<Mouse>, a lightweight replacement for parts of
290 Moose.
291
292
293 Because Mouse strives for compatibility with Moose, it's easy to substitute one
294 for the other. This module facilitates that substitution. By default, Mouse
295 will be provided to libraries, unless Moose is already loaded -or-
296 explicitly requested by the end-user. The end-user can force the decision
297 of which backend to use by setting the environment variable C<ANY_MOOSE> to
298 be C<Moose> or C<Mouse>.
299
300
301 Note that the decision of which backend to use is made only once, so that if
302 Any-Moose picks Mouse, then a third-party library loads Moose, anything else
303 that uses Any-Moose will continue to pick Mouse.
304
305
306 So, if you have to use L<Mouse>, please be considerate to the Moose fanboys
307 (like myself!) and use L<Any-Moose> instead. C<:)>
257308
258309 =head1 SEE ALSO
259310
311
260312 L<Moose>
261313
314
262315 L<Mouse>
316
317
318 L<Squirrel> - a deprecated first-stab at Any-Moose-like logic. Its biggest
319 fault was in making the decision of which backend to use every time it was
320 used, rather than just once.
263321
264322 =head1 AUTHORS
265323
271329
272330 =head1 COPYRIGHT AND LICENSE
273331
274 This software is copyright (c) 2009 by Best Practical Solutions.
332 This software is copyright (c) 2010 by Best Practical Solutions.
275333
276334 This is free software; you can redistribute it and/or modify it under
277 the same terms as perl itself.
335 the same terms as the Perl 5 programming language system itself.
278336
279337 =cut
280338
0 use strict;
1 use Test::More tests => 1;
2 use_ok 'Any::Moose';
3
4 show_version($_) for qw/
5 Mouse
6 MouseX::Types
7 Moose
8 Class::MOP
9 MooseX::Types
10 /;
11
12 sub show_version {
13 my $klass = shift;
14 if (eval "require $klass; 1;") {
15 no strict 'refs';
16 diag "$klass: " . ${"${klass}::VERSION"};
17 } else {
18 diag "$klass not found";
19 }
20 }
0 #!/usr/bin/env perl
1 use strict;
2 use warnings;
3 use Test::More;
4
5 BEGIN { delete $ENV{ANY_MOOSE} }
6
7 BEGIN {
8 eval 'require Moose;';
9 plan skip_all => 'Moose not available' if $@;
10 plan tests => 20;
11 }
12
13 package MyClass;
14 use Any::Moose;
15
16 package main;
17
18 ok(Any::Moose::is_class_loaded('Moose'), 'Moose is loaded');
19 ok(Any::Moose::is_class_loaded('MyClass'), 'MyClass is loaded');
20 ok(!Any::Moose::is_class_loaded('NonexistentPackage'), 'NonexistentPackage is not loaded');
21
22 push @INC, 't/lib';
23
24 ok(!Any::Moose::is_class_loaded('AnyMooseTest'), 'AnyMooseTest not yet loaded');
25 ok(Any::Moose::load_class('AnyMooseTest'), 'Loading AnyMooseTest');
26 ok(Any::Moose::is_class_loaded('AnyMooseTest'), 'AnyMooseTest now loaded');
27
28 is(eval { Any::Moose::load_class('NonexistentTest'); 1 }, undef, 'load_class on nonexistent module fails');
29 ok(!Any::Moose::is_class_loaded('NonexistentTest'), 'and it is still not loaded');
30
31 like(Any::Moose::class_of('MyClass'), qr/Moose::Meta::Class=HASH/, 'metaclass check');
32 is(Any::Moose::class_of('NonexistentTest'), undef, 'class_of nonexistent class is undef');
33 like(Any::Moose::class_of(MyClass->new), qr/Moose::Meta::Class=HASH/, 'metaclass check via object');
34
35 like(Any::Moose::get_metaclass_by_name('MyClass'), qr/Moose::Meta::Class=HASH/, 'metaclass check');
36 is(Any::Moose::get_metaclass_by_name('NonexistentTest'), undef, 'class_of nonexistent class is undef');
37 is(Any::Moose::get_metaclass_by_name(MyClass->new), undef, 'get_metaclass_by_name via object returns undef');
38
39 my %metaclasses = map { $_->name => $_ } Any::Moose::get_all_metaclass_instances();
40 is($metaclasses{MyClass}, MyClass->meta);
41 is($metaclasses{AnyMooseTest}, AnyMooseTest->meta);
42 is($metaclasses{NonexistentTest}, undef);
43
44 is((grep { $_ eq 'MyClass' } Any::Moose::get_all_metaclass_names()), 1);
45 is((grep { $_ eq 'AnyMooseTest' } Any::Moose::get_all_metaclass_names()), 1);
46 is((grep { $_ eq 'NonexistentTest' } Any::Moose::get_all_metaclass_names()), 0);
47
0 #!/usr/bin/env perl
1 use strict;
2 use warnings;
3 use Test::More;
4
5 BEGIN { delete $ENV{ANY_MOOSE} }
6
7 BEGIN {
8 eval 'require Mouse;';
9 plan skip_all => 'Mouse not available' if $@;
10 plan tests => 20;
11 }
12
13 package MyClass;
14 use Any::Moose;
15
16 package main;
17
18 ok(Any::Moose::is_class_loaded('Mouse'), 'Mouse is loaded');
19 ok(Any::Moose::is_class_loaded('MyClass'), 'MyClass is loaded');
20 ok(!Any::Moose::is_class_loaded('NonexistentPackage'), 'NonexistentPackage is not loaded');
21
22 push @INC, 't/lib';
23
24 ok(!Any::Moose::is_class_loaded('AnyMooseTest'), 'AnyMooseTest not yet loaded');
25 ok(Any::Moose::load_class('AnyMooseTest'), 'Loading AnyMooseTest');
26 ok(Any::Moose::is_class_loaded('AnyMooseTest'), 'AnyMooseTest now loaded');
27
28 is(eval { Any::Moose::load_class('NonexistentTest'); 1 }, undef, 'load_class on nonexistent module fails');
29 ok(!Any::Moose::is_class_loaded('NonexistentTest'), 'and it is still not loaded');
30
31 like(Any::Moose::class_of('MyClass'), qr/Mouse::Meta::Class=HASH/, 'metaclass check');
32 is(Any::Moose::class_of('NonexistentTest'), undef, 'class_of nonexistent class is undef');
33 like(Any::Moose::class_of(MyClass->new), qr/Mouse::Meta::Class=HASH/, 'metaclass check via object');
34
35 like(Any::Moose::get_metaclass_by_name('MyClass'), qr/Mouse::Meta::Class=HASH/, 'metaclass check');
36 is(Any::Moose::get_metaclass_by_name('NonexistentTest'), undef, 'class_of nonexistent class is undef');
37 is(Any::Moose::get_metaclass_by_name(MyClass->new), undef, 'get_metaclass_by_name via object returns undef');
38
39 my %metaclasses = map { $_->name => $_ } Any::Moose::get_all_metaclass_instances();
40 is($metaclasses{MyClass}, MyClass->meta);
41 is($metaclasses{AnyMooseTest}, AnyMooseTest->meta);
42 is($metaclasses{NonexistentTest}, undef);
43
44 is((grep { $_ eq 'MyClass' } Any::Moose::get_all_metaclass_names()), 1);
45 is((grep { $_ eq 'AnyMooseTest' } Any::Moose::get_all_metaclass_names()), 1);
46 is((grep { $_ eq 'NonexistentTest' } Any::Moose::get_all_metaclass_names()), 0);
47
+0
-18
t/005-is_class_loaded-moose.t less more
0 #!/usr/bin/env perl
1 use strict;
2 use warnings;
3 use Test::More;
4
5 BEGIN { delete $ENV{ANY_MOOSE} }
6
7 BEGIN {
8 eval 'require Moose;';
9 plan skip_all => 'Moose not available' if $@;
10 plan tests => 2;
11 }
12
13 package MyFoo;
14 use Any::Moose;
15 ::ok Any::Moose::is_class_loaded('Moose');
16 ::ok !Any::Moose::is_class_loaded('Meese');
17
+0
-22
t/005-is_class_loaded-mouse.t less more
0 #!/usr/bin/env perl
1 use strict;
2 use warnings;
3 use Test::More;
4
5 BEGIN { delete $ENV{ANY_MOOSE} }
6
7 BEGIN {
8 eval 'require Mouse';
9 plan skip_all => 'Mouse not available' if $@;
10 plan tests => 2;
11 }
12
13 {
14 package MyFoo;
15 use Any::Moose;
16 }
17
18 package main;
19 ok(Any::Moose::is_class_loaded('Mouse'));
20 ok(!Any::Moose::is_class_loaded('Meese'));
21
0 package AnyMooseTest;
1 use Any::Moose;
2
3 has an_attribute => (
4 is => 'ro',
5 );
6
7 1;
8