Programming with Gtk2-Perl/Getting Started/Hello World

Breaking it down
Because if someone hasn't told you to include these at the top of every Perl program you write, I'm telling you now.

The Gtk+ libraries are included under the Gtk2 namespace.

This method needs to be called from every Gtk2 application. It initializes the library for use, setting up things things like the color map and connecting the default signal handlers. This method also checks the arguments that have been passed to your application on the command line. The following arguments are accepted by all Gtk applications. They will be removed from the argument list, leaving the rest for your application to handle.

* --gtk-module * --g-fatal-warnings * --gtk-debug * --gtk-no-debug * --gdk-debug * --gdk-no-debug * --display * --sync * --name * --class

Instead of explicitly calling the init method yourself, you could call it implicitly like this.

These two lines create new widgets. A widget is an element of the GUI. Windows, labels, buttons, menus, images are all examples of widgets. In this example we can see that the constructor to the window widget takes an argument telling it to make a top-level window, and the label widget can take some text to display. Different widgets accept different parameters to the constructor. Check the Gtk documentation for more information on specific widgets.

This line packs the label into the window. A window widget is an example of a container. A container widget is used to control how its children will be displayed on the screen. Container widgets have a variety of methods that can be used to add children. Here we are using a very basic example to display a label inside a window.

Can you guess what these lines do?

We could have said this instead.

Every Gtk application has must have a call to the main method. This turns control over the Gtk main loop which waits for events to occur and then takes the proper action.

We didn't forget to cover this line. Here we attach a callback to the 'delete-event' of the window. A callback is a reference to a function. In this example we use an anonymous subroutine, which interrupts Gtk's main loop and returns control back over to the program. Any code after the call to Gtk2->main will be executed, in our example the program just closes. Had we not connected this callback to the 'delete-event', the window would have closed but our program would still have been running in the main loop, and the user would have no easy way to exit the application. (You could kill it with Ctrl+c.)