13-Basic TCL Language
13-Basic TCL Language
net/publication/262042277
CITATIONS READS
0 788
1 author:
Graham Phillips
University of Southern California
15 PUBLICATIONS 3,901 CITATIONS
SEE PROFILE
All content following this page was uploaded by Graham Phillips on 05 May 2014.
Graham Phillips
Computer Science Department
University of Southern California
Los Angeles
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 1
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
Purpose
Language overview
Authorities
The defining document for the language is “Tcl and the Tk Toolkit” by
John K. Ousterhout, Addison-Wesley, 1994, ISBN 0-201-63337-X.
The WWW page called “The Tcl/Tk Project At Sun Microsystems
Laboratories” may be found at
http://www.smli.com/research/tcl/
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 2
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
Syntax
Each command consists of one or more words where the words are
separated by white-space:
<command> <arg1> <arg2> ... <argn>.
A Tcl script consists of one or more commands. Commands are
separated by newlines or semi-colons.
% set a 2
% set a 24; set b 15
Variable substitution
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 3
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
Syntax cont.
Command substitution
Backslash substitution
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 4
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
Syntax cont.
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 5
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
Syntax cont.
Comments
A comment line is started by the # character and extends to the end of the
line. The # character must be in the position of the first word of a
command word.
% set b 101 ;# is legal but
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 6
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
Variables
Associative arrays
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 7
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
Variables
Associative arrays
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 7
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
Variables cont.
Predefined variables
The following variables may be predefined. Tcl does not require that all
applications support the following variables, but most applications do.
$argv0 is the name of the script file,
$argv is the list of command line arguments,
$argc is the number of command line arguments and
$env is an array variable whose elements are all of the processes’s
environment variables.
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 8
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
Precedence of Operators
• The operators are similar to the ANSI C operators and have the same
precedence.
• The % (mod) operator has a more consistent behavior in Tcl than C. In
Tcl the remainder is always greater than or equal to zero.
• Short-circuit evaluation is used.
• The operators <,>, <=, >=. ==, != may be used with strings as well as
real numbers and integers.
Literals
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 9
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
Expression evaluation
Order of evaluation
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 10
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
Expressions cont.
Conversions
When two operands are combined using a binary operator, Tcl uses
arithmetic evaluation unless one or both of the operands cannot be parsed
as a number. The result is converted back to a string.
% set x 0
% set x 00
% if {$x == $y} {set z 1}
1
String comparison can be forced.
% string compare $x $y
-1
The global variable tcl_precision controls the precision of the
conversion. If tcl_precision = 17 then no information is lost
during the conversion.
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 11
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
Lists
Creating lists
% concat {a b c} {d e} f {g h i}
a b c d e f g h i
% list {a b c} {d e} f {g h i}
{a b c} {d e} f {g h i}
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 12
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
Lists cont.
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 13
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
Lists cont.
% set y /usr/include/sys/types.h
% split $y /
{} usr include sys types.h
% join { {} usr include sys types.h} /
/usr/include/sys/types.h
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 14
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
File I/O
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 15
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
Tcl’s control flow commands are similar to the control flow statements in
C and C-shell.
IF statement
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 16
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
The while loop takes two arguments: an expression and a script. The
while statement returns an empty string as the result. Note that the
brace { must be on the same line as the preceding word or else the
newline is treated as the command separator.
% set i 1
% while {$i <= 4} {
puts “$i squared is [expr $i*$i]” }
incr i 1
}
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 17
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
The first argument of the for loop is an initialization script, the second
is a termination script, the third is a condition and the fourth is the body
of the loop. The first expression is executed as a Tcl script. If the
condition evaluates to non-zero, for executes the body followed by the
reinitialization script.
% for {set i 1} {$i <= 4} {incr i 1} {
puts “$i squared is [expr $i*$i]” }
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
The foreach statement executes a Tcl script for each element in a list.
% foreach i {1 2 3 4} {
puts “$i squared is [expr $i*$i]” }
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 18
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 19
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 20
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
Procedures
Tcl procedures are implemented using the proc keyword. The first
argument to proc is the procedure name, the second is a list of
arguments and the third is the procedure body.
• Procedures are not declared. Once the proc command completes, a
new command exists which may be invoked like any other Tcl
command.
• Within the procedure, the procedure arguments are local variables
while all other variables are global variables.
• Global variables must be referenced using the global command
which sets variable bindings (e.g. global x y). Once invoked, the
bindings remain effective until the procedure ends.
• Procedure arguments may be passed by value or by reference.
• Arguments may have default values.
• A variable number of arguments is permitted.
• Tcl does not provide a form of variable equivalent to static variable in
C and therefore global variables must be used for purposes like this.
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 21
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
Procedures cont.
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 22
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
Exception handling
Concurrency
String manipulation
Tcl supports two mechanisms for matching strings. The first is called
“globbing” that is based on the UNIX mechanism for file name
expansion. The second uses regular expressions.
Processes
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 23
U N I V E R S I T Y O F S O U T H E R N C A L I F O R N I A
Comments
GRAHAM PHILLIPS
COMPUTER SCIENCE DEPARTMENT
USC 24