The Tcl Syntax

ArticleCategory: [Artikel Kategorie]

Software Development

AuthorImage:[Bild des Autors]

[Photo of the Author]

AuthorName[Name des Autors]

Brent B. Welch

AboutTheAuthor:[Über den Autor]

Brent Welch is having fun building the www.scriptics.com website, including an annotated database of Tcl-related URLs (the Tcl Resource Center) and the ecommerce infrastructure for the TclPro product. He has also built several large Tcl/Tk applications, including the TclHttpd web server, which runs www.scriptics.com, the Exmh mail user interface and the webtk HTML editor. He is the author of "Practical Programming in Tcl and Tk." Welch received a BS in Aerospace Engineering at the University of Colorado, Boulder, in 1982 and an MS in Computer Science at the University of California, Berkeley, in 1986 and a PhD in Computer Science at the University of California, Berkeley, in 1990. Previously Welch was a member of research staff at Xerox PARC working on distributed systems, and later a member of the Tcl/Tk team at Sun Microsystems Laboratories. He is a member of the ACM and the IEEE Computer Society. Home Page: http://www.beedub.com/

Abstract:[Zusammenfassung]

This article explains basics concept and syntax of the Tcl language.

ArticleIllustration:[Titelbild des Artikels]

[Ilustration]

ArticleBody:[Der eigentliche Artikel]

An Introduction to Tcl Syntax

For a scripting language, Tcl has a simple syntax.

cmd arg arg arg
A Tcl command is formed by words separated by white space. The first word is the name of the command, and the remaining words are arguments to the command.
$foo
The dollar sign ($) substitutes the value of a variable. In this example, the variable name is foo.
[clock seconds]
Square brackets execute a nested command. For example, if you want to pass the result of one command as the argument to another, you use this syntax. In this example, the nested command is clock seconds, which gives the current time in seconds.
"some stuff"
Double quotation marks group words as a single argument to a command. Dollar signs and square brackets are interpreted inside double quotation marks.
{some stuff}
Curly braces also group words into a single argument. In this case, however, elements within the braces are not interpreted.
\
The backslash (\) is used to quote special characters. For example, \n generates a newline. The backslash also is used to "turn off" the special meanings of the dollar sign, quotation marks, square brackets, and curly braces.

A Little Example

Below is a Tcl command that prints the current time. It uses three Tcl commands: set, clock, and puts. The set command assigns the variable. The clock command manipulates time values. The puts command prints the values.

set seconds [clock seconds] puts "The time is [clock format $seconds]"

Note that you do not use $ when assigning to a variable. Only when you want the value do you use $. The seconds variable isn't needed in the previous example. You could print the current time with one command:

puts "The time is [clock format [clock seconds]]"

Grouping and Substitution

The Tcl syntax is used to guide the Tcl parser through three steps: argument grouping, result substitution, and command dispatch.

  1. Argument grouping. Tcl needs to determine how to organize the arguments to the commands. In the simplest case, white space separates arguments. As stated earlier, the quotation marks and braces syntax is used to group multiple words into one argument. In the previous example, double quotation marks are used to group a single argument to the puts command.

  2. Result substitution. After the arguments are grouped, Tcl performs string substitutions. Put simply, it replaces $foo with the value of the variable foo, and it replaces bracketed commands with their result. That substitutions are done after grouping is crucial. This sequence ensures that unusual values do not complicate the structure of commands.

  3. Command dispatch. After substitution, Tcl uses the command name as a key into a dispatch table. It calls the C procedure identified in the table, and the C procedure implements the command. You also can write command procedures in Tcl. There are simple conventions about argument passing and handling errors.

Another Example

Here is another example:

set i 0 while {$i < 10} { puts "$i squared = [expr $i*$i]" incr i }

Here, curly braces are used to group arguments without doing any substitutions. The Tcl parser knows nothing special about the while command. It treats it like any other command. It is the implementation of the while command knows that the first argument is an expression, and the second argument is more Tcl commands. The braces group two arguments: the boolean expression that controls the loop and the commands in the loop body.

We also see two math expressions: the boolean comparison and multiplication. The while command automatically evaluates its first argument as an expression. In other cases you must explicitly use the expr command to perform math evaluation.

Command Dispatch

Lastly, Tcl calls something else to do the hard work. We've seen that Tcl uses expr to perform math functions, puts to handle output functions, and set to assign variables. These Tcl commands are implemented by a C procedure that has registered itself with Tcl. The C command procedures take the string arguments from the Tcl command and return a new string as their result. It is very easy to write C command procedures. They can do everything from accessing databases to creating graphical user interfaces. Tcl, the language, doesn't really know what the commands do. It just groups arguments, substitutes results, and dispatches commands.

One Last Example

Here is the factorial procedure:

proc fac {x} {
    if {$x < 0} {
        error "Invalid argument $x: must be a positive integer"
    } elseif {$x <= 1} {
        return 1
    } else {
        return [expr $x * [fac [expr $x-1]]]
    }
}


More Reading

Source for this article: Scriptics

mirror server hosted at Truenetwork, Russian Federation.