Begin moving Moose::Attribute and Moose::Class into Moose::Meta::*
[gitmo/Mouse.git] / lib / Mouse.pm
CommitLineData
c3398f5b 1#!perl
2package Mouse;
3use strict;
4use warnings;
5
6our $VERSION = '0.01';
7
8use Sub::Exporter;
9use Carp 'confess';
10use Scalar::Util 'blessed';
11
12use Mouse::Attribute;
13use Mouse::Class;
14use Mouse::Object;
d60c78b9 15use Mouse::TypeRegistry;
c3398f5b 16
17do {
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 {
b17094ce 46 return \&confess;
c3398f5b 47 },
48
49 blessed => sub {
b17094ce 50 return \&blessed;
c3398f5b 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
ca73a208 65 my $meta = Mouse::Class->initialize($CALLER);
66 $meta->superclasses('Mouse::Object')
67 unless $meta->superclasses;
c3398f5b 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
83sub load_class {
84 my $class = shift;
85
2a674d23 86 return 1 if is_class_loaded($class);
87
c3398f5b 88 (my $file = "$class.pm") =~ s{::}{/}g;
89
90 eval { CORE::require($file) };
2a674d23 91 confess "Could not load class ($class) because : $@" if $@;
c3398f5b 92
93 return 1;
94}
95
2a674d23 96sub 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
c3398f5b 1081;
109
110__END__
111
112=head1 NAME
113
6caea456 114Mouse - Moose minus antlers
c3398f5b 115
116=head1 VERSION
117
118Version 0.01 released ???
119
120=head1 SYNOPSIS
121
122 package Point;
6caea456 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;
c3398f5b 135 use Mouse;
136
6caea456 137 extends 'Point';
c3398f5b 138
6caea456 139 has 'z' => (is => 'rw', isa => 'Int');
140
141 #after 'clear' => sub {
142 # my $self = shift;
143 # $self->z(0);
144 #};
c3398f5b 145
146=head1 DESCRIPTION
147
148Moose.
149
150=head1 INTERFACE
151
152=head2 meta -> Mouse::Class
153
154Returns this class' metaclass instance.
155
156=head2 extends superclasses
157
158Sets this class' superclasses.
159
160=head2 has (name|names) => parameters
161
162Adds an attribute (or if passed an arrayref of names, multiple attributes) to
163this class.
164
165=head2 confess error -> BOOM
166
167L<Carp/confess> for your convenience.
168
169=head2 blessed value -> ClassName | undef
170
171L<Scalar::Util/blessed> for your convenience.
172
173=head1 MISC
174
175=head2 import
176
6caea456 177Importing Mouse will default your class' superclass list to L<Mouse::Object>.
c3398f5b 178You may use L</extends> to replace the superclass list.
179
180=head2 unimport
181
182Please unimport Mouse so that if someone calls one of the keywords (such as
183L</extends>) it will break loudly instead breaking subtly.
184
185=head1 FUNCTIONS
186
187=head2 load_class Class::Name
188
6caea456 189This will load a given C<Class::Name> (or die if it's not loadable).
c3398f5b 190This function can be used in place of tricks like
191C<eval "use $module"> or using C<require>.
192
193=head1 AUTHOR
194
195Shawn M Moore, C<< <sartak at gmail.com> >>
196
197=head1 BUGS
198
199No known bugs.
200
201Please report any bugs through RT: email
202C<bug-mouse at rt.cpan.org>, or browse
203L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mouse>.
204
205=head1 COPYRIGHT AND LICENSE
206
207Copyright 2008 Shawn M Moore.
208
209This program is free software; you can redistribute it and/or modify it
210under the same terms as Perl itself.
211
212=cut
213