Refactor tc parametarization
[gitmo/Mouse.git] / lib / Mouse / PurePerl.pm
1 package Mouse::PurePerl;
2
3 package
4     Mouse::Util;
5
6 use strict;
7 use warnings;
8
9 use warnings FATAL => 'redefine'; # to avoid to load Mouse::PurePerl
10
11 use B ();
12
13 sub 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
46 sub 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
59 sub 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
71 sub 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
79 sub generate_isa_predicate_for {
80     my($for_class, $name) = @_;
81
82     my $predicate = sub{ Scalar::Util::blessed($_[0]) && $_[0]->isa($for_class) };
83
84     if(defined $name){
85         no strict 'refs';
86         *{ caller() . '::' . $name } = $predicate;
87         return;
88     }
89
90     return $predicate;
91 }
92
93
94 package
95     Mouse::Util::TypeConstraints;
96
97 use Scalar::Util qw(blessed looks_like_number openhandle);
98
99 sub Any        { 1 }
100 sub Item       { 1 }
101
102 sub Bool       { $_[0] ? $_[0] eq '1' : 1 }
103 sub Undef      { !defined($_[0]) }
104 sub Defined    {  defined($_[0])  }
105 sub Value      {  defined($_[0]) && !ref($_[0]) }
106 sub Num        { !ref($_[0]) && looks_like_number($_[0]) }
107 sub Int        {  defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ }
108 sub Str        {  defined($_[0]) && !ref($_[0]) }
109
110 sub Ref        { ref($_[0]) }
111 sub ScalarRef  { ref($_[0]) eq 'SCALAR' }
112 sub ArrayRef   { ref($_[0]) eq 'ARRAY'  }
113 sub HashRef    { ref($_[0]) eq 'HASH'   }
114 sub CodeRef    { ref($_[0]) eq 'CODE'   }
115 sub RegexpRef  { ref($_[0]) eq 'Regexp' }
116 sub GlobRef    { ref($_[0]) eq 'GLOB'   }
117
118 sub FileHandle {
119     openhandle($_[0])  || (blessed($_[0]) && $_[0]->isa("IO::Handle"))
120 }
121
122 sub Object     { blessed($_[0]) && blessed($_[0]) ne 'Regexp' }
123
124 sub ClassName  { Mouse::Util::is_class_loaded($_[0]) }
125 sub RoleName   { (Mouse::Util::class_of($_[0]) || return 0)->isa('Mouse::Meta::Role') }
126
127
128 package
129     Mouse::Meta::Module;
130
131 sub name          { $_[0]->{package} }
132
133 sub _method_map   { $_[0]->{methods} }
134 sub _attribute_map{ $_[0]->{attribute_map} }
135
136 sub namespace{
137     my $name = $_[0]->{package};
138     no strict 'refs';
139     return \%{ $name . '::' };
140 }
141
142 sub 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
166 package
167     Mouse::Meta::Class;
168
169 sub is_anon_class{
170     return exists $_[0]->{anon_serial_id};
171 }
172
173 sub roles { $_[0]->{roles} }
174
175 sub linearized_isa { @{ get_linear_isa($_[0]->{package}) } }
176
177 package
178     Mouse::Meta::Role;
179
180 sub is_anon_role{
181     return exists $_[0]->{anon_serial_id};
182 }
183
184 sub get_roles { $_[0]->{roles} }
185
186 package
187     Mouse::Meta::Attribute;
188
189 use Mouse::Meta::Method::Accessor;
190
191 # readers
192
193 sub name                 { $_[0]->{name}                   }
194 sub associated_class     { $_[0]->{associated_class}       }
195
196 sub accessor             { $_[0]->{accessor}               }
197 sub reader               { $_[0]->{reader}                 }
198 sub writer               { $_[0]->{writer}                 }
199 sub predicate            { $_[0]->{predicate}              }
200 sub clearer              { $_[0]->{clearer}                }
201 sub handles              { $_[0]->{handles}                }
202
203 sub _is_metadata         { $_[0]->{is}                     }
204 sub is_required          { $_[0]->{required}               }
205 sub default              { $_[0]->{default}                }
206 sub is_lazy              { $_[0]->{lazy}                   }
207 sub is_lazy_build        { $_[0]->{lazy_build}             }
208 sub is_weak_ref          { $_[0]->{weak_ref}               }
209 sub init_arg             { $_[0]->{init_arg}               }
210 sub type_constraint      { $_[0]->{type_constraint}        }
211
212 sub trigger              { $_[0]->{trigger}                }
213 sub builder              { $_[0]->{builder}                }
214 sub should_auto_deref    { $_[0]->{auto_deref}             }
215 sub should_coerce        { $_[0]->{coerce}                 }
216
217 sub documentation        { $_[0]->{documentation}          }
218
219 # predicates
220
221 sub has_accessor         { exists $_[0]->{accessor}        }
222 sub has_reader           { exists $_[0]->{reader}          }
223 sub has_writer           { exists $_[0]->{writer}          }
224 sub has_predicate        { exists $_[0]->{predicate}       }
225 sub has_clearer          { exists $_[0]->{clearer}         }
226 sub has_handles          { exists $_[0]->{handles}         }
227
228 sub has_default          { exists $_[0]->{default}         }
229 sub has_type_constraint  { exists $_[0]->{type_constraint} }
230 sub has_trigger          { exists $_[0]->{trigger}         }
231 sub has_builder          { exists $_[0]->{builder}         }
232
233 sub has_documentation    { exists $_[0]->{documentation}   }
234
235 sub accessor_metaclass(){ 'Mouse::Meta::Method::Accessor' }
236
237 package
238     Mouse::Meta::TypeConstraint;
239
240 sub name    { $_[0]->{name}    }
241 sub parent  { $_[0]->{parent}  }
242 sub message { $_[0]->{message} }
243
244 sub _compiled_type_constraint{ $_[0]->{compiled_type_constraint} }
245
246 sub _compiled_type_coercion  { $_[0]->{_compiled_type_coercion}  }
247
248 sub has_coercion{ exists $_[0]->{_compiled_type_coercion} }
249
250 1;
251 __END__
252
253 =head1 NAME
254
255 Mouse::PurePerl - A Mouse guts in pure Perl
256
257 =head1 VERSION
258
259 This document describes Mouse version 0.40_03
260
261 =head1 SEE ALSO
262
263 L<Mouse::XS>
264
265 =cut