TCL TK
TCL TK
Tcl is quite simple to learn and let's start creating our first Tcl program!
Let us write a simple Tcl program. All Tcl files will have an extension, i.e., .tcl. So, put the
following source code in a test.tcl file.
#!/usr/bin/tclsh
Assuming, Tcl environment is setup correctly; let's run the program after switching to file's
directory and then execute the program using −
$ tclsh test.tcl
Hello, World!
Let us now see the basic structure of Tcl program, so that it will be easy for you to understand
basic building blocks of the Tcl language. In Tcl, we use new line or semicolon to terminate
the previous line of code. But semicolon is not necessary, if you are using newline for each
command.
Comments
Comments are like helping text in your Tcl program and the interpreter ignores them.
Comments can be written using a hash_(#) sign in the beginning.
#!/usr/bin/tclsh
Hello World!
Multiline or block comment is written using 'if' with condition '0'. An example is shown below.
#!/usr/bin/tclsh
if 0 {
Hello World!
#!/usr/bin/tclsh
puts "Hello World!" ;# my first print in Tcl program
Hello World!
Identifiers
A Tcl identifier is a name used to identify a variable, function, or any other user-defined item.
An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more
letters, underscores, dollars ($) , and digits (0 to 9).
Tcl does not allow punctuation characters such as @, and % within identifiers. Tcl is a case
sensitive_ language. Thus Manpower and manpower are two different identifiers in Tcl. Here
are some of the examples of acceptable identifiers −
Whitespace in Tcl
A line containing only whitespace, possibly with a comment, is known as a blank line, and a
Tcl interpreter totally ignores it.
Whitespace is the term used in Tcl to describe blanks, tabs, newline characters, and
comments. Whitespace separates one part of a statement from another and enables the
interpreter to identify where one element in a statement, such as puts, ends and the next
element begins. Therefore, in the following statement −
#!/usr/bin/tclsh
There must be at least one whitespace character (usually a space) between “puts” and "Hello
World!" for the interpreter to be able to distinguish them. On the other hand, in the following
statement −
#!/usr/bin/tclsh
Tcl - Commands
As you know, Tcl is a Tool command language, commands are the most vital part of the
language. Tcl commands are built in-to the language with each having its own predefined
function. These commands form the reserved words of the language and cannot be used for
other variable naming. The advantage with these Tcl commands is that, you can define your
own implementation for any of these commands to replace the original built-in functionality.
Each of the Tcl commands validates the input and it reduces the work of the interpreter.
Tcl command is actually a list of words, with the first word representing the command to be
executed. The next words represent the arguments. In order to group the words into a single
argument, we enclose multiple words with "" or {}.
#!/usr/bin/tclsh
Hello, world!
In the above code, ‘puts’ is the Tcl command and "Hello World" is the argument1. As said
before, we have used "" to group two words.
#!/usr/bin/tclsh
Hello, world!
In the above code, ‘puts’ is the Tcl command, ‘stdout’ is argument1, and "Hello World" is
argument2. Here, stdout makes the program to print in the standard output device.
Command Substitution
In command substitutions, square brackets are used to evaluate the scripts inside the square
brackets. A simple example to add two numbers is shown below −
#!/usr/bin/tclsh
puts [expr 1 + 6 + 9]
16
Variable Substitution
In variable substitutions, $ is used before the variable name and this returns the contents of
the variable. A simple example to set a value to a variable and print it is shown below.
#!/usr/bin/tclsh
set a 3
puts $a
Backslash Substitution
These are commonly called escape sequences; with each backslash, followed by a letter
having its own meaning. A simple example for newline substitution is shown below −
#!/usr/bin/tclsh
puts "Hello\nWorld"
Hello
World
Tcl - Variables
In Tcl, there is no concept of variable declaration. Once, a new variable name is encountered,
Tcl will define a new variable.
Variable Naming
The name of variables can contain any characters and length. You can even have white
spaces by enclosing the variable in curly braces, but it is not preferred.
The set command is used for assigning value to a variable. The syntax for set command is,
#!/usr/bin/tclsh
set variableA 10
puts $variableA
puts ${variable B}
10
test
As you can see in the above program, the $variableName is used to get the value of the
variable.
Dynamic Typing
Tcl is a dynamically typed language. The value of the variable can be dynamically converted
to the required type when required. For example, a number 5 that is stored as string will be
converted to number when doing an arithmetic operation. It is shown below −
#!/usr/bin/tclsh
puts $variableA
puts $sum
30
Mathematical Expressions
As you can see in the above example, expr is used for representing mathematical expression.
The default precision of Tcl is 12 digits. In order to get floating point results, we should add at
least a single decimal digit. A simple example explains the above.
#!/usr/bin/tclsh
puts $result
puts $result
puts $result
1.1111111111111112
1.1111111111111112
In the above example, you can see three cases. In the first case, the dividend and the divisor
are whole numbers and we get a whole number as result. In the second case, the divisor
alone is a decimal number and in the third case, the dividend is a decimal number. In both
second and third cases, we get a decimal number as result.
In the above code, you can change the precision by using tcl_precision special variable. It is
shown below −
#!/usr/bin/tclsh
set tcl_precision 5
puts $result
1.1111
Tcl - Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. Tcl language is rich in built-in operators and provides the following types of
operators −
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Ternary Operator
This chapter will explain the arithmetic, relational, logical, bitwise, and ternary operators one
by one.
Arithmetic Operators
Following table shows all the arithmetic operators supported by Tcl language. Assume
variable ‘A’ holds 10 and variable ‘B’ holds 20, then −
Relational Operators
Following table shows all the relational operators supported by Tcl language. Assume
variable A holds 10 and variable B holds 20, then −
Logical Operators
Following table shows all the logical operators supported by Tcl language. Assume
variable A holds 1 and variable B holds 0, then −
Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and
^ are as follows −
A = 0011 1100
B = 0000 1101
----------------------
The Bitwise operators supported by Tcl language are listed in the following table. Assume
variable A holds 60 and variable B holds 13, then −
Operator precedence determines the grouping of terms in an expression. This affects how an
expression is evaluated. Certain operators have higher precedence than others; for example,
the multiplication operator has higher precedence than the addition operator.
For example : x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher
precedence than +, so it first gets multiplied with 3 * 2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the
lowest appear at the bottom. Within an expression, higher precedence operators will be
evaluated first.
Tcl - Decisions
Decision making structures require that the programmer specifies one or more conditions to
be evaluated or tested by the program, along with a statement or statements to be executed
if the condition is determined to be true, and optionally, other statements to be executed if
the condition is determined to be false.
Following is the general form of a typical decision making structure found in most of the
programming languages −
Tcl language uses the expr command internally and hence it’s not required for us to use expr
statement explicitly.
Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon.
The value of a '? expression' is determined like this: Exp1 is evaluated. If it is true, then Exp2
is evaluated and becomes the value of the entire '? expression.' If Exp1 is false, then Exp3 is
evaluated and its value becomes the value of the expression. An example is shown below.
#!/usr/bin/tclsh
set a 10;
When you compile and execute the above program, it produces the following result −
Value of b is 30
Value of b is 20
Tcl - Loops
There may be a situation, where you need to execute a block of code several number of
times. In general, statements are executed sequentially: The first statement in a function is
executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated
execution paths.
A loop statement allows us to execute a statement or group of statements multiple times and
following is the general form of a loop statement in most of the programming languages −
Tcl language provides the following types of loops to handle looping requirements.
Loop control statements change execution from its normal sequence. When execution leaves
a scope, all automatic objects that were created in that scope are destroyed.
A loop becomes infinite loop if a condition never becomes false. The while loop is
traditionally used for this purpose. You can make an endless loop by leaving the conditional
expression as 1.
while {1} {
When the conditional expression is absent, it is assumed to be true. Tcl programmers more
commonly use the while {1} construct to signify an infinite loop.
Tcl - Arrays
An array is a systematic arrangement of a group of elements using indices. The syntax for
the conventional array is shown below.
#!/usr/bin/tclsh
puts $languages(0)
puts $languages(1)
Tcl
C Language
Size of Array
#!/usr/bin/tclsh
Array Iteration
Though, array indices can be non-continuous like values specified for index 1 then index 10
and so on. But, in case they are continuous, we can use array iteration to access elements of
the array. A simple array iteration for printing elements of the array is shown below.
#!/usr/bin/tclsh
for { set index 0 } { $index < [array size languages] } { incr index } {
languages(0) : Tcl
languages(1) : C Language
Associative Arrays
In Tcl, all arrays by nature are associative. Arrays are stored and retrieved without any
specific order. Associative arrays have an index that is not necessarily a number, and can be
sparsely populated. A simple example for associative array with non-number indices is shown
below.
#!/usr/bin/tclsh
set personA(Age) 14
puts $personA(Name)
puts $personA(Age)
Dave
14
Indices of Array
#!/usr/bin/tclsh
set personA(Age) 14
puts [array names personA]
Age Name
You can use the indices of array to iterate through the associative array. An example is shown
below.
#!/usr/bin/tclsh
set personA(Age) 14
personA(Age): 14
personA(Name): Dave