Moose compat: the details of load_class. Check whether the package has any methods...
[gitmo/Mouse.git] / lib / Mouse.pm
1 #!perl
2 package Mouse;
3 use strict;
4 use warnings;
5
6 our $VERSION = '0.01';
7
8 use Sub::Exporter;
9 use Carp 'confess';
10 use Scalar::Util 'blessed';
11
12 use Mouse::Attribute;
13 use Mouse::Class;
14 use Mouse::Object;
15 use Mouse::TypeRegistry;
16
17 do {
18     my $CALLER;
19
20     my %exports = (
21         meta => sub {
22             my $meta = Mouse::Class->initialize($CALLER);
23             return sub { $meta };
24         },
25
26         extends => sub {
27             my $caller = $CALLER;
28             return sub {
29                 $caller->meta->superclasses(@_);
30             };
31         },
32
33         has => sub {
34             return sub {
35                 my $package = caller;
36                 my $names = shift;
37                 $names = [$names] if !ref($names);
38
39                 for my $name (@$names) {
40                     Mouse::Attribute->create($package, $name, @_);
41                 }
42             };
43         },
44
45         confess => sub {
46             return \&confess;
47         },
48
49         blessed => sub {
50             return \&blessed;
51         },
52     );
53
54     my $exporter = Sub::Exporter::build_exporter({
55         exports => \%exports,
56         groups  => { default => [':all'] },
57     });
58
59     sub import {
60         $CALLER = caller;
61
62         strict->import;
63         warnings->import;
64
65         my $meta = Mouse::Class->initialize($CALLER);
66         $meta->superclasses('Mouse::Object')
67             unless $meta->superclasses;
68
69         goto $exporter;
70     }
71
72     sub unimport {
73         my $caller = caller;
74
75         no strict 'refs';
76         for my $keyword (keys %exports) {
77             next if $keyword eq 'meta'; # we don't delete this one
78             delete ${ $caller . '::' }{$keyword};
79         }
80     }
81 };
82
83 sub load_class {
84     my $class = shift;
85
86     return 1 if is_class_loaded($class);
87
88     (my $file = "$class.pm") =~ s{::}{/}g;
89
90     eval { CORE::require($file) };
91     confess "Could not load class ($class) because : $@" if $@;
92
93     return 1;
94 }
95
96 sub is_class_loaded {
97     my $class = shift;
98
99     no strict 'refs';
100     return 1 if defined ${"${class}::VERSION"} || defined @{"${class}::ISA"};
101     foreach my $symbol (keys %{"${class}::"}) {
102             next if substr($symbol, -2, 2) eq '::';
103             return 1 if defined &{"${class}::${symbol}"};
104     }
105     return 0;
106 }
107
108 1;
109
110 __END__
111
112 =head1 NAME
113
114 Mouse - Moose minus antlers
115
116 =head1 VERSION
117
118 Version 0.01 released ???
119
120 =head1 SYNOPSIS
121
122     package Point;
123     use Mouse; # automatically turns on strict and warnings
124
125     has 'x' => (is => 'rw', isa => 'Int');
126     has 'y' => (is => 'rw', isa => 'Int');
127
128     sub clear {
129         my $self = shift;
130         $self->x(0);
131         $self->y(0);
132     }
133
134     package Point3D;
135     use Mouse;
136
137     extends 'Point';
138
139     has 'z' => (is => 'rw', isa => 'Int');
140
141     #after 'clear' => sub {
142     #    my $self = shift;
143     #    $self->z(0);
144     #};
145
146 =head1 DESCRIPTION
147
148 Moose.
149
150 =head1 INTERFACE
151
152 =head2 meta -> Mouse::Class
153
154 Returns this class' metaclass instance.
155
156 =head2 extends superclasses
157
158 Sets this class' superclasses.
159
160 =head2 has (name|names) => parameters
161
162 Adds an attribute (or if passed an arrayref of names, multiple attributes) to
163 this class.
164
165 =head2 confess error -> BOOM
166
167 L<Carp/confess> for your convenience.
168
169 =head2 blessed value -> ClassName | undef
170
171 L<Scalar::Util/blessed> for your convenience.
172
173 =head1 MISC
174
175 =head2 import
176
177 Importing Mouse will default your class' superclass list to L<Mouse::Object>.
178 You may use L</extends> to replace the superclass list.
179
180 =head2 unimport
181
182 Please unimport Mouse so that if someone calls one of the keywords (such as
183 L</extends>) it will break loudly instead breaking subtly.
184
185 =head1 FUNCTIONS
186
187 =head2 load_class Class::Name
188
189 This will load a given C<Class::Name> (or die if it's not loadable).
190 This function can be used in place of tricks like
191 C<eval "use $module"> or using C<require>.
192
193 =head1 AUTHOR
194
195 Shawn M Moore, C<< <sartak at gmail.com> >>
196
197 =head1 BUGS
198
199 No known bugs.
200
201 Please report any bugs through RT: email
202 C<bug-mouse at rt.cpan.org>, or browse
203 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mouse>.
204
205 =head1 COPYRIGHT AND LICENSE
206
207 Copyright 2008 Shawn M Moore.
208
209 This program is free software; you can redistribute it and/or modify it
210 under the same terms as Perl itself.
211
212 =cut
213