Include Mouse::PurePerl first
[gitmo/Mouse.git] / lib / Mouse / PurePerl.pm
CommitLineData
ccb38d0b 1package Mouse::PurePerl;
2
df6dd016 3package
4 Mouse::Util;
5
6use strict;
7use warnings;
8
9use warnings FATAL => 'redefine'; # to avoid to load Mouse::PurePerl
10
11use B ();
12
13sub is_class_loaded {
14 my $class = shift;
15
16 return 0 if ref($class) || !defined($class) || !length($class);
17
18 # walk the symbol table tree to avoid autovififying
19 # \*{${main::}{"Foo::"}} == \*main::Foo::
20
21 my $pack = \%::;
22 foreach my $part (split('::', $class)) {
23 my $entry = \$pack->{$part . '::'};
24 return 0 if ref($entry) ne 'GLOB';
25 $pack = *{$entry}{HASH} or return 0;
26 }
27
28 # check for $VERSION or @ISA
29 return 1 if exists $pack->{VERSION}
30 && defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} };
31 return 1 if exists $pack->{ISA}
32 && defined *{$pack->{ISA}}{ARRAY} && @{ $pack->{ISA} } != 0;
33
34 # check for any method
35 foreach my $name( keys %{$pack} ) {
36 my $entry = \$pack->{$name};
37 return 1 if ref($entry) ne 'GLOB' || defined *{$entry}{CODE};
38 }
39
40 # fail
41 return 0;
42}
43
44
45# taken from Sub::Identify
46sub get_code_info {
47 my ($coderef) = @_;
48 ref($coderef) or return;
49
50 my $cv = B::svref_2object($coderef);
51 $cv->isa('B::CV') or return;
52
53 my $gv = $cv->GV;
54 $gv->isa('B::GV') or return;
55
56 return ($gv->STASH->NAME, $gv->NAME);
57}
58
59sub get_code_package{
60 my($coderef) = @_;
61
62 my $cv = B::svref_2object($coderef);
63 $cv->isa('B::CV') or return '';
64
65 my $gv = $cv->GV;
66 $gv->isa('B::GV') or return '';
67
68 return $gv->STASH->NAME;
69}
70
7d96ae4d 71sub get_code_ref{
72 my($package, $name) = @_;
73 no strict 'refs';
74 no warnings 'once';
75 use warnings FATAL => 'uninitialized';
76 return *{$package . '::' . $name}{CODE};
77}
78
79package
80 Mouse::Util::TypeConstraints;
81
02d8cd0a 82use Scalar::Util qw(blessed looks_like_number openhandle);
83
1d5ecd5f 84sub _generate_class_type_for{
85 my($for_class, $name) = @_;
86
02d8cd0a 87 my $predicate = sub{ blessed($_[0]) && $_[0]->isa($for_class) };
1d5ecd5f 88
89 if(defined $name){
90 no strict 'refs';
91 *{ caller() . '::' . $name } = $predicate;
92 return;
93 }
94
95 return $predicate;
96}
97
98
7d96ae4d 99sub Any { 1 }
100sub Item { 1 }
7d96ae4d 101
102sub Bool { $_[0] ? $_[0] eq '1' : 1 }
103sub Undef { !defined($_[0]) }
104sub Defined { defined($_[0]) }
105sub Value { defined($_[0]) && !ref($_[0]) }
106sub Num { !ref($_[0]) && looks_like_number($_[0]) }
107sub Int { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ }
108sub Str { defined($_[0]) && !ref($_[0]) }
109
110sub Ref { ref($_[0]) }
111sub ScalarRef { ref($_[0]) eq 'SCALAR' }
112sub ArrayRef { ref($_[0]) eq 'ARRAY' }
113sub HashRef { ref($_[0]) eq 'HASH' }
114sub CodeRef { ref($_[0]) eq 'CODE' }
115sub RegexpRef { ref($_[0]) eq 'Regexp' }
116sub GlobRef { ref($_[0]) eq 'GLOB' }
117
118sub FileHandle {
119 openhandle($_[0]) || (blessed($_[0]) && $_[0]->isa("IO::Handle"))
120}
121
122sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }
123
124sub ClassName { Mouse::Util::is_class_loaded($_[0]) }
125sub RoleName { (Mouse::Util::class_of($_[0]) || return 0)->isa('Mouse::Meta::Role') }
126
127
43165725 128package
129 Mouse::Meta::Module;
130
926404f2 131sub name { $_[0]->{package} }
132
133sub _method_map { $_[0]->{methods} }
134sub _attribute_map{ $_[0]->{attribute_map} }
43165725 135
2591e962 136sub namespace{
137 my $name = $_[0]->{package};
138 no strict 'refs';
139 return \%{ $name . '::' };
140}
141
3e44140b 142sub add_method {
143 my($self, $name, $code) = @_;
144
145 if(!defined $name){
146 $self->throw_error('You must pass a defined name');
147 }
148 if(!defined $code){
149 $self->throw_error('You must pass a defined code');
150 }
151
152 if(ref($code) ne 'CODE'){
153 $code = \&{$code}; # coerce
154 }
155
156 $self->{methods}->{$name} = $code; # Moose stores meta object here.
157
158 my $pkg = $self->name;
159 no strict 'refs';
160 no warnings 'redefine', 'once';
161 *{ $pkg . '::' . $name } = $code;
162 return;
163}
164
165
43165725 166package
167 Mouse::Meta::Class;
168
169sub is_anon_class{
170 return exists $_[0]->{anon_serial_id};
171}
172
173sub roles { $_[0]->{roles} }
174
cccb83de 175sub linearized_isa { @{ get_linear_isa($_[0]->{package}) } }
176
43165725 177package
178 Mouse::Meta::Role;
179
180sub is_anon_role{
181 return exists $_[0]->{anon_serial_id};
182}
183
184sub get_roles { $_[0]->{roles} }
185
186package
187 Mouse::Meta::Attribute;
188
d7d8d49b 189use Mouse::Meta::Method::Accessor;
43165725 190
191# readers
192
193sub name { $_[0]->{name} }
194sub associated_class { $_[0]->{associated_class} }
195
196sub accessor { $_[0]->{accessor} }
197sub reader { $_[0]->{reader} }
198sub writer { $_[0]->{writer} }
199sub predicate { $_[0]->{predicate} }
200sub clearer { $_[0]->{clearer} }
201sub handles { $_[0]->{handles} }
202
203sub _is_metadata { $_[0]->{is} }
204sub is_required { $_[0]->{required} }
205sub default { $_[0]->{default} }
206sub is_lazy { $_[0]->{lazy} }
207sub is_lazy_build { $_[0]->{lazy_build} }
208sub is_weak_ref { $_[0]->{weak_ref} }
209sub init_arg { $_[0]->{init_arg} }
210sub type_constraint { $_[0]->{type_constraint} }
211
212sub trigger { $_[0]->{trigger} }
213sub builder { $_[0]->{builder} }
214sub should_auto_deref { $_[0]->{auto_deref} }
215sub should_coerce { $_[0]->{coerce} }
216
d899d3e7 217sub documentation { $_[0]->{documentation} }
218
43165725 219# predicates
220
221sub has_accessor { exists $_[0]->{accessor} }
222sub has_reader { exists $_[0]->{reader} }
223sub has_writer { exists $_[0]->{writer} }
224sub has_predicate { exists $_[0]->{predicate} }
225sub has_clearer { exists $_[0]->{clearer} }
226sub has_handles { exists $_[0]->{handles} }
227
228sub has_default { exists $_[0]->{default} }
229sub has_type_constraint { exists $_[0]->{type_constraint} }
230sub has_trigger { exists $_[0]->{trigger} }
231sub has_builder { exists $_[0]->{builder} }
232
d899d3e7 233sub has_documentation { exists $_[0]->{documentation} }
234
93540011 235sub accessor_metaclass(){ 'Mouse::Meta::Method::Accessor' }
236
43165725 237package
238 Mouse::Meta::TypeConstraint;
239
240sub name { $_[0]->{name} }
241sub parent { $_[0]->{parent} }
242sub message { $_[0]->{message} }
243
244sub _compiled_type_constraint{ $_[0]->{compiled_type_constraint} }
245
93540011 246sub _compiled_type_coercion { $_[0]->{_compiled_type_coercion} }
df6dd016 247
93540011 248sub has_coercion{ exists $_[0]->{_compiled_type_coercion} }
df6dd016 249
2501;
251__END__
ccb38d0b 252
253=head1 NAME
254
255Mouse::PurePerl - A Mouse guts in pure Perl
256
257=head1 VERSION
258
2b68f76d 259This document describes Mouse version 0.40_03
ccb38d0b 260
261=head1 SEE ALSO
262
263L<Mouse::XS>
264
265=cut