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