in reply to A value that stays a value (for some time)

Hi freakingwildchild,

Have you considered using a closure for this?  It sounds like you want the value to not change for at least 10 minutes after the first time you create the value; if that's the case, I'd suggest something like:

#!/usr/bin/perl -w # Strict use strict; use warnings; # Test code my $psub = same_value_for_time_period(5); while (1) { my $x = $psub->(); printf "Value is %s\n", $x; sleep 1; } # Subroutines sub same_value_for_time_period { my ($duration) = @_; # Eg. 600 = 10 minutes my $start_time = time; my $value = get_new_value(); sub { my $now = time; if ($now - $start_time < $duration) { return $value; } else { $start_time = $now; $value = get_new_value(); } } } sub get_new_value { my $value; # Your code for creating a new value here ... $value = rand 1000; return $value; }

Here, you call the subroutine same_value_for_time_period with a duration (the number of seconds you wish the value to remain constant for).  It returns a pointer to a closure; a subroutine which, each time you call it after it's first created, gives you the same value within the specified duration, and then changes after that.  The subroutine get_new_value is called when the duration is up, to give a new value.  As you can see, I've tested it for a short duration (5 seconds), and just using a random number whenever the value changes.

Is that the kind of thing you're looking for?


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Replies are listed 'Best First'.
Re^2: A value that stays a value (for some time)
by freakingwildchild (Scribe) on Jun 18, 2006 at 14:53 UTC
    This code has some interesting ideas for running a server returning such value; but; this would mean it isn't a stateless number calculated for that moment ; if I restart the proces (which happens in my cgi's, I don't use mod_perl (yet)) it will change numbers

    It would give me the same results if I would do a time() and change the last 2 digits to a 00 and use that as "current string for this time"; what I need is a sort of "random-but-still-the-same for 10 minutes".

    Next to that the "five-before-twelve problem" is still in the air with both methods...

      I'm unable to see how you can choose a stateless value which is guaranteed to not change for a finite period of time.

      Since the number is stateless, it must depend on some external reference (eg. a timet).  However, you want the value to change, there's no way to avoid getting the value at some arbitrary number of seconds N prior to the number changing, for some N less than the duration you're interested in (here 600 seconds).  So unless I've misunderstood your requirements, I don't see how you what you're looking for is possible.

      Now, if you're willing to accept numbers which are the same for a 10-minute period, just use:

      my $value = int(time / 600);

      But, of course, you still have the problem of having the value change very soon after you choose it.


      s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
        That's what I currently got; divided by the seconds but want it to be valid for the time it started without writing something to the server; it would be related only to time and the tables I attach to those times.