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