Some ClassName simplification and fixes
[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::Meta::Attribute;
13 use Mouse::Meta::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::Meta::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::Meta::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::Meta::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     return if ref($class);
86     return unless defined($class) && length($class);
87
88     return 1 if is_class_loaded($class);
89
90     (my $file = "$class.pm") =~ s{::}{/}g;
91
92     eval { CORE::require($file) };
93     confess "Could not load class ($class) because : $@" if $@;
94
95     return 1;
96 }
97
98 sub is_class_loaded {
99     my $class = shift;
100
101     return 0 if ref($class) || !defined($class) || !length($class);
102
103     no strict 'refs';
104     return 1 if defined ${"${class}::VERSION"} || defined @{"${class}::ISA"};
105     foreach my $symbol (keys %{"${class}::"}) {
106             next if substr($symbol, -2, 2) eq '::';
107             return 1 if defined &{"${class}::${symbol}"};
108     }
109     return 0;
110 }
111
112 1;
113
114 __END__
115
116 =head1 NAME
117
118 Mouse - Moose minus antlers
119
120 =head1 VERSION
121
122 Version 0.01 released ???
123
124 =head1 SYNOPSIS
125
126     package Point;
127     use Mouse; # automatically turns on strict and warnings
128
129     has 'x' => (is => 'rw', isa => 'Int');
130     has 'y' => (is => 'rw', isa => 'Int');
131
132     sub clear {
133         my $self = shift;
134         $self->x(0);
135         $self->y(0);
136     }
137
138     package Point3D;
139     use Mouse;
140
141     extends 'Point';
142
143     has 'z' => (is => 'rw', isa => 'Int');
144
145     #after 'clear' => sub {
146     #    my $self = shift;
147     #    $self->z(0);
148     #};
149
150 =head1 DESCRIPTION
151
152 Moose.
153
154 =head1 INTERFACE
155
156 =head2 meta -> Mouse::Meta::Class
157
158 Returns this class' metaclass instance.
159
160 =head2 extends superclasses
161
162 Sets this class' superclasses.
163
164 =head2 has (name|names) => parameters
165
166 Adds an attribute (or if passed an arrayref of names, multiple attributes) to
167 this class.
168
169 =head2 confess error -> BOOM
170
171 L<Carp/confess> for your convenience.
172
173 =head2 blessed value -> ClassName | undef
174
175 L<Scalar::Util/blessed> for your convenience.
176
177 =head1 MISC
178
179 =head2 import
180
181 Importing Mouse will default your class' superclass list to L<Mouse::Object>.
182 You may use L</extends> to replace the superclass list.
183
184 =head2 unimport
185
186 Please unimport Mouse so that if someone calls one of the keywords (such as
187 L</extends>) it will break loudly instead breaking subtly.
188
189 =head1 FUNCTIONS
190
191 =head2 load_class Class::Name
192
193 This will load a given C<Class::Name> (or die if it's not loadable).
194 This function can be used in place of tricks like
195 C<eval "use $module"> or using C<require>.
196
197 =head1 AUTHOR
198
199 Shawn M Moore, C<< <sartak at gmail.com> >>
200
201 =head1 BUGS
202
203 No known bugs.
204
205 Please report any bugs through RT: email
206 C<bug-mouse at rt.cpan.org>, or browse
207 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mouse>.
208
209 =head1 COPYRIGHT AND LICENSE
210
211 Copyright 2008 Shawn M Moore.
212
213 This program is free software; you can redistribute it and/or modify it
214 under the same terms as Perl itself.
215
216 =cut
217