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.7. Coding Style

The code lines in a Perl/Tk script can get quite cumbersome and clunky because of all the option/value pairs used in defining and configuring each widget. There are several ways to format the code to deal with readability (and in some cases, "edit-ability"). Most just involve adding extra spaces or tabs to line up different portions of code. Once you get used to seeing the code, it won't appear to be quite so mysterious and unwieldy.

One coding style places each option/value pair on separate lines (this is my personal favorite, and I use it all the time):

$bttn = $parent->Button(-text => "my text",
                        -command => sub { exit }, 
                        -width => 10, 
                        -height => 10);

With this type of coding style, it is extremely obvious what the pairs are and what value is associated with which option. (You could also go to the extreme of aligning each=>to make nice columns, depending on how much time you have to press the space bar.) Some people like to start the option/value pairs on the next line and put the ending ); on its own separate line, after the last option/value pair, which retains the comma for formatting ease:

$bttn = $parent->Button(
   -text => "Exit", 
   -command => sub { exit }, 
   -width => 10, 
   -height => 10,
  );

This makes the code easier to edit; an option/value pair can be added or deleted on each line without having to mess with parentheses, semicolons or commas. It also keeps the next lines closer to the left side of the page so if you have several indentation levels, you don't ...

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