annotate lib/IMPL/lang.pm @ 419:bbc4739c4d48 ref20150831

working on IMPL::Config::Container
author cin
date Sun, 29 Jan 2017 10:30:20 +0300
parents 30e8c6a74937
children 7798345304bc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::lang;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3 use warnings;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use parent qw(Exporter);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use IMPL::clone qw(clone);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 use Scalar::Util qw(blessed);
408
5c80e33f1218 added 'coarsen' function
cin
parents: 407
diff changeset
8 use POSIX;
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 use Carp qw(carp);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10
409
cin
parents: 408
diff changeset
11 our @EXPORT = qw(&is &isclass &typeof &coarsen &coarsen_dt);
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 our %EXPORT_TAGS = (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 base => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 qw(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 &is
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 &clone
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 &isclass
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 &typeof
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 409
diff changeset
19 &ishash
30e8c6a74937 working on di container (role based registrations)
cin
parents: 409
diff changeset
20 &isarray
419
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 412
diff changeset
21 &isscalar
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 412
diff changeset
22 &isglob
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 declare => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 qw(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 &public
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 &protected
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 &private
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 &property
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 &static
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 &property
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 &_direct
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 &ACCESS_PUBLIC
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 &ACCESS_PROTECTED
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 &ACCESS_PRIVATE
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 &PROP_GET
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 &PROP_SET
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 &PROP_OWNERSET
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 &PROP_LIST
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 &PROP_ALL
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 &PROP_RO
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 &PROP_RW
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 &PROP_DIRECT
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 compare => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 qw(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 &equals
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 &equals_s
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 &hashCompare
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 hash => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 qw(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 &hashApply
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 &hashMerge
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 &hashDiff
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 &hashCompare
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 &hashParse
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 &hashSave
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 our @EXPORT_OK = keys %{ { map (($_,1) , map (@{$_}, values %EXPORT_TAGS) ) } };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 use IMPL::Const qw(:all);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 409
diff changeset
71 sub is {
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 carp "A typename can't be undefined" unless $_[1];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 blessed($_[0]) and $_[0]->isa( $_[1] );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 sub isclass {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 carp "A typename can't be undefined" unless $_[1];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 local $@;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 eval {not ref $_[0] and $_[0]->isa( $_[1] ) };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 sub typeof(*) {
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 409
diff changeset
83 blessed($_[0]);
30e8c6a74937 working on di container (role based registrations)
cin
parents: 409
diff changeset
84 }
30e8c6a74937 working on di container (role based registrations)
cin
parents: 409
diff changeset
85
30e8c6a74937 working on di container (role based registrations)
cin
parents: 409
diff changeset
86 sub isarray {
30e8c6a74937 working on di container (role based registrations)
cin
parents: 409
diff changeset
87 not blessed($_[0]) and ref $_[0] eq 'ARRAY';
30e8c6a74937 working on di container (role based registrations)
cin
parents: 409
diff changeset
88 }
30e8c6a74937 working on di container (role based registrations)
cin
parents: 409
diff changeset
89
30e8c6a74937 working on di container (role based registrations)
cin
parents: 409
diff changeset
90 sub ishash {
30e8c6a74937 working on di container (role based registrations)
cin
parents: 409
diff changeset
91 not blessed($_[0]) and ref $_[0] eq 'HASH';
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93
419
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 412
diff changeset
94 sub isscalar {
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 412
diff changeset
95 not blessed($_[0]) and ref $_[0] eq 'SCALAR';
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 412
diff changeset
96 }
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 412
diff changeset
97
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 412
diff changeset
98 sub isglob {
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 412
diff changeset
99 not blessed($_[0]) and ref $_[0] eq 'GLOB';
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 412
diff changeset
100 }
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 412
diff changeset
101
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 sub public($) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 my $info = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 $info->{access} = ACCESS_PUBLIC;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105 my $implementor = delete $info->{implementor};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 $implementor->Implement($info);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 sub private($) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 my $info = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 $info->{access} = ACCESS_PRIVATE;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 my $implementor = delete $info->{implementor};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 $implementor->Implement($info);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 sub protected($) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 my $info = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 $info->{access} = ACCESS_PROTECTED;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 my $implementor = delete $info->{implementor};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 $implementor->Implement($info);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 sub _direct ($) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 my $info = shift;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 $info->{direct} = 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126 return $info;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 sub property($$) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130 my ($propName,$attributes) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132 $attributes = {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 get => $attributes & PROP_GET,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134 set => $attributes & PROP_SET,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 isList => $attributes & PROP_LIST
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136 } unless ref $attributes;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138 my $class = caller;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140 return hashMerge (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141 $attributes,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142 {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143 implementor => $class->ClassPropertyImplementor,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144 name => $propName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
145 class => scalar(caller),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
146 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
147 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
148 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
150 sub static($$) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
151 my ( $name, $value ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
152 my $class = caller;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
153 $class->static_accessor( $name, $value );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
154 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
155
408
5c80e33f1218 added 'coarsen' function
cin
parents: 407
diff changeset
156 sub coarsen {
5c80e33f1218 added 'coarsen' function
cin
parents: 407
diff changeset
157 my ( $value, $resolution ) = @_;
5c80e33f1218 added 'coarsen' function
cin
parents: 407
diff changeset
158 return $resolution ? ceil( $value / $resolution ) * $resolution : $value;
5c80e33f1218 added 'coarsen' function
cin
parents: 407
diff changeset
159 }
5c80e33f1218 added 'coarsen' function
cin
parents: 407
diff changeset
160
409
cin
parents: 408
diff changeset
161 # datetime is DateTime object
cin
parents: 408
diff changeset
162 # resolution is DateTime::Duration object, the resulting time will be aligned to it
cin
parents: 408
diff changeset
163 sub coarsen_dt {
cin
parents: 408
diff changeset
164 my ( $datetime, $resolution ) = @_;
cin
parents: 408
diff changeset
165
cin
parents: 408
diff changeset
166 return $datetime unless $resolution;
cin
parents: 408
diff changeset
167
cin
parents: 408
diff changeset
168 my $date = $datetime->clone()->truncate( to => "day" );
cin
parents: 408
diff changeset
169
cin
parents: 408
diff changeset
170 return $date->add(
cin
parents: 408
diff changeset
171 minutes => coarsen(
cin
parents: 408
diff changeset
172 $datetime->subtract_datetime($date)->in_units('minutes'),
cin
parents: 408
diff changeset
173 $resolution->in_units('minutes')
cin
parents: 408
diff changeset
174 )
cin
parents: 408
diff changeset
175 );
cin
parents: 408
diff changeset
176 }
cin
parents: 408
diff changeset
177
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
178 sub equals {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
179 if (defined $_[0]) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
180 return 0 if (not defined $_[1]);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
181
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
182 return $_[0] == $_[1];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
183 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
184 return 0 if defined $_[1];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
185
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
186 return 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
187 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
188 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
189
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
190 sub equals_s {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
191 if (defined $_[0]) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
192 return 0 if (not defined $_[1]);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
193
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
194 return $_[0] eq $_[1];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
195 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
196 return 0 if defined $_[1];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
197
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
198 return 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
199 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
200 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
201
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
202 sub hashDiff {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
203 my ($src,$dst) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
204
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
205 $dst = $dst ? { %$dst } : {} ;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
206 $src ||= {};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
207
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
208 my %result;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
209
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
210 foreach my $key ( keys %$src ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
211 if (exists $dst->{$key}) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
212 $result{"+$key"} = $dst->{$key} unless equals_s($dst->{$key}, $src->{$key});
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
213 delete $dst->{$key};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
214 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
215 $result{"-$key"} = 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
216 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
217 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
218
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
219 $result{"+$_"} = $dst->{$_} foreach keys %$dst;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
220
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
221 return \%result;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
222 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
223
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
224 sub hashMerge {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
225 return hashApply( { %{$_[0] || {}} }, $_[1] );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
226 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
227
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
228 sub hashApply {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
229 my ($target,$diff) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
230
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
231 return $target unless ref $diff eq 'HASH';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
232
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
233 while ( my ($key,$value) = each %$diff) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
234 $key =~ /^(\+|-)?(.*)$/;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
235 my $op = $1 || '+';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
236 $key = $2;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
237
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
238 if ($op eq '-') {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
239 delete $target->{$key};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
240 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
241 $target->{$key} = $value;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
242 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
243 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
244
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
245 return $target;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
246 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
247
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
248 sub hashCompare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
249 my ($l,$r,$cmp) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
250
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
251 $cmp ||= \&equals_s;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
252
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
253 return 0 unless scalar keys %$l == scalar keys %$r;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
254 &$cmp($l->{$_},$r->{$_}) || return 0 foreach keys %$l;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
255
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
256 return 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
257 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
258
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
259 sub hashParse {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
260 my ($s,$p,$d) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
261
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
262 $p = $p ? qr/$p/ : qr/\n+/;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
263 $d = $d ? qr/$d/ : qr/\s*=\s*/;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
264
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
265 return {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
266 map split($d,$_,2), split($p,$s)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
267 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
268 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
269
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
270 sub hashSave {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
271 my ($hash,$p,$d) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
272
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
273 return "" unless ref $hash eq 'HASH';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
274
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
275 $p ||= "\n";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
276 $d ||= " = ";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
277
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
278 return
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
279 join(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
280 $p,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
281 map(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
282 join(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
283 $d,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
284 $_,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
285 $hash->{$_}
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
286 ),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
287 keys %$hash
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
288 )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
289 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
290 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
291
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
292 1;