[Charlotte.PM] Code inside Perl Tk

Christopher Fowler cfowler at outpostsentinel.com
Fri Aug 17 07:05:50 PDT 2007


Are there any Perl TK wizards here?

I'm playing with Perl Tk to create a GUI that selects on many sockets
and displays messages from remote servers.  The problem I'm having is
understanding where I need to execute my select loop.  It seems all the
examples I've looked at shows the building of the GUI and then the
execution of 'MainLoop'.  At that point I no longer have control of the
program.  Where would I actually place this pseudo code?

sub monitor {
 while(1) {
   my @ready = $selector->can_read();
   # Display messages...
 }
}

I tried placing it in a thread and then build GUI elements in that
thread while MainLoop was running.  Xlib did not like that much.
$MW = MainWindow->new;

######################
#
# Load any images and fonts
#
######################
ZloadImages();
ZloadFonts ();


#$MW->setTitle("SAM Alarm Services");


# Widget HList1 isa HList
$ZWIDGETS{'HList1'} = $MW->HList()->grid(
   -row    => 0,
   -column => 0,
  );


# Widget Frame1 isa Frame
$ZWIDGETS{'Frame1'} = $MW->Frame()->grid(
   -row    => 0,
   -column => 1,
  );

sub child {
  my $red = $ZWIDGETS{'HList1'}->ItemStyle( 'text', -foreground =>
'#800000');
  my $sas = config();

  foreach my $ref (keys %{$sas}) {
    my $c = undef;
    $ref = $sas->{$ref};
    next unless uc($ref->{'a'}) eq 'TRUE';
    my $tree = $ZWIDGETS{'HList1'}->addchild('');
    $ZWIDGETS{'HList1'}->itemCreate( $tree, 0, -itemtype => 'text',
-text => "$ref->{'n'}", -style => $red );
  }

  while(1) { sleep 60; }
}

my $thread  = threads->create("child",undef);
$thread->detach();

###############
#
# MainLoop
#
###############

MainLoop;


My plan was to do all the network code in the whil(1) loop above.




More information about the charlotte mailing list