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