line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::MonM::Util; # $Id: Util.pm 85 2019-07-14 12:03:14Z abalama $ |
2
|
2
|
|
|
2
|
|
12
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
46
|
|
3
|
2
|
|
|
2
|
|
7
|
use utf8; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
13
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=encoding utf-8 |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
App::MonM::Util - Internal utilities |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 VERSION |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Version 1.01 |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use App::MonM::Util qw/ |
18
|
|
|
|
|
|
|
explain expire_calc |
19
|
|
|
|
|
|
|
/; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
print explain( $object ); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 DESCRIPTION |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
Internal utilities |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 FUNCTIONS |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=over 4 |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=item B |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
print explain( $object ); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Returns Data::Dumper dump |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=item B, B, B, B, B |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
print cyan("Format %s", "text"); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Returns colored string |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=item B |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
print getExpireOffset("+1d"); # 86400 |
46
|
|
|
|
|
|
|
print getExpireOffset("-1d"); # -86400 |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
Returns offset of expires time (in secs). |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
Original this function is the part of CGI::Util::expire_calc! |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
This internal routine creates an expires time exactly some number of hours from the current time. |
53
|
|
|
|
|
|
|
It incorporates modifications from Mark Fisher. |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
format for time can be in any of the forms: |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
now -- expire immediately |
58
|
|
|
|
|
|
|
+180s -- in 180 seconds |
59
|
|
|
|
|
|
|
+2m -- in 2 minutes |
60
|
|
|
|
|
|
|
+12h -- in 12 hours |
61
|
|
|
|
|
|
|
+1d -- in 1 day |
62
|
|
|
|
|
|
|
+3M -- in 3 months |
63
|
|
|
|
|
|
|
+2y -- in 2 years |
64
|
|
|
|
|
|
|
-3m -- 3 minutes ago(!) |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
If you don't supply one of these forms, we assume you are specifying the date yourself |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=item B |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
print getBit(123, 3) ? "SET" : "UNSET"; # UNSET |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Getting specified Bit |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=item B |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my $anode = node2anode({}); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Returns array of nodes |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=item B |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
my $hash = set2attr({set => ["AttrName Value"]}); # {"AttrName" => "Value"} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
Converts attributes from the "set" format to regular hash |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=item B |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
printf("%08b", setBit(123, 3)); # 01111111 |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
Setting specified Bit. Returns new value. |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=back |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 HISTORY |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
See C file |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 AUTHOR |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Serż Minus (Sergey Lepenkov) L Eabalama@cpan.orgE |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 COPYRIGHT |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Copyright (C) 1998-2019 D&D Corporation. All Rights Reserved |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 LICENSE |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
109
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
See C file and L |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=cut |
114
|
|
|
|
|
|
|
|
115
|
2
|
|
|
2
|
|
68
|
use vars qw/ $VERSION @EXPORT @EXPORT_OK /; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
102
|
|
116
|
|
|
|
|
|
|
$VERSION = '1.01'; |
117
|
|
|
|
|
|
|
|
118
|
2
|
|
|
2
|
|
965
|
use Data::Dumper; #$Data::Dumper::Deparse = 1; |
|
2
|
|
|
|
|
10133
|
|
|
2
|
|
|
|
|
99
|
|
119
|
2
|
|
|
2
|
|
499
|
use Term::ANSIColor qw/ colored /; |
|
2
|
|
|
|
|
6632
|
|
|
2
|
|
|
|
|
636
|
|
120
|
2
|
|
|
2
|
|
13
|
use CTK::ConfGenUtil; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
121
|
|
121
|
2
|
|
|
2
|
|
10
|
use CTK::TFVals qw/ :ALL /; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
316
|
|
122
|
2
|
|
|
2
|
|
13
|
use CTK::Util qw/ trim /; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
110
|
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
use constant { |
125
|
2
|
|
|
|
|
101
|
BIT_SET => 1, |
126
|
|
|
|
|
|
|
BIT_UNSET => 0, |
127
|
2
|
|
|
2
|
|
12
|
}; |
|
2
|
|
|
|
|
3
|
|
128
|
|
|
|
|
|
|
|
129
|
2
|
|
|
2
|
|
10
|
use base qw/Exporter/; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1328
|
|
130
|
|
|
|
|
|
|
@EXPORT = qw/ |
131
|
|
|
|
|
|
|
blue green red yellow cyan |
132
|
|
|
|
|
|
|
/; |
133
|
|
|
|
|
|
|
@EXPORT_OK = qw/ |
134
|
|
|
|
|
|
|
explain |
135
|
|
|
|
|
|
|
getExpireOffset |
136
|
|
|
|
|
|
|
node2anode set2attr |
137
|
|
|
|
|
|
|
getBit setBit |
138
|
|
|
|
|
|
|
/; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub explain { |
141
|
0
|
|
|
0
|
1
|
0
|
my $dumper = new Data::Dumper( [shift] ); |
142
|
0
|
|
|
|
|
0
|
$dumper->Indent(1)->Terse(1); |
143
|
0
|
0
|
|
|
|
0
|
$dumper->Sortkeys(1) if $dumper->can("Sortkeys"); |
144
|
0
|
|
|
|
|
0
|
return $dumper->Dump; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
sub getExpireOffset { |
147
|
0
|
|
0
|
0
|
1
|
0
|
my $time = trim(shift // 0); |
148
|
0
|
|
|
|
|
0
|
my %mult = ( |
149
|
|
|
|
|
|
|
's' => 1, |
150
|
|
|
|
|
|
|
'm' => 60, |
151
|
|
|
|
|
|
|
'h' => 60*60, |
152
|
|
|
|
|
|
|
'd' => 60*60*24, |
153
|
|
|
|
|
|
|
'M' => 60*60*24*30, |
154
|
|
|
|
|
|
|
'y' => 60*60*24*365 |
155
|
|
|
|
|
|
|
); |
156
|
0
|
0
|
0
|
|
|
0
|
if (!$time || (lc($time) eq 'now')) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
157
|
0
|
|
|
|
|
0
|
return 0; |
158
|
|
|
|
|
|
|
} elsif ($time =~ /^\d+$/) { |
159
|
0
|
|
|
|
|
0
|
return $time; # secs |
160
|
|
|
|
|
|
|
} elsif ($time=~/^([+-]?(?:\d+|\d*\.\d*))([smhdMy])/) { |
161
|
0
|
|
0
|
|
|
0
|
return ($mult{$2} || 1) * $1; |
162
|
|
|
|
|
|
|
} |
163
|
0
|
|
|
|
|
0
|
return $time; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
sub node2anode { |
167
|
0
|
|
|
0
|
1
|
0
|
my $n = shift; |
168
|
0
|
0
|
0
|
|
|
0
|
return [] unless $n && ref($n) =~ /ARRAY|HASH/; |
169
|
0
|
0
|
|
|
|
0
|
return [$n] if ref($n) eq 'HASH'; |
170
|
0
|
|
|
|
|
0
|
return $n; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
sub set2attr { |
173
|
1
|
|
|
1
|
1
|
2
|
my $in = shift; |
174
|
1
|
50
|
|
|
|
6
|
my $attr = is_array($in) ? $in : array($in => "set"); |
175
|
1
|
|
|
|
|
9
|
my %attrs; |
176
|
1
|
|
|
|
|
2
|
foreach (@$attr) { |
177
|
3
|
50
|
|
|
|
17
|
$attrs{$1} = $2 if $_ =~ /^\s*(\S+)\s+(.+)$/; |
178
|
|
|
|
|
|
|
} |
179
|
1
|
|
|
|
|
14
|
return {%attrs}; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
sub setBit { |
182
|
0
|
|
|
0
|
1
|
|
my $v = fv2zero(shift); |
183
|
0
|
|
|
|
|
|
my $n = fv2zero(shift); |
184
|
0
|
|
|
|
|
|
return $v | (2**$n); |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
sub getBit { |
187
|
0
|
|
|
0
|
1
|
|
my $v = fv2zero(shift); |
188
|
0
|
|
|
|
|
|
my $n = fv2zero(shift); |
189
|
0
|
0
|
|
|
|
|
return ($v & (1 << $n)) ? BIT_SET : BIT_UNSET; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
# Colored helper functions |
193
|
0
|
|
|
0
|
1
|
|
sub green { colored(['bright_green'], sprintf(shift, @_)) } |
194
|
0
|
|
|
0
|
1
|
|
sub red { colored(['bright_red'], sprintf(shift, @_)) } |
195
|
0
|
|
|
0
|
1
|
|
sub yellow { colored(['bright_yellow'], sprintf(shift, @_)) } |
196
|
0
|
|
|
0
|
1
|
|
sub cyan { colored(['bright_cyan'], sprintf(shift, @_)) } |
197
|
0
|
|
|
0
|
1
|
|
sub blue { colored(['bright_blue'], sprintf(shift, @_)) } |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
1; |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
__END__ |