Skip to Main Content
Learning Perl/Tk
book

Learning Perl/Tk

by Nancy Walsh
January 1999
Beginner content levelBeginner
373 pages
9h 43m
English
O'Reilly Media, Inc.
Content preview from Learning Perl/Tk

1.10. Hello World Example

Every programming language goes through the Hello World example. It is a good example because it shows how to do something very simple but useful. In our Hello World example, we'll have the title of our window say "Hello World" and create a button that will dismiss the application:

#!/usr/bin/perl
use Tk;
my $mw = MainWindow->new;
$mw->title("Hello World");
$mw->Button(-text => "Done", -command => sub { exit })->pack;
MainLoop;

Despite only being six lines long, there is quite a bit going on in our little program. The first line, as any Perl programmer knows, invokes Perl (only on Unix; in Win32 you have to type perl hello.pl to invoke the program). The second line tells Perl that we would like to use the Tk module.

The third line

my $mw = MainWindow->new;

is how we create a window. The window will have the same basic window manager decorations as all your other windows. In a Unix environment, it will look like all your other windows, and if it were in MS Windows, it would look like those windows.

The title of our window is changed by using the title method. If we hadn't used this method, the text across the top of the window would be the same as the name of the file containing the code. For instance, if my code were stored in a file named hello_world, the string "Hello_world" would appear across the title bar of my application (Tk automatically capitalizes the first character for you). Using the title method is not required, but it makes the application ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Perl/Tk

Mastering Perl/Tk

Stephen Lidie, Nancy Walsh

Publisher Resources

ISBN: 1565923146Supplemental ContentCatalog PageErrata