annotate lib/IMPL/lang.pm @ 427:09e0086a82a7 ref20150831 tip

Merge
author cin
date Tue, 15 May 2018 00:51:33 +0300
parents 7798345304bc
children
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 compare => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 qw(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 &equals
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 &equals_s
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 &hashCompare
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 hash => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 qw(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 &hashApply
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 &hashMerge
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 &hashDiff
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 &hashCompare
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 &hashParse
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 &hashSave
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
44 our @EXPORT_OK =
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
45 keys %{ { map ( ( $_, 1 ), map ( @{$_}, values %EXPORT_TAGS ) ) } };
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 use IMPL::Const qw(:all);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 409
diff changeset
49 sub is {
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 carp "A typename can't be undefined" unless $_[1];
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
51 blessed( $_[0] ) and $_[0]->isa( $_[1] );
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 sub isclass {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 carp "A typename can't be undefined" unless $_[1];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 local $@;
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
57 eval { not ref $_[0] and $_[0]->isa( $_[1] ) };
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 sub typeof(*) {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
61 blessed( $_[0] );
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 409
diff changeset
62 }
30e8c6a74937 working on di container (role based registrations)
cin
parents: 409
diff changeset
63
30e8c6a74937 working on di container (role based registrations)
cin
parents: 409
diff changeset
64 sub isarray {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
65 not blessed( $_[0] ) and ref $_[0] eq 'ARRAY';
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 409
diff changeset
66 }
30e8c6a74937 working on di container (role based registrations)
cin
parents: 409
diff changeset
67
30e8c6a74937 working on di container (role based registrations)
cin
parents: 409
diff changeset
68 sub ishash {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
69 not blessed( $_[0] ) and ref $_[0] eq 'HASH';
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71
419
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 412
diff changeset
72 sub isscalar {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
73 not blessed( $_[0] ) and ref $_[0] eq 'SCALAR';
419
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 412
diff changeset
74 }
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 412
diff changeset
75
bbc4739c4d48 working on IMPL::Config::Container
cin
parents: 412
diff changeset
76 sub isglob {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
77 not blessed( $_[0] ) and ref $_[0] eq 'GLOB';
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79
408
5c80e33f1218 added 'coarsen' function
cin
parents: 407
diff changeset
80 sub coarsen {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
81 my ( $value, $resolution ) = @_;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
82 return $resolution ? ceil( $value / $resolution ) * $resolution : $value;
408
5c80e33f1218 added 'coarsen' function
cin
parents: 407
diff changeset
83 }
5c80e33f1218 added 'coarsen' function
cin
parents: 407
diff changeset
84
409
cin
parents: 408
diff changeset
85 # datetime is DateTime object
cin
parents: 408
diff changeset
86 # resolution is DateTime::Duration object, the resulting time will be aligned to it
cin
parents: 408
diff changeset
87 sub coarsen_dt {
cin
parents: 408
diff changeset
88 my ( $datetime, $resolution ) = @_;
cin
parents: 408
diff changeset
89
cin
parents: 408
diff changeset
90 return $datetime unless $resolution;
cin
parents: 408
diff changeset
91
cin
parents: 408
diff changeset
92 my $date = $datetime->clone()->truncate( to => "day" );
cin
parents: 408
diff changeset
93
cin
parents: 408
diff changeset
94 return $date->add(
cin
parents: 408
diff changeset
95 minutes => coarsen(
cin
parents: 408
diff changeset
96 $datetime->subtract_datetime($date)->in_units('minutes'),
cin
parents: 408
diff changeset
97 $resolution->in_units('minutes')
cin
parents: 408
diff changeset
98 )
cin
parents: 408
diff changeset
99 );
cin
parents: 408
diff changeset
100 }
cin
parents: 408
diff changeset
101
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 sub equals {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
103 if ( defined $_[0] ) {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
104 return 0 if ( not defined $_[1] );
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
105
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 return $_[0] == $_[1];
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
107 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
108 else {
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 return 0 if defined $_[1];
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
110
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 return 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 sub equals_s {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
116 if ( defined $_[0] ) {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
117 return 0 if ( not defined $_[1] );
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
118
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 return $_[0] eq $_[1];
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
120 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
121 else {
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122 return 0 if defined $_[1];
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
123
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 return 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128 sub hashDiff {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
129 my ( $src, $dst ) = @_;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
130
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
131 $dst = $dst ? {%$dst} : {};
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132 $src ||= {};
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
133
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134 my %result;
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
135
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136 foreach my $key ( keys %$src ) {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
137 if ( exists $dst->{$key} ) {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
138 $result{"+$key"} = $dst->{$key}
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
139 unless equals_s( $dst->{$key}, $src->{$key} );
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140 delete $dst->{$key};
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
141 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
142 else {
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143 $result{"-$key"} = 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
145 }
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
146
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
147 $result{"+$_"} = $dst->{$_} foreach keys %$dst;
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
148
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149 return \%result;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
150 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
151
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
152 sub hashMerge {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
153 return hashApply( { %{ $_[0] || {} } }, $_[1] );
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
154 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
155
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
156 sub hashApply {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
157 my ( $target, $diff ) = @_;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
158
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
159 return $target unless ref $diff eq 'HASH';
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
160
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
161 while ( my ( $key, $value ) = each %$diff ) {
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
162 $key =~ /^(\+|-)?(.*)$/;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
163 my $op = $1 || '+';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
164 $key = $2;
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
165
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
166 if ( $op eq '-' ) {
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
167 delete $target->{$key};
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
168 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
169 else {
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
170 $target->{$key} = $value;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
171 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
172 }
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
173
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
174 return $target;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
175 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
176
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
177 sub hashCompare {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
178 my ( $l, $r, $cmp ) = @_;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
179
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
180 $cmp ||= \&equals_s;
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
181
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
182 return 0 unless scalar keys %$l == scalar keys %$r;
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
183 &$cmp( $l->{$_}, $r->{$_} ) || return 0 foreach keys %$l;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
184
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
185 return 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
186 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
187
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
188 sub hashParse {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
189 my ( $s, $p, $d ) = @_;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
190
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
191 $p = $p ? qr/$p/ : qr/\n+/;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
192 $d = $d ? qr/$d/ : qr/\s*=\s*/;
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
193
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
194 return { map split( $d, $_, 2 ), split( $p, $s ) };
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
195 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
196
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
197 sub hashSave {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
198 my ( $hash, $p, $d ) = @_;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
199
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
200 return "" unless ref $hash eq 'HASH';
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
201
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
202 $p ||= "\n";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
203 $d ||= " = ";
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
204
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 419
diff changeset
205 return join( $p, map( join( $d, $_, $hash->{$_} ), keys %$hash ) );
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
206 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
207
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
208 1;