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

Get Learning Perl/Tk now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.