Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > SERVER SIDE CODING > PERL TUTORIALS > INCLUDE GUIS IN YOUR SERVER PROGRAMMING WITH PERL/TK


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

Include GUIs in your server programming with Perl/Tk
By Cameron Laird - 2004-10-21 Page:  1 2

Modern GUI toolkits make for easy system-level GUIs

As a system programmer, you habitually work with command-line interfaces. Perhaps you've lost track of how easy it can be to wrap existing functionality with a lightweight graphical user interface (GUI). New Perl/Tk releases make it timely to remember that sometimes high quality accompanies ease of use.

You can keep your focus on highly productive server-side programming, and still choose to jazz up your interfaces occasionally. What's more, lightweight toolkits such as Perl/Tk make it possible to do this without the costs of higher-profile GUI approaches.

Toolkit landscape

What are the costs? GUIs are generally harder to program, test, port, and deploy than their corresponding command-line-oriented relatives. Microsoft Visual Basic (VB) is widely recognized for its simplification of GUI construction. However, effective isolation to the Windows world and its clumsiness as a programming language constrain VB; see Resources later in this article for details on these disadvantages. Although Java, GTK+, and Qt have become accepted for Linux and related open source development, it takes a lot of study and lines of code to begin to use them; their "barriers to entry" discourage low-key transitions from character-oriented to WIMP (windows-icons-menus-pointers) programming.

As simplistic as the Web's model of user interaction is, it suffices for plenty of little jobs. In fact, most of my own GUI programming is of Web applications.

Sometimes, though, the Web's not enough. You need more sophisticated programmability or a different security model. Sometimes you just need a "second source"; a Web-based control panel for your Web server makes your system too fragile. Sometimes the Web's too much: the usual servers and clients have big memory footprints and demand configuration.

There's good news, though. Keep your highly-productive system programming habits. Stir in such lightweight high-level language bindings as Perl/Tk or Tkinter. You'll end up with portable, maintainable, surprisingly well-performing, "GUIfied" versions of the programs you've already developed.

Whether your favorite language is Ruby or Fortran, the right accessories will give you similar opportunities. Here's an example of how it can work for a programmer already comfortable with Perl.

Warming up to Perl/Tk

You know Perl. Its clever succinctness lets you code in five minutes the report generators, network probes, text filters, and process monitors that demand day-long efforts with other languages.

What you might not know, though, is that with just a few lines more, you can turn your Perl applications into well-behaved, good-looking GUI ones. Teodor Zlatanov's article on developerWorks describing cfengine and related Perl utilities (see Resources for a link to the article) shows how to write handy tools such as this tiny script, which shuts down all inetd processes:

Listing 1. Source code for kill_inetd.pl

           
    use Proc::ProcessTable;

    $t = new Proc::ProcessTable;  

    foreach $p (@{$t->table}) {
            # note that we will also kill "xinetd" and all processes
            # whose command line contains "inetd"
        kill 9, $p->pid if $p->cmndline =~ 'inetd';
    }
    

What if you need to shut down several rogue CGI processes? Or what if you want more graceful signaling than the rather brutal "kill 9"? What if your boss or your backup needs to use this script while you're away? The traditional answer is just to fire up an editor, substitute cgi for inetd, and so on, and re-run perl kill_inetd.pl from the command line.

Transform that first listing, though, to Listing 2:

Listing 2. Source code for modified kill_inetd.pl

    use Proc::ProcessTable;
    use Tk;
       
       
    sub kill_them {
        $t = new Proc::ProcessTable;  
        foreach $p (@{$t->table}) {
            kill $signal, $p->pid if $p->cmndline =~ $keyword;
        }
        $result_text = "Signal $signal sent to all '$keyword' processes.";
    }
    
    
    $main = MainWindow->new();
    $main->Label(-text =>
          "Control panel for killing processes\n   enter keyword here")->pack();
    $main->Entry(-textvariable => \$keyword)->pack();
    $radiobutton_frame = $main->Frame->pack();
    $radiobutton_frame->Radiobutton(-text => "KILL",
                       -variable => \$signal,
                       -value => 9)->pack(-side => left);
    $radiobutton_frame->Radiobutton(-text => "TERM",
                       -variable => \$signal,
                       -value => 15)->pack(-side => left);
    $main->Button(-text => "Send kill signal to named processes",
                  -command => \&kill_them)->pack();
    $main->Label(-textvariable => \$result_text)->pack();
    
        # Set reasonable defaults.
    $keyword = "inetd";
    $signal = 9;
    
    MainLoop();
    

Simple signal-sending control panel
Simple signal-sending control panel

Notice, though, what you do not have with this twenty-line script: you don't have pages of declarations and configuration. You don't have any license restriction beyond the familiar Perl ones. You don't need to learn a new language.



View Include GUIs in your server programming with Perl/Tk Discussion

Page:  1 2 Next Page: New horizons

First published by IBM developerWorks


Copyright 2004-2017 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.