This file is indexed.

/usr/share/perl5/Tie/Hash/Expire.pm is in libtie-hash-expire-perl 0.03-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package Tie::Hash::Expire;

use strict;

use POSIX qw/ceil/;
use Carp;

use vars qw($VERSION $HI_RES_AVAILABLE);

$VERSION = '0.03';

BEGIN {
	eval "use Time::HiRes qw/time/";
	unless($@){
		$HI_RES_AVAILABLE = 1;
	}
}

$Tie::Hash::Expire::clean_int = 180; # Maybe later, the user can set this.

sub TIEHASH {
	my $class = shift;
	my $args = shift || {};

	# TODO: What do we do without $args->{expire_seconds}
	unless(exists $args->{expire_seconds}){
		carp "hash tied to Tie::Hash::Expire without specifying expire_seconds.  Hash keys will not expire.";
	}
	if(!$HI_RES_AVAILABLE and $args->{expire_seconds} =~ /\.\d+/){
		carp "expire_seconds appears to be a decimal number, but Time::HiRes is not available.";
	}

	my $self = {
		'last_clean'	=>	time,
		'clean_int'	=>	$Tie::Hash::Expire::clean_int,
		'hash'		=>	{},
		'array'		=>	[],
		'lifespan'	=>	$args->{expire_seconds},
	};
	bless $self, $class;
	return $self;
}

sub STORE {

	my $self = shift;
	my $key = shift;
	my $value = shift;

	my $time = time;

	$self->maybe_clean();

	$self->DELETE($key);

	# Insert it on the end.
	push @{$self->{array}}, [$time,$key,$value];
	$self->{hash}->{$key} = $#{$self->{array}};

}

sub FETCH {

	my $self = shift;
	my $key = shift;

	$self->maybe_clean();

	if(exists $self->{hash}->{$key}){
		# It exists, but may be expired.
		my $time = time;

		my $index = $self->{hash}->{$key};
		if((defined $self->{lifespan}) and $time - $self->{array}->[$index]->[0] >= $self->{lifespan}){
			# It is expired.
			$self->chop_hash($index);
			return undef;
		}
		# It is not expired.
		return $self->{array}->[$index]->[2];
	} else {
		return undef;
	}
}

sub EXISTS {

	my $self = shift;
	my $key = shift;

	$self->maybe_clean();

	if(exists $self->{hash}->{$key}){
		# It exists, but may be expired.
		my $time = time;

		my $index = $self->{hash}->{$key};
		if(defined $self->{lifespan} and $time - $self->{array}->[$index]->[0] >= $self->{lifespan}){
			# It is expired.
			$self->chop_hash($index);
		}
	}

	return exists $self->{hash}->{$key};

}

sub DELETE {

	my $self = shift;
	my $key = shift;

	$self->maybe_clean();

	if(exists($self->{hash}->{$key})){
		splice @{$self->{array}}, $self->{hash}->{$key},1;
		$self->rebuild_hash();
	}
}

sub CLEAR {

	my $self = shift;

	$self->{hash} = {};
	$self->{array} = [];
	$self->{last_clean} = time;

}

sub FIRSTKEY {

	my $self = shift;
	$self->clean_house();

	if(scalar @{$self->{array}}){
		my $key = $self->{array}->[0]->[1];
		$self->{curr_key} = 0;
		return $key;
	} else {
		return undef;
	}
}

sub NEXTKEY {

	my $self = shift;

	my $chopped = $self->clean_house();

	# First, update $self->{curr_key}
	$self->{curr_key}++;

	if(defined $chopped){	# The hash has changed while iterating.
		if($self->{curr_key} <= $chopped){	# Start over
			$self->{curr_key} = 0;
		} else {				# Adjust number
			$self->{curr_key} = ($self->{curr_key}-$chopped)-1;
		}
	}

	# Return the right thing:
	if($self->{curr_key} <= $#{$self->{array}}){
		return $self->{array}->[$self->{curr_key}]->[1];
	} else {
		return undef;
	}
}

sub clean_house {

	my $self = shift;

	# Locate the first expired datum and chop there.
	# Return the index of the first chopped key, or undef if no chop
	# occurred.

	unless(defined $self->{lifespan}){
		return undef;
	}

	my $max = $#{$self->{array}};
	my $min = -1;
	my $time = time;
 	$self->{last_clean} = $time;

	while($max > $min){
		my $try = ceil(($max+$min)/2);
		if($time - $self->{array}->[$try]->[0] >= $self->{lifespan}){
			$min = $try;
		} else {
			$max = $try-1;
		}
	}
	if($min>=0){
		$self->chop_hash($min);
		return $min;
	} else {
		return undef;
	}

}

sub maybe_clean {

	my $self = shift;

	my $time = time;
	if($time - $self->{last_clean} >= $self->{clean_int}){
		$self->clean_house();
	}
}

sub chop_hash {

	my $self = shift;
	my ($index) = @_;

	# Eliminate all entries from the array at $index and before.

	if($index >= $#{$self->{array}}){
		@{$self->{array}} = ();
	} else {
		@{$self->{array}} = @{$self->{array}}[($index+1) .. $#{$self->{array}}];
	}

	$self->rebuild_hash();
}

sub rebuild_hash {

	my $self = shift;

	$self->{hash} = {
		map {$self->{array}->[$_]->[1], $_} (0..$#{$self->{array}})
	};
}
1;

__END__

=head1 NAME

Tie::Hash::Expire - Hashes with keys that expire after a user-set period.

=head1 SYNOPSIS

  use Tie::Hash::Expire;

  my %test;
  tie %test, 'Tie::Hash::Expire', {'expire_seconds' => 10};

  $test{'dog'} = 'doghouse';
  sleep 5;
  $test{'bird'} = 'nest';
  sleep 6;

  print keys %test, "\n";	# The only key is 'bird'

  my %hi_res;
  tie %hi_res, 'Tie::Hash::Expire', {'expire_seconds' => 5.21};
	# Decimal number of seconds works if you have Time::HiRes

=head1 ABSTRACT

Hashes tied to Tie::Hash::Expire have keys that cease to exist 'expire_seconds' after their most recent modification or their creation.

=head1 DESCRIPTION

Hashes tied to Tie::Hash::Expire behave like normal hashes in all respects except that when a key is added or the value associated with a key is changed, the current time is stored, and after 'expire_seconds' the key and value are removed from the hash.

Resolutions finer than seconds are available if the module finds access to Time::HiRes.  If Time::HiRes is available, you can expect expiration to be accurate to 0.001 seconds.  You may specify 'expire_seconds' to be decimal numbers like 5.12 .  If Time::HiRes is available, this number will be used precisely.  If you specify a decimal number and don't have access to Time::HiRes, a warning is generated and the code will function as though you specified the next higher integer.

The number of seconds specified by 'expire_seconds' is taken to mean an absolute maximum lifespan for the key, at the resolution described above.  In other words, if you set 'expire_seconds' to 1 second, and do not have Time::HiRes, keys could expire as quickly as the next machine instruction, but will not last longer than 1 second.

=head1 AUTHOR

Jeff Yoak, E<lt>jeff@yoak.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright 2004 by Jeff Yoak

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself. 

=cut