Mercurial > pub > Impl
annotate Lib/IMPL/declare.pm @ 280:c6d0f889ef87
+IMPL::declare now supports meta attributes
*bugfixes related to the typeof() operator
author | cin |
---|---|
date | Wed, 06 Feb 2013 02:15:48 +0400 |
parents | af8af4b8337e |
children | a8dbddf491dd |
rev | line source |
---|---|
197
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
1 package IMPL::declare; |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
2 use strict; |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
3 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
4 use Scalar::Util qw(set_prototype); |
209 | 5 use Carp qw(carp); |
228 | 6 use IMPL::Class::PropertyInfo(); |
230 | 7 use IMPL::Const qw(:access); |
251 | 8 use IMPL::require(); |
9 | |
10 BEGIN { | |
11 *_require = *IMPL::require::_require; | |
12 *_trace = *IMPL::require::_trace; | |
13 } | |
197
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
14 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
15 sub import { |
228 | 16 my ( $self, $args ) = @_; |
17 | |
197
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
18 return unless $args; |
228 | 19 |
20 die "A hash reference is required" unless ref $args eq 'HASH'; | |
21 | |
22 no strict 'refs'; | |
280 | 23 no warnings 'once'; |
228 | 24 |
197
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
25 my $caller = caller; |
228 | 26 |
197
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
27 my $aliases = $args->{require} || {}; |
251 | 28 |
252 | 29 $IMPL::require::PENDING{$caller} = 1; |
251 | 30 _trace("declare $caller"); |
31 $IMPL::require::level++; | |
228 | 32 |
33 while ( my ( $alias, $class ) = each %$aliases ) { | |
251 | 34 _trace("$alias => $class"); |
35 $IMPL::require::level ++; | |
198 | 36 my $c = _require($class); |
228 | 37 |
38 *{"${caller}::$alias"} = set_prototype( | |
39 sub { | |
40 $c; | |
41 }, | |
42 '' | |
43 ); | |
251 | 44 $IMPL::require::level --; |
228 | 45 } |
46 | |
47 my $base = $args->{base} || {}; | |
48 | |
49 my %ctor; | |
50 my @isa; | |
51 | |
52 if ( ref $base eq 'ARRAY' ) { | |
53 carp "Odd elements number in require" | |
54 unless scalar(@$base) % 2 == 0; | |
55 while ( my ( $class, $mapper ) = splice @$base, 0, 2 ) { | |
251 | 56 _trace("parent $class"); |
57 $IMPL::require::level ++; | |
228 | 58 $class = $aliases->{$class} || _require($class); |
251 | 59 $IMPL::require::level --; |
228 | 60 |
61 push @isa, $class; | |
62 $ctor{$class} = $mapper; | |
63 } | |
64 } | |
65 elsif ( ref $base eq 'HASH' ) { | |
66 while ( my ( $class, $mapper ) = each %$base ) { | |
251 | 67 _trace("parent $class"); |
68 $IMPL::require::level ++; | |
228 | 69 $class = $aliases->{$class} || _require($class); |
251 | 70 $IMPL::require::level --; |
228 | 71 |
72 push @isa, $class; | |
73 $ctor{$class} = $mapper; | |
74 } | |
75 } | |
230 | 76 |
279 | 77 %{"${caller}::CTOR"} = %ctor; |
78 push @{"${caller}::ISA"}, @isa; | |
79 | |
80 if(ref($args->{meta}) eq 'ARRAY') { | |
81 $caller->SetMeta($_) foreach @{$args->{meta}}; | |
82 } | |
228 | 83 |
84 my $props = $args->{props} || []; | |
85 | |
86 if ( $props eq 'HASH' ) { | |
87 $props = [%$props]; | |
88 } | |
89 | |
90 die "A hash or an array reference is required in the properties list" | |
91 unless ref $props eq 'ARRAY'; | |
92 | |
93 carp "Odd elements number in properties declaration of $caller" | |
94 unless scalar(@$props) % 2 == 0; | |
95 | |
96 if (@$props) { | |
256
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
252
diff
changeset
|
97 $self->_implementProps($props,$caller); |
228 | 98 } |
251 | 99 |
256
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
252
diff
changeset
|
100 if ($args->{_implement}) { |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
252
diff
changeset
|
101 $self->_implementProps($caller->abstractProps,$caller); |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
252
diff
changeset
|
102 } |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
252
diff
changeset
|
103 |
251 | 104 $IMPL::require::level--; |
252 | 105 delete $IMPL::require::PENDING{$caller}; |
197
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
106 } |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
107 |
256
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
252
diff
changeset
|
108 sub _implementProps { |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
252
diff
changeset
|
109 my ($self, $props, $caller) = @_; |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
252
diff
changeset
|
110 |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
252
diff
changeset
|
111 for ( my $i = 0 ; $i < @$props - 1 ; $i = $i + 2 ) { |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
252
diff
changeset
|
112 my ( $prop, $spec ) = @{$props}[ $i, $i + 1 ]; |
278 | 113 |
114 $caller->ClassPropertyImplementor->Implement( | |
115 $spec, | |
256
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
252
diff
changeset
|
116 { |
275 | 117 name => $prop, |
118 class => $caller, | |
119 access => $prop =~ /^_/ | |
256
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
252
diff
changeset
|
120 ? ACCESS_PRIVATE |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
252
diff
changeset
|
121 : ACCESS_PUBLIC |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
252
diff
changeset
|
122 } |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
252
diff
changeset
|
123 ); |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
252
diff
changeset
|
124 } |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
252
diff
changeset
|
125 } |
32aceba4ee6d
corrected ViewHandlers to handle cookies and headers.
sergey
parents:
252
diff
changeset
|
126 |
197
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
127 1; |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
128 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
129 __END__ |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
130 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
131 =pod |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
132 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
133 =head1 NAME |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
134 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
135 C<IMPL::declare> - описывает класс |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
136 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
137 =head1 SYNOPSIS |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
138 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
139 =begin code |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
140 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
141 package My::Bar; |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
142 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
143 use IMPL::declare { |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
144 require => { |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
145 TFoo => 'My::Foo', |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
146 TBox => 'My::Box' |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
147 }, |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
148 base => { |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
149 TFoo => '@_', |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
150 'IMPL::Object' => undef, |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
151 } |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
152 } |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
153 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
154 sub CreateBox { |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
155 my ($this) = @_; |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
156 return TBox->new($this); |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
157 } |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
158 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
159 =end code |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
160 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
161 Специальная ситрока C<@_> означает передачу параметров конструктора текущего класса конструктору |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
162 базового класса без изменений. |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
163 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
164 =head1 DESCRIPTION |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
165 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
166 Описывает текущий пакет(модуль) как класс. В качестве параметра получает ссылку на хеш, |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
167 в которой храняться метаданные для объявления класса. |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
168 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
169 =head1 METADATA |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
170 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
171 =head2 C<require> |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
172 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
173 Содержит ссылку на хеш с синонимами модулей, которые будут доступны в текушем модуле, |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
174 аналогично использованию C<IMPL::require>. Однако, если модуль не требует загрузки при |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
175 помощи C<require> нужно использовать префикс C<'-'> в его имени |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
176 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
177 =begin code |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
178 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
179 { |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
180 require => { |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
181 TObject => 'IMPL::Object', # will be loaded with require |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
182 TFoo => '-My:App::Data::Foo' # will not use 'require' to load module |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
183 } |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
184 } |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
185 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
186 =end code |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
187 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
188 =head2 C<base> |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
189 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
190 Обисывает базове классы для текущего класса. Если данный параметр - ссылка массив, то |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
191 этот массив будет превращен в массив C<@ISA>. Если данный параметр - ссылка на хеш, то |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
192 его ключи опичавют список базовых классов, а значения - преобразование параметров для |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
193 вызова базовых конструкторов. |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
194 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
195 В качестве имен базовых классов могут быть как полные имена модулей, так и назначенные |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
196 ранее псевдонимы. Использование префикса C<'-'> перед B<полным именем модуля> означает, |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
197 что модуль не требуется загружать, в случае с псевдонимами, префикс C<'-'> уже был указан |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
198 при их объявлении. |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
199 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
200 =begin code |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
201 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
202 { |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
203 require => { |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
204 TFoo => '-My:App::Data::Foo' # will not use 'require' to load module |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
205 }, |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
206 base => { |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
207 TFoo => '@_', # pass parameters unchanged |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
208 'My::Base::Class' => sub { name => $_[0], data => $_[1] }, # remap parameters |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
209 '-My::Extentions' => undef, # do not pass any parameters |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
210 } |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
211 } |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
212 |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
213 =end code |
6b1dda998839
Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff
changeset
|
214 |
228 | 215 =cut |