[Charlotte.PM] Perl Tk

Christopher Fowler cfowler at outpostsentinel.com
Sat Jun 18 19:03:43 PDT 2005


Today I was bored and decided to play around with Perl TK.  We have a
Web app written in J2EE and I'm thinking about creating a utility the
scrapes the pages and displays them in pseudo real time.  My problem is
that I've not figured out how to center the main window and ham hacing a
hard time seeing good examples on the net.

--- Cut Here ----
 sub start_app {
  my($org, $user, $pass, $server) = undef;

  my $login = MainWindow->new();
  $login->configure(-title => 'Login');
  $login->geometry("+100+300");
  my $frm1 = $login->Frame(
    -relief => "groove",
    -borderwidth => 3);
  $frm1->pack(-side => 'top', -fill => 'x');
  my $frm1_lbl1 = $frm1->Label (
    -text => 'Server Address:');
  $frm1_lbl1->pack( -side => 'left');
  $server = $frm1->Entry( -width => 32, -background => 'white');
  $server->pack( -side => 'right', -pady => 3);

  my $frm2 = $login->Frame(
    -relief => "groove",
    -borderwidth => 3);
  $frm2->pack(-side => 'top', -fill => 'x');
  my $frm2_lbl1 = $frm2->Label (
    -text => 'Organization:');
  $frm2_lbl1->pack( -side => 'left');
  $org = $frm2->Entry( -width => 32, -background => 'white');
  $org->pack( -side => 'right', -pady => 3);

  my $frm3 = $login->Frame(
    -relief => "groove",
    -borderwidth => 3);
  $frm3->pack(-side => 'top', -fill => 'x');
  my $frm3_lbl1 = $frm3->Label (
    -text => 'Username:');
  $frm3_lbl1->pack( -side => 'left');
  $user = $frm3->Entry( -width => 32, -background => 'white');
  $user->pack( -side => 'right', -pady => 3);

  my $frm4 = $login->Frame(
    -relief => "groove",
    -borderwidth => 3);
  $frm4->pack(-side => 'top', -fill => 'x');
  my $frm4_lbl1 = $frm4->Label (
    -text => 'Password:');
  $frm4_lbl1->pack( -side => 'left');
  $pass = $frm4->Entry( -width => 32, -background => 'white');
  $pass->pack( -side => 'right', -pady => 3);

  my $frm4 = $login->Frame(
    -relief => "groove",
    -borderwidth => 3);
  $frm4->pack(-side => 'top', -fill => 'x');
  my $submit = $frm4->Button( -text => 'Login', -command => sub
{ login_button;} );
  $submit->pack(-side => 'left');
  my $exit = $frm4->Button( -text => 'Cancel', -command => sub {exit
0;} );
  $exit->pack(-side => 'right');


  MainLoop();
}
------------------------
That code is simply to build a login window that when ran will prompt
the user to log into our application.  I've got all the login stuff
done.  It simply does a post on login.html with the org,user, and pass
values.

What I'm trying to do is center that window.

Before I get any further is Perl Tk really usable or clunky.  I like the
fact that I can use perl to scrap our pages and do all the other nice
things that perl can do.

On the perl side I'm using the following regex to grok the fields from
each <tr> that has data.
--- Cut Here ---
  foreach my $line (@lines) {
    next unless $line =~ m/alarm_detail\.html\?id=(\d+).+?[center;|
critical]">(.+?)<\/td>.+?center;">(.+?)<\/td>.+?center;">(.+?)<\/td>.+?
center;">(.+?)<\/td>.+?center;">(.+?)<\/td>.+?center;">(.+?)<\/td>.+?
center;">(.+?)<\/td>/;
    print INDEX "$line\n\n\n";
    $ALARMS{$1}{'alarm_id'}     = $1;
    $ALARMS{$1}{'level'}        = $2;
    $ALARMS{$1}{'raised'}       = $3;
    $ALARMS{$1}{'description'}  = $4;
    $ALARMS{$1}{'location'}     = $5;
    $ALARMS{$1}{'device'}       = $6;
    $ALARMS{$1}{'alarm_name'}   = $7;
  }
------------------------------------

Is there a way to simplify that regex?  And on the last '>(.+?)<\/td>'
It is possible that that could be '></td>' or '>User</td>'  If a user is
not assigned to that alarm then there is nothing there.  How can I fix
that piece to match on '></td>' or '>Bob</td>'.

Thanks,
Chris




More information about the charlotte mailing list