Hello World Example
Every programming language goes through the Hello World example, which is a complete program that prints a string (typically “Hello World”) and exits. “Hello World” may get its share of ridicule, but it’s a remarkably effective tool that shows readers how to write and execute a working program while they’re still in the first chapter of the book. 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 being only six lines long, there is quite a bit going on in our little program. The first line, as any Perl programmer knows, invokes Perl.[7] The second line tells Perl 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.
The title of
our window is changed 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, excluding any extension. For instance,
if the code were stored in a file named
hello_world, the string
“Hello_world” would appear across the title bar of the
application (Tk automatically capitalizes the first character for
you). Using the title method is not required, but it makes the application look ...
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.
Read now
Unlock full access