Since i've been stumped by this before, and so have probably many others, i want to show how to do external event loops (main loops) when using Curses::UI.

By external i mean using your own, not the one provided by Curses::UI.

This is somewhat loosely based on some of the tutorial code included in the package to make the window as well as some quickly hacked together code. Sorry about that. Control-X opens the menu, Control-Q quits the application.

First of all, we need to make sure we got a read timeout for the input. So, make sure this is set correctly right after getting a new UI instance:

use Curses::UI; my $cui = new Curses::UI( -color_support => 1); $cui->{-read_timeout} = 0;

In our (rather simple) main loop, whenever we update the screen, we call draw() and do_one_event(). In every loop, we also call do_one_event() after our sleeping period to handle any user input:

my $last = time; while(1) { $cui->do_one_event(); my $now = time; if($last != $now) { $last = $now; foreach my $sensor (@sensors) { my $newtext = int(rand(1000) * 100) / 100; $sensor->text($newtext); } $cui->draw(); $cui->do_one_event(); } else { sleep(0.01); } }

Just for reference, here is the complete, runable example, in all its indent-mismatched-copy-and-paste glory:

#!/usr/bin/perl -w use strict; use warnings; use Time::HiRes qw[sleep]; use Curses::UI; my $cui = new Curses::UI( -color_support => 1); $cui->{-read_timeout} = 0; my @menu = ( { -label => 'File', -submenu => [ { -label => 'Exit ^Q', -value => \&exit_dialog } ] }, ); my $menu = $cui->add( 'menu','Menubar', -menu => \@menu, -fg => "white", -bg => "blue", ); my $win1 = $cui->add( 'win1', 'Window', -border => 1, -y => 1, -bfg => 'red', ); sub exit_dialog() { my $return = $cui->dialog( -message => "Do you really want to quit?", -title => "Are you sure???", -buttons => ['yes', 'no'], ); exit(0) if $return; } my @labels; my @sensors; for(my $i = 1; $i <= 3; $i++) { push @labels, $win1->add('label' . $i, 'Label', -width => 14, -height => 1, -text => 'Sensor ' . $i . ' :', -x => 4, -y => 3 + $i, ); push @sensors, $win1->add('sensordata' . $i, 'Label', -width => 10, -height => 1, -text => '----------', -x => 19, -y => 3 + $i, ); } $cui->set_binding(sub {$menu->focus()}, "\cX"); $cui->set_binding( \&exit_dialog , "\cQ"); my $last = time; while(1) { $cui->do_one_event(); my $now = time; if($last != $now) { $last = $now; foreach my $sensor (@sensors) { my $newtext = int(rand(1000) * 100) / 100; $sensor->text($newtext); } $cui->draw(); $cui->do_one_event(); } else { sleep(0.01); } }
perl -e 'use MIME::Base64; print decode_base64("4pmsIE5ldmVyIGdvbm5hIGdpdmUgeW91IHVwCiAgTmV2ZXIgZ29ubmEgbGV0IHlvdSBkb3duLi4uIOKZqwo=");'

In reply to External event loop for Curses::UI by cavac

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.