164
|
1 package IMPL::lang;
|
|
2 use strict;
|
|
3 use warnings;
|
|
4
|
165
|
5 use parent qw(Exporter);
|
164
|
6 use IMPL::_core::version;
|
173
|
7 use IMPL::clone qw(clone);
|
164
|
8
|
167
|
9 require IMPL::Class::PropertyInfo;
|
164
|
10
|
167
|
11 our @EXPORT = qw(&is);
|
|
12 our %EXPORT_TAGS = (
|
194
|
13 base => [
|
|
14 qw(
|
|
15 &is
|
|
16 &clone
|
|
17 )
|
|
18 ],
|
|
19 constants => [
|
|
20 qw(
|
|
21 &ACCESS_PUBLIC
|
|
22 &ACCESS_PROTECTED
|
|
23 &ACCESS_PRIVATE
|
|
24 &PROP_GET
|
|
25 &PROP_SET
|
|
26 &PROP_OWNERSET
|
|
27 &PROP_LIST
|
|
28 &PROP_ALL
|
|
29 )
|
|
30 ],
|
167
|
31
|
194
|
32 declare => [
|
|
33 qw(
|
|
34 &public
|
|
35 &protected
|
|
36 &private
|
|
37 &virtual
|
|
38 &property
|
|
39 &static
|
|
40 &property
|
213
|
41 &ACCESS_PUBLIC
|
|
42 &ACCESS_PROTECTED
|
|
43 &ACCESS_PRIVATE
|
|
44 &PROP_GET
|
|
45 &PROP_SET
|
|
46 &PROP_OWNERSET
|
|
47 &PROP_LIST
|
|
48 &PROP_ALL
|
194
|
49 )
|
|
50 ],
|
|
51 compare => [
|
|
52 qw(
|
|
53 &equals
|
|
54 &equals_s
|
|
55 &hashCompare
|
|
56 )
|
|
57 ],
|
|
58 hash => [
|
|
59 qw(
|
|
60 &hashApply
|
|
61 &hashMerge
|
|
62 &hashDiff
|
|
63 &hashCompare
|
|
64 &hashParse
|
|
65 &hashSave
|
|
66 )
|
|
67 ]
|
167
|
68 );
|
|
69
|
|
70 our @EXPORT_OK = keys %{ { map (($_,1) , map (@{$_}, values %EXPORT_TAGS) ) } };
|
|
71
|
|
72 use constant {
|
194
|
73 ACCESS_PUBLIC => 1,
|
|
74 ACCESS_PROTECTED => 2,
|
|
75 ACCESS_PRIVATE => 3,
|
|
76 PROP_GET => 1,
|
|
77 PROP_SET => 2,
|
|
78 PROP_OWNERSET => 10,
|
|
79 PROP_LIST => 4,
|
|
80 PROP_ALL => 3
|
167
|
81 };
|
164
|
82
|
|
83 sub is($$) {
|
194
|
84 eval { $_[0]->isa( $_[1] ) };
|
167
|
85 }
|
|
86
|
|
87 sub virtual($) {
|
194
|
88 $_[0]->Virtual(1);
|
|
89 $_[0];
|
167
|
90 }
|
|
91
|
|
92 sub public($) {
|
194
|
93 $_[0]->Access(ACCESS_PUBLIC);
|
|
94 $_[0]->Implement;
|
|
95 $_[0];
|
167
|
96 }
|
|
97
|
|
98 sub private($) {
|
194
|
99 $_[0]->Access(ACCESS_PRIVATE);
|
|
100 $_[0]->Implement;
|
|
101 $_[0];
|
167
|
102 }
|
|
103
|
|
104 sub protected($) {
|
194
|
105 $_[0]->Access(ACCESS_PROTECTED);
|
|
106 $_[0]->Implement;
|
|
107 $_[0];
|
164
|
108 }
|
|
109
|
167
|
110 sub property($$;$) {
|
194
|
111 my ( $propName, $mutators, $attributes ) = @_;
|
|
112 my $Info = new IMPL::Class::PropertyInfo(
|
|
113 {
|
|
114 Name => $propName,
|
|
115 Mutators => $mutators,
|
|
116 Class => scalar(caller),
|
|
117 Attributes => $attributes
|
|
118 }
|
|
119 );
|
|
120 return $Info;
|
167
|
121 }
|
|
122
|
|
123 sub static($$) {
|
194
|
124 my ( $name, $value ) = @_;
|
|
125 my $class = caller;
|
|
126 $class->static_accessor( $name, $value );
|
167
|
127 }
|
|
128
|
|
129 sub equals {
|
194
|
130 if (defined $_[0]) {
|
|
131 return 0 if (not defined $_[1]);
|
|
132
|
|
133 return $_[0] == $_[1];
|
|
134 } else {
|
|
135 return 0 if defined $_[1];
|
|
136
|
|
137 return 1;
|
|
138 }
|
167
|
139 }
|
|
140
|
|
141 sub equals_s {
|
194
|
142 if (defined $_[0]) {
|
|
143 return 0 if (not defined $_[1]);
|
|
144
|
|
145 return $_[0] eq $_[1];
|
|
146 } else {
|
|
147 return 0 if defined $_[1];
|
|
148
|
|
149 return 1;
|
|
150 }
|
167
|
151 }
|
|
152
|
168
|
153 sub hashDiff {
|
194
|
154 my ($src,$dst) = @_;
|
|
155
|
|
156 $dst = $dst ? { %$dst } : {} ;
|
|
157 $src ||= {};
|
|
158
|
|
159 my %result;
|
|
160
|
|
161 foreach my $key ( keys %$src ) {
|
|
162 if (exists $dst->{$key}) {
|
|
163 $result{"+$key"} = $dst->{$key} unless equals_s($dst->{$key}, $src->{$key});
|
|
164 delete $dst->{$key};
|
|
165 } else {
|
|
166 $result{"-$key"} = 1;
|
|
167 }
|
|
168 }
|
|
169
|
|
170 $result{"+$_"} = $dst->{$_} foreach keys %$dst;
|
|
171
|
|
172 return \%result;
|
168
|
173 }
|
|
174
|
|
175 sub hashMerge {
|
210
|
176 return hashApply( { %{$_[0] || {}} }, $_[1] );
|
168
|
177 }
|
|
178
|
|
179 sub hashApply {
|
194
|
180 my ($target,$diff) = @_;
|
|
181
|
|
182 while ( my ($key,$value) = each %$diff) {
|
|
183 $key =~ /^(\+|-)?(.*)$/;
|
|
184 my $op = $1 || '+';
|
|
185 $key = $2;
|
|
186
|
|
187 if ($op eq '-') {
|
|
188 delete $target->{$key};
|
|
189 } else {
|
|
190 $target->{$key} = $value;
|
|
191 }
|
|
192 }
|
|
193
|
|
194 return $target;
|
168
|
195 }
|
|
196
|
|
197 sub hashCompare {
|
194
|
198 my ($l,$r,$cmp) = @_;
|
|
199
|
|
200 $cmp ||= \&equals_s;
|
|
201
|
|
202 return 0 unless scalar keys %$l == scalar keys %$r;
|
|
203 &$cmp($l->{$_},$r->{$_}) || return 0 foreach keys %$l;
|
|
204
|
|
205 return 1;
|
168
|
206 }
|
|
207
|
174
|
208 sub hashParse {
|
194
|
209 my ($s,$p,$d) = @_;
|
|
210
|
|
211 $p = $p ? qr/$p/ : qr/\n+/;
|
|
212 $d = $d ? qr/$d/ : qr/\s*=\s*/;
|
|
213
|
|
214 return {
|
|
215 map split($d,$_,2), split($p,$s)
|
|
216 };
|
174
|
217 }
|
|
218
|
|
219 sub hashSave {
|
194
|
220 my ($hash,$p,$d) = @_;
|
|
221
|
|
222 return "" unless ref $hash eq 'HASH';
|
|
223
|
|
224 $p ||= "\n";
|
|
225 $d ||= " = ";
|
|
226
|
|
227 return
|
|
228 join(
|
|
229 $p,
|
|
230 map(
|
|
231 join(
|
|
232 $d,
|
|
233 $_,
|
|
234 $hash->{$_}
|
|
235 ),
|
|
236 keys %$hash
|
|
237 )
|
|
238 );
|
174
|
239 }
|
|
240
|
167
|
241 1;
|