in reply to A value that stays a value (for some time)
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?
|
|---|
| 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 | |
by liverpole (Monsignor) on Jun 18, 2006 at 15:15 UTC | |
by freakingwildchild (Scribe) on Jun 18, 2006 at 16:07 UTC |