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$..$/

In reply to Re: A value that stays a value (for some time) by liverpole
in thread A value that stays a value (for some time) by freakingwildchild

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.