Tie::Scalar::Timeout - Scalar variables that time out
use Tie::Scalar::Timeout;
tie my $k, 'Tie::Scalar::Timeout', EXPIRES => '+2s';
$k = 123; sleep(3); # $k is now undef
tie my $m, 'Tie::Scalar::Timeout', NUM_USES => 3, VALUE => 456;
tie my $n, 'Tie::Scalar::Timeout', VALUE => 987, NUM_USES => 1,
POLICY => 777;
tie my $p, 'Tie::Scalar::Timeout', VALUE => 654, NUM_USES => 1,
POLICY => \&expired;
sub expired { $is_expired++ }
This module allows you to tie a scalar variable whose value will be reset (subject to an expiry policy) after a certain time and/or a certain number of uses. One possible application for this module might be to time out session variables in mod_perl programs.
When tying, you can specify named arguments in the form of a hash. The following named parameters are supported:
EXPIRESEXPIRES to specify an interval or absolute time after which the
value will be reset. (Technically, the value will still be there, but the
module's FETCH sub will return the value as dictated by the expiry policy.)
Values for the EXPIRES field are modelled after Netscape's cookie
expiration times. Except, of course, that negative values don't really make
sense in a universe with linear, one-way time. The following forms are all
valid for the EXPIRES field:
+30s 30 seconds from now
+10m ten minutes from now
+1h one hour from now
+3M in three months
+10y in ten years time
25-Apr-2001 00:40:33 at the indicated time & date
Assigning a value to the variable causes EXPIRES to be reset to the
original value.
VALUEVALUE hash key, you can specify an initial value for the
variable.
NUM_USESEXPIRES, you can also specify a maximum
number of times the variable may be read from before it expires. If both
EXPIRES and NUM_USES are set, the variable will expire when either
condition becomes true. If NUM_USES isn't set or set to a negative
value, it won't influence the expiry process.
Assigning a value to the variable causes NUM_USES to be reset to the
original value.
POLICYundef
after it has expired. You can specify either a scalar value or a code
reference as the value of the POLICY parameter. If you specify a scalar
value, that value will be returned after the variable has expired. Thus,
the default expiration policy is equivalent to
POLICY => undef
If you specify a code reference as the value of the POLICY parameter,
that code will be called when the variable value is FETCH()ed after it
has expired. This might be used to set some other variable, or reset the
variable to a different value, for example.
None known so far. If you find any bugs or oddities, please do tell me about them.
Marcel Grünauer <marcel@codewerk.com>
Copyright 2000 Marcel Grünauer. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
perl(1), Tie::Scalar(3pm).