Re: [code-review] Hello!
Jean-Michel Hiver <[email protected]> Mon, 02 Feb 2004 09:16:24 +0000
| Newsgroups | gmane.comp.lang.perl.code-review-ladder |
|---|---|
| Message-ID | <[email protected]> |
Jeff Yoak wrote:
> At 03:50 AM 1/31/2004, Jean-Michel Hiver wrote:
>
>> That's quite an fun / interesting module. It would be nice to allow
>> expiration times under one second when Time::HiRes is installed.
>
>
> Yes, I thought about that. What is your suggestion about the best way
> to go about it? I don't like requiring Time::HiRes. It's not in the
> core, is it? I have it, but don't recall if I installed it
> personally. I could allow decimal times (actually, I don't prevent
> them from being entered now), and simply treat them as they are now
> unless I have access to Time::HiRes. Probably a warning would be
> appropriate if the user specifies a decimal value but doesn't have
> Time::HiRes available.
Something like this in your module would do:
package Tie::Hash::Expire;
use strict;
use warnings;
use POSIX qw/ceil/;
use Carp;
our $TIME_HIRES_INSTALLED = 0;
eval { "use Time::HiRes qw()" };
$TIME_HIRES_INSTALLED = 1 unless ($@);
Then if $TIME_HIRES_INSTALLED is equals to 1 you can use the functions
of Time::HiRes:: rather than the perl core functions.
Time::HiRes::time()
Time::HiRes::sleep()
Time::HiRes::alarm()
etc. See perldoc Time::HiRes.
Well, that's how I'd do it anyway. I'm sure others on the list will
correct me if it's not optimal :)
Cheers,
Jean-Michel.