RE: [code-review] Tie::Hash::Expire
"Hodges, Paul" <Paul.Hodges-ntWzkUHD06N8olp/[email protected]> Mon, 2 Feb 2004 10:35:15 -0600
| Newsgroups | gmane.comp.lang.perl.code-review-ladder |
|---|---|
| Message-ID | <2820B295B95836498EED98D7A065C4F104DBD503@bremocmg-55> |
> Yesterday I uploaded the first version of my first CPAN module,=20
> Tie::Hash::Expire, a package providing hash keys that expire=20
> and cease to exist after a user-set period. I would be much
> obliged if anyone here would take a look.
A couple of comments, including a request for clarification -- and =
Japhy, welcome aboard. :)
1) My first thought was "Why not use an alarm?" After I thought about it =
a little, I wanted to pat you on the back for not falling into that =
trap. :)
2) I agree with a previous post: the default behavior should be that =
without an expiration period, keys should not expire.
3) I'd tend to use a more oo-ish approach, and put the data in a normal =
hash. For example
$my_tied_hash{foo} =3D 1; # $internal_hash_entry{foo} =3D { =
_data=3D>1, _expires=3D>time() };
or more likely
$my_tied_hash{foo} =3D 1; # $internal_hash_entry{foo} =3D [ 1, =
time() ];
4) I'd also recommend a custom timing function:
$tied_hash->set(foo =3D> 17); # THIS entry expires in 17 seconds.
which might be implemented more as
$tied_hash->set(foo =3D> 17); # $internal_hash_entry{foo} =3D [ 1, =
time(), 17 ];
leaving other entries as
$my_tied_hash{foo} =3D 1; # $internal_hash_entry{foo} =3D [ 1, =
time(), $default ];
Depending on how elaborate you wanted to get, you might even add some =
convenience shortcuts:
use Tie::Hash::Expire default =3D> 10, shortcuts =3D> 'all', reqs =
=3D> [ qw( Time::HiRes ) ];
$h =3D tie %h, Tie::Hash::Expire;
$h{foo} =3D 1; # expires in the default 10 seconds;
$h->set(bar =3D> 17); # expires in 17 seconds
# for the precise-minded: expires in 02:13:41.11
$h->set(baz =3D> hours(2)+minutes(13)+seconds(41)+fracsec(.11) ); # =
or somesuch
You might even include days, weeks, months, etc. That may work better =
with the Exporter, but I like the idea of being able to set the =
expiration default in the use() statement.=20
Obviously, I'm just tossing out ideas. Caveat auditor. :)
Paul
*****
"The information transmitted is intended only for the person or entity =
to which it is addressed and may contain confidential, proprietary, =
and/or privileged material. Any review, retransmission, dissemination =
or other use of, or taking of any action in reliance upon, this =
information by persons or entities other than the intended recipient is =
prohibited. If you received this in error, please contact the sender =
and delete the material from all computers." 113