Re: [code-review] Hello!

Paul Hoffman <[email protected]> Tue, 3 Feb 2004 14:22:21 -0500
Newsgroups gmane.comp.lang.perl.code-review-ladder
Message-ID <[email protected]>
On Sun, 01 Feb 2004 17:35:20 -0800, Jeff Yoak <[email protected]> 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.

Others have very ably recommended ways to test for Time::HiRes.  I'd 
simply
add that you might want to use `use vars' instead of `our' since the 
latter
won't work on all versions of Perl 5.x (I think the change happened in 
5.6.0,
but I may be wrong) --

     use vars qw($have_time_hires);
     eval "use Time::HiRes qw()";
     $have_time_hires = $@ eq '';

Also, this probably won't help you, but you might consider using 
randomness
to obtain a timeout that *averages* to the desired value --

     sub approx {
         my ($t) = @_;
         return $t if $have_time_hires;
         my $int_t = int $t;
         $int_t ++ if rand() < $t - $int_t;
         return $int_t;
     }

The idea is, if you call approx(1.5) a hundred times, you'll get 1 about
half of the time and 2 the rest of the time.

(I use this when throttling down tests that hit the 'net.)

Practically speaking, something like this would probably make more 
sense --

     $timeout = int($timeout + 0.999) unless $have_time_hires;

HTH,

Paul.

--
Paul Hoffman :: Taubman Medical Library :: Univ. of Michigan
[email protected] :: [email protected] :: http://www.nkuitse.com/