Hi Monks, So I've been having this problem where I am sampling something (say temperature) but the rate at which i want to sample needs to be variable. I setup an entry box so the user can enter the desired time, and start stop and restart buttons. Everything works great the first time through, set the timer to 2000, and it samples every 2 seconds. Stop and restart work as well. But if i try and change the value and sample again, it yields erratic behavior. It looks like it might be creating another instance of the repeat. Any ideas on what I am doing wrong? Thank you for you time.
#!/usr/bin/perl use Tk; use strict; use warnings; use subs qw/samp destruct restartsamp/; my $mw = MainWindow->new; $mw->minsize(400,300); $mw->maxsize(1024,768); my $srate; my $dosample = 1; my $smpratelbl = $mw->Label(-text=> "Enter Sample Rate [msec]"); my $smprate = $mw->Entry(-textvariable=>\$srate); my $startsmp = $mw->Button(-text => "Sample/Change Rate", -command => sub{my $smp = $mw->repeat( +$srate, \&samp)}); my $stopsmp = $mw->Button(-text => "Stop Sampling", -command => \&destruct); my $restartsmp = $mw->Button(-text => "Restart Sampling", -command => \&restartsamp); $smpratelbl->pack; $smprate->pack; $startsmp->pack; $restartsmp->pack; $stopsmp->pack; MainLoop; sub destruct(){ $dosample = 0; } sub restartsamp(){ $dosample = 1; } sub samp(){ my $temp1; if($dosample == 1){ my $temp1 = localtime; print "$temp1 $srate\n"; my $temp2 = "$temp1 " . "$srate\n"; } }
foreach(@the_wicked){sleep(0);}

In reply to Tk::Repeat - Variable Rate? by transiency

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.