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