Think Commands, Not Statements

Tcl is a syntactically simple language. The first word is the name of the command to be executed, and the remaining words are arguments to that command. Words of a command are sequences of characters separated by whitespace, but quoting can cause whitespace to be included in a word. Each of the lines in the following example are complete words:

abc
941.32
Long's Peak
$result
"the quick brown fox jumped over the lazy dog"
"checking if $somevar exists: [info exists $somevar]"
[llength $list]
{set area [expr {$pi * pow($r,2)}]; puts "area = $area"}

Problems occur when programmers do not pay enough attention to the differences between Tcl and other languages with which they may be familiar. It is sometimes a trap to try using idioms from other languages. Languages such as C have compilers that understand syntax and generate machine code that provides execution and instruction branching. Tcl has only commands and arguments; commands enable program flow control.

A good example of this difference is the if command. If is a command whose arguments are a conditional expression and blocks of Tcl code to be executed depending on the result of the expression. Tcl sees this if command as if it were a single list of three words:

{if} {$salary < 0} {puts "oops, salary is negative"}

It is quite common to write if commands as one would in C, breaking up the command over several lines, as in this example:

if {$salary < 0} {
    puts "oops, salary is negative"
}

Some programmers prefer a different for matting style, aligning the braces of the true condition block as follows:

if {$salary < 0}
    {
        puts "oops, salary is negative"
    }

When this code is run, Tcl reports an error with the if command, saying that no script follows the expression. The reason is that the if command was terminated by the newline character. The opening brace on the second line is treated as the start of a new command list.

The use of braces in Tcl to quote strings includes all characters up to the matching ending brace, including newline characters. The first if code fragment fully satisfies Tcl, since the opening brace to the true condition code block begins on the same line; the last one fails because the true condition code block begins on a new line, and newlines are used as command terminators in Tcl. The same operation applies to other commands typically written across multiple lines—for, foreach, while, switch, proc, and so forth. Don’t forget that if commands with else clauses also need to be coded on the same logical line, as in this example:

set salary 60000.0
if {$salary < 0} {
    puts "oops, salary is negative"
} else {
    set monthlySalary [expr $salary / 12]
    puts "Monthly salary: $monthlySalary"
}

This code produces the following output:

Monthly salary: 5000.0

Whitespace is also required around the words of a command list. The code fragment in the following example fails because whitespace is missing between the expression and the true condition code block:

if {$salary < 0}{
    puts "oops, salary is negative"
}

Some readers will note that the earlier example can be fixed by quoting the newline character of the first line with a backslash, causing the logical command line to be continued on the second line:

if {$salary < 0} 
    {
        puts "oops, salary is negative"
    }

Although this is perfectly acceptable in Tcl, it adds noise characters to the code without much benefit. The best solution is to adopt the conventional Tcl coding style. In other cases, however, breaking up a long command with escaped newline characters (i.e., end of line quoted with “”) is useful to maintain readability in your code. This is especially true if you use a text editor that wraps lines instead of scrolling horizontally:

puts "At the sound of the tone, the time will be [clock format 
                    [clock seconds]-format %H:%M]"

This code produces the following output:

At the sound of the tone, the time will be 12:43
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset