Gtk+ (Gimp toolkit) has been a fairly common library for building GUI elements in Linux, used by the Gimp and Gnome projects for their widget library. Gtk+ has now evolved to Gtk+2, which brings a more sophisticated look to the widget set. Using the Gtk2-Perl package, you can utilize this widget set to create applications in Perl that will run in X with a contemporary GUI look and feel.
A related Gnome2-Perl module allows you to use even more GUI features in your applications. If you want to use Perl in Microsoft Windows, you'll need to add quite a few bits to Windows to get Gtk2-Perl up and running. Windows support is somewhat experimental, but if you are interested, you can check out the details here. Essentially, you will need to install all of the underlying support libraries that Gtk+2 depends on.
Under Linux, depending on how new your distribution is, you may already have Gtk2-Perl packaged, and you can just install it using your system package manager. Otherwise, daily CVS snapshots are available for download. You can also grab it from CPAN.
Building from source
Listing A shows you how to build it from the make command. If you see any errors from perl Makefile.PL or make commands, chances are your system is lacking some of the underlying requirements to build Gtk2-Perl. The resulting error messages should point you in the right direction. There may be some failures in make test, as Gtk2-Perl is still under heavy development, but the majority of the tests should pass.
A typical make test output, at the time of this writing, would look like this sidebar. This is not necessarily an indication that your build is bad, but more a reflection of the relatively new nature of Gtk2-Perl.
The sudo command enables you to complete the install as root system-wide. If you don’t have permissions to execute a sudo command, you can do the install as a user in your home directory.
On the particular distribution version I'm running on this laptop, I had to update MakeMaker and install Parse-RecDescent, Filter, Inline, and Inline-Files, found at CPAN. I also had to install the underlying libraries for Gtk+2, glib2, atk1.2, and pango to provide the underlying library support for Gtk+2. If your distribution is already using Gnome2, there's a good chance you will have much of this in place, if not a prepackaged Gtk2-Perl that is ready to go. On another machine running the current beta of Mandrake Linux, I didn't need to install anything extra.
Once you've built and installed Gtk2-Perl, you can run a number of examples in the tutorial-examples directory from the command line:
As you can see by looking at the example files, it's possible to do some fairly nice GUI design with a minimum of Perl coding. If the examples run without error, your Gtk2-Perl install should be in good shape.
Get more tutorials
A tutorial for writing applications for Gtk2 in C is available from gtk.org. Much of the information applies to Perl, as well.
Coding for Gtk2-Perl is similar to that for Gtk-Perl; the key difference is in the module used. For Gtk2, you call Use Gtk2. Some of the other syntax is slightly different from the Gtk-Perl module, but it's similar enough that you won't be lost if you've already done some work with Gtk-Perl.
Listing B shows a basic Gtk2-Perl application. The result should look similar to Figure A.
|Hello World application|
In practice, the subroutine called by the button click event would probably be in its own subroutine so it could be reused by a menu item or some other widget. I put it inline just to make the example as few lines of code as possible. The code calls up the Gtk2.pm module, creates a top-level window with a button, and displays some text on the button. Clicking the button exits the application. The $window->show_all() displays the GUI, and the last two lines will be used in all Gtk2-Perl applications to initiate the event handler for the GUI elements.
For a slightly more useful example, let's create an application that connects to a simple Perl-based server application. The application dumps the output of w when a connection is made to the port it is monitoring. It's something I did some time ago on Perl socket programming. You could easily modify the example to report on other status conditions across a network of machines. The code in Listing C is the server-side application.
Make the server app executable with the following command:
chmod +x server1.pl
The application defaults to listening for connections on port 7890. You can run it by entering:
For the client application (Listing D), you'll need several GUI elements, such as an entry field to enter the hostname, as well as a text widget to display the output or error message. You’ll also need two buttons: one to initiate the poll and another to exit the program.
Once you've coded your app, you can make it executable as you did with the server using chmod and then execute it:
If you have the server app running on the same machine, you could enter "localhost" as the "Host to poll," or if the server is running on another machine, use that machine's hostname. If the connection is successful, you should see the output of w in the text widget. In the event of an error, you'll see the error message. You trap the case of an empty hostname and pass this to the error routine. The socket code traps several other error scenarios. The successful result should look like Figure B.
|Client-side GUI screenshot|
That's about it. In just a few lines of code, you can put together a useful GUI interface around what was a command line program. With a little elbow grease, you can have a fairly complex Perl-based GUI.