line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Tie::Scalar::Decay; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
795
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
30
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
1057992
|
use Time::HiRes qw(time); # if Time::HiRes isn't available for your |
|
1
|
|
|
|
|
2526
|
|
|
1
|
|
|
|
|
6
|
|
6
|
|
|
|
|
|
|
# system, then you can comment this out and |
7
|
|
|
|
|
|
|
# it'll still work mostly. However, test |
8
|
|
|
|
|
|
|
# five will fail for obvious reasons |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
my $VERSION='1.1.1'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub TIESCALAR { |
13
|
2
|
|
|
2
|
|
249
|
my $class = shift; |
14
|
2
|
|
|
|
|
21
|
my $self = { |
15
|
|
|
|
|
|
|
VALUE => undef, |
16
|
|
|
|
|
|
|
PERIOD => 5, |
17
|
|
|
|
|
|
|
FUNCTION => \&_defaultdecayfunction, |
18
|
|
|
|
|
|
|
@_ |
19
|
|
|
|
|
|
|
}; |
20
|
2
|
|
|
|
|
10
|
$self->{CREATION_TIME}=time; |
21
|
|
|
|
|
|
|
|
22
|
2
|
50
|
|
|
|
19
|
die("Tie::Scalar::Decay - invalid PERIOD.\n") |
23
|
|
|
|
|
|
|
unless($self->{PERIOD} =~ /^([0-9.]+)([smhd]?)$/); |
24
|
|
|
|
|
|
|
|
25
|
2
|
|
|
|
|
22
|
return bless $self, $class; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub FETCH { |
29
|
5
|
|
|
5
|
|
13001570
|
my $self = shift; |
30
|
5
|
|
|
|
|
34
|
my $value=$self->{VALUE}; |
31
|
|
|
|
|
|
|
|
32
|
5
|
100
|
|
|
|
132
|
return $value if(time < $self->{CREATION_TIME} + $self->_get_period()); |
33
|
|
|
|
|
|
|
|
34
|
3
|
|
|
|
|
16
|
foreach(1..$self->_get_periods()) { |
35
|
5
|
100
|
|
|
|
25
|
if(ref($self->{FUNCTION}) eq 'CODE') { |
36
|
1
|
|
|
|
|
4
|
$value=&{$self->{FUNCTION}}($value); |
|
1
|
|
|
|
|
5
|
|
37
|
|
|
|
|
|
|
} else { |
38
|
4
|
|
|
|
|
308
|
eval($self->{FUNCTION}); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} |
41
|
3
|
|
|
|
|
14
|
return $value; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub STORE { |
45
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
46
|
0
|
|
|
|
|
0
|
$self->{VALUE} = shift; |
47
|
0
|
|
|
|
|
0
|
$self->{CREATION_TIME}=time; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _defaultdecayfunction { |
51
|
1
|
|
|
1
|
|
3
|
my $value=$_[0]; |
52
|
1
|
50
|
|
|
|
8
|
if($value=~/^-?[0-9\.]+$/) { return $value/2; } |
|
1
|
|
|
|
|
6
|
|
53
|
0
|
|
|
|
|
0
|
else { return $value; } |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub _get_periods { |
57
|
3
|
|
|
3
|
|
5
|
my $self=shift; |
58
|
3
|
|
|
|
|
14
|
my $elapsedtime=time-$self->{CREATION_TIME}; |
59
|
|
|
|
|
|
|
|
60
|
3
|
|
|
|
|
8
|
return int($elapsedtime/$self->_get_period()); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub _get_period { |
64
|
8
|
|
|
8
|
|
18
|
my $self=shift; |
65
|
8
|
|
|
|
|
21
|
my $period=$self->{PERIOD}; |
66
|
8
|
|
|
|
|
65
|
my %mult = ( |
67
|
|
|
|
|
|
|
's'=>1, |
68
|
|
|
|
|
|
|
'm'=>60, |
69
|
|
|
|
|
|
|
'h'=>3600, # 60*60 |
70
|
|
|
|
|
|
|
'd'=>86400, # 60*60*24 |
71
|
|
|
|
|
|
|
); |
72
|
8
|
|
100
|
|
|
71
|
$period=~s/^([0-9\.]+)([smhd]?)$/$1 * (($mult{$2})||1)/e; |
|
8
|
|
|
|
|
263
|
|
73
|
8
|
|
|
|
|
70
|
return $period; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
1; |
77
|
|
|
|
|
|
|
__END__ |