Use Scalar::Util/Carp imports for brevity
[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 - miniature Moose near the speed of light
103
104 =head1 VERSION
105
106 Version 0.01 released ???
107
108 =head1 SYNOPSIS
109
110     package Point;
111     use Mouse;
112
113     has x => (
114         is => 'rw',
115     );
116
117     has y => (
118         is        => 'rw',
119         default   => 0,
120         predicate => 'has_y',
121         clearer   => 'clear_y',
122     );
123
124 =head1 DESCRIPTION
125
126 Moose.
127
128 =head1 INTERFACE
129
130 =head2 meta -> Mouse::Class
131
132 Returns this class' metaclass instance.
133
134 =head2 extends superclasses
135
136 Sets this class' superclasses.
137
138 =head2 has (name|names) => parameters
139
140 Adds an attribute (or if passed an arrayref of names, multiple attributes) to
141 this class.
142
143 =head2 confess error -> BOOM
144
145 L<Carp/confess> for your convenience.
146
147 =head2 blessed value -> ClassName | undef
148
149 L<Scalar::Util/blessed> for your convenience.
150
151 =head1 MISC
152
153 =head2 import
154
155 Importing Mouse will set your class' superclass list to L<Mouse::Object>.
156 You may use L</extends> to replace the superclass list.
157
158 =head2 unimport
159
160 Please unimport Mouse so that if someone calls one of the keywords (such as
161 L</extends>) it will break loudly instead breaking subtly.
162
163 =head1 FUNCTIONS
164
165 =head2 load_class Class::Name
166
167 This will load a given Class::Name> (or die if it's not loadable).
168 This function can be used in place of tricks like
169 C<eval "use $module"> or using C<require>.
170
171 =head1 AUTHOR
172
173 Shawn M Moore, C<< <sartak at gmail.com> >>
174
175 =head1 BUGS
176
177 No known bugs.
178
179 Please report any bugs through RT: email
180 C<bug-mouse at rt.cpan.org>, or browse
181 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mouse>.
182
183 =head1 COPYRIGHT AND LICENSE
184
185 Copyright 2008 Shawn M Moore.
186
187 This program is free software; you can redistribute it and/or modify it
188 under the same terms as Perl itself.
189
190 =cut
191