by Brent B. Welch About the author: 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/ Content: |
Abstract:
This article explains basics concept and syntax of the Tcl language.
For a scripting language, Tcl has a simple syntax.
cmd arg arg arg
$foo
foo
.
[clock seconds]
"some stuff"
{some stuff}
\
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]]"
The Tcl syntax is used to guide the Tcl parser through three steps: argument grouping, result substitution, and command dispatch.
puts
command.
$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.
Here is another example:
Here, curly braces are used to group arguments without doing any substitutions. The Tcl parser knows nothing special about theset i 0 while {$i < 10} { puts "$i squared = [expr $i*$i]" incr i }
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.
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.
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]]] } }
Webpages maintained by the LinuxFocus Editor team
© Brent B. Welch LinuxFocus 1999 |
1999-08-02, generated by lfparser version 0.7