Implementation of ClassName, borrowed from Moose
[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 {
46 return \&Carp::confess;
47 },
48
49 blessed => sub {
50 return \&Scalar::Util::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 no strict 'refs';
66 @{ $CALLER . '::ISA' } = 'Mouse::Object';
67
68 goto $exporter;
69 }
70
71 sub unimport {
72 my $caller = caller;
73
74 no strict 'refs';
75 for my $keyword (keys %exports) {
76 next if $keyword eq 'meta'; # we don't delete this one
77 delete ${ $caller . '::' }{$keyword};
78 }
79 }
80};
81
82sub load_class {
83 my $class = shift;
84
85 (my $file = "$class.pm") =~ s{::}{/}g;
86
87 eval { CORE::require($file) };
88 confess "Could not load class ($class) because : $@"
89 if $@
90 && $@ !~ /^Can't locate .*? at /;
91
92 return 1;
93}
94
951;
96
97__END__
98
99=head1 NAME
100
101Mouse - miniature Moose near the speed of light
102
103=head1 VERSION
104
105Version 0.01 released ???
106
107=head1 SYNOPSIS
108
109 package Point;
110 use Mouse;
111
112 has x => (
113 is => 'rw',
114 );
115
116 has y => (
117 is => 'rw',
118 default => 0,
119 predicate => 'has_y',
120 clearer => 'clear_y',
121 );
122
123=head1 DESCRIPTION
124
125Moose.
126
127=head1 INTERFACE
128
129=head2 meta -> Mouse::Class
130
131Returns this class' metaclass instance.
132
133=head2 extends superclasses
134
135Sets this class' superclasses.
136
137=head2 has (name|names) => parameters
138
139Adds an attribute (or if passed an arrayref of names, multiple attributes) to
140this class.
141
142=head2 confess error -> BOOM
143
144L<Carp/confess> for your convenience.
145
146=head2 blessed value -> ClassName | undef
147
148L<Scalar::Util/blessed> for your convenience.
149
150=head1 MISC
151
152=head2 import
153
154Importing Mouse will set your class' superclass list to L<Mouse::Object>.
155You may use L</extends> to replace the superclass list.
156
157=head2 unimport
158
159Please unimport Mouse so that if someone calls one of the keywords (such as
160L</extends>) it will break loudly instead breaking subtly.
161
162=head1 FUNCTIONS
163
164=head2 load_class Class::Name
165
166This will load a given Class::Name> (or die if it's not loadable).
167This function can be used in place of tricks like
168C<eval "use $module"> or using C<require>.
169
170=head1 AUTHOR
171
172Shawn M Moore, C<< <sartak at gmail.com> >>
173
174=head1 BUGS
175
176No known bugs.
177
178Please report any bugs through RT: email
179C<bug-mouse at rt.cpan.org>, or browse
180L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mouse>.
181
182=head1 COPYRIGHT AND LICENSE
183
184Copyright 2008 Shawn M Moore.
185
186This program is free software; you can redistribute it and/or modify it
187under the same terms as Perl itself.
188
189=cut
190