return0 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks! I was wondering if I could steal your fancy brains for a moment! I have written a few Curses::UI apps and realized that they look like stop animation slow pokes when running. The read from streams that are updated in milliseconds but i cannot seem to update the label's text fields in the windows faster than once per second :/ I looked at the sauce for UI.pm and saw the set_timer() function and that it (can) take three parameters, but no matter what I pass to it, it only refreshes the windows text labels each second. Here is a simple example that doesn't update fields but will print over the curses window "called!" each second the timer calls it:

#!/usr/bin/perl -w use strict; use Curses::UI; my $date = localtime(); my $n = 1; # i've tried -2,-1,0,0.1,0.2,...,<1 my $cui = new Curses::UI(-color_support=>1); my $titlewid = $cui->add('titlew','Window',-border=>0,-height=>1,-widt +h=>34); $titlewid->add('titlel', 'Label',-text =>"my slow motion app!",-bold = +> 1,-fg=>"blue",-height=>1); my $widclock = $cui->add('clock','Window',-border=>0,-height=>1,-x=>35 +,-width=>25); $winclock::label = $widclock->add('wclock','Label',-text=>$date,-bold= +>0,height=>1,-width=>25); $winclock::label->draw(); $cui->set_binding( sub{exit}, "\cC"); $cui->set_timer('update_timer',\&call,$n); # call back in here $cui->mainloop(); sub call{ print "called!\n"; }

The source of UI.pm has:

sub set_timer($$;) { my $self = shift; my $id = shift; my $callback = shift; my $time = shift || 1; $self->fatalerror( "add_timer(): callback is no CODE reference" ) unless defined $callback and ref $callback eq 'CODE'; $self->fatalerror( "add_timer(): id is not set" ) unless defined $id; my $config = { -time => $time, -callback => $callback, -enabled => 1, -lastrun => time(), }; $self->{-timers}->{$id} = $config; $self->set_read_timeout; return $self; }

(it is here CPAN UI.pm)

Do you guys think there is some way I could hack it to "sleep" for less than a second? maybe 100 milliseconds? I don't wanna alter the pm and recompile because i would like my code to be easily redistributable :( Without the set_timer() function i have no other idea how to update the fields in "real" time. I tried using Sub::Override, but had no luck. Any ideas?

Thanks so much!

Replies are listed 'Best First'.
Re: timing for curses::UI and set_timer()
by return0 (Acolyte) on Apr 13, 2013 at 02:55 UTC

    I think I've got it! :D I sent it $n = '(1 - 1)' into the function as the last argument, and it's !blazing! fast now! :D :D I won't mark this resolved yet because i think it DoS'ed my gpsd heh... just in case... This is a dirty hack and all, LOL

      It works, but we still need a small millisecond sleep to not strain the CPU. but you can send anything into the PM, i just used ')' now. Thanks again!