0% found this document useful (0 votes)
66 views54 pages

Unit-Iii TCL

Tcl is a scripting language used to build GUIs and for general programming. It aims to enable programs to communicate and interact with each other. Tcl was designed by John Ousterhout and combines a scripting language with its own interpreter. It can be embedded into applications and runs on many platforms including Windows, Mac, and Unix. Tcl uses commands like set to assign values to variables and dynamic typing where variables can take on different types. Control structures in Tcl include if-else and switch-case statements to make decisions in code.

Uploaded by

Abhiram Pula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views54 pages

Unit-Iii TCL

Tcl is a scripting language used to build GUIs and for general programming. It aims to enable programs to communicate and interact with each other. Tcl was designed by John Ousterhout and combines a scripting language with its own interpreter. It can be embedded into applications and runs on many platforms including Windows, Mac, and Unix. Tcl uses commands like set to assign values to variables and dynamic typing where variables can take on different types. Control structures in Tcl include if-else and switch-case statements to make decisions in code.

Uploaded by

Abhiram Pula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 54

UNIT-III

TCL
Tcl is a general purpose multi-paradigm system programming language. It is a scripting language
that aims at providing the ability for applications to communicate with each other. On the other
hand, Tk(Tool Kit) is a cross platform widget toolkit used for building GUI in many languages.

Tcl is shortened form of Tool Command Language. John Ousterhout of the University
of California, Berkeley, designed it. It is a combination of a scripting language and its own
interpreter that gets embedded to the application, we develop with it.

Tcl was developed initially for Unix. It was then ported to Windows, DOS, OS/2, and Mac
OSX. Tcl is much similar to other unix shell languages like Bourne Shell (Sh), the C Shell
(csh), the Korn Shell (sh), and Perl.

It aims at providing ability for programs to interact with other programs and also for acting
as an embeddable interpreter. Even though, the original aim was to enable programs to
interact, you can find full-fledged applications written in Tcl/Tk.

Features of Tcl
The features of Tcl are as follows −

 Reduced development time.

 Powerful and simple user interface kit with integration of TK.

 Write once, run anywhere. It runs on Windows, Mac OS X, and almost on every Unix platform.

 Quite easy to get started for experienced programmers; since, the language is so simple that they
can learn Tcl in a few hours or days.

 You can easily extend existing applications with Tcl. Also, it is possible to include Tcl in C, C++,
or Java to Tcl or vice versa.

 Have a powerful set of networking functions.

 Finally, it's an open source, free, and can be used for commercial applications without any limit.

Applications
Tcl is a general-purpose language and you can find Tcl everywhere. It includes,

 Scalable websites that are often backed by databases.

 High performance web servers build with TclHttpd.

 Tcl with CGI based websites.

 Desktop GUI applications.

 Embedded applications.
TCL/Tk Structure and Syntax
Tcl is quite simple to learn and let's start creating our first Tcl program!

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.
Live Demo

#!/usr/bin/tclsh

puts "Hello, World!"

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

We will get the following output −


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.
Live Demo

#!/usr/bin/tclsh

# my first program in Tcl

puts "Hello World!"

When the above code is executed, it produces the following result −


Hello World!

Multiline or block comment is written using 'if' with condition '0'. An example is shown
below.
Live Demo
#!/usr/bin/tclsh

if 0 {

my first program in Tcl program

Its very simple

puts "Hello World!"

When the above code is executed, it produces the following result −


Hello World!

Inline comments use ;#. An example is given below.


Live Demo

#!/usr/bin/tclsh

puts "Hello World!" ;# my first print in Tcl program

When the above code is executed, it produces the following result −


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 Manpowerand manpower are two different identifiers in Tcl.
Here are some of the examples of acceptable identifiers −
mohd zara abc move_name a_123
myname50 _temp j a23b9 retVal

Reserved Words
The following list shows a few of the reserved words in Tcl. These reserved words may not
be used as constant or variable or any other identifier names.

after append array auto_execok


auto_import auto_load auto_load_index auto_qualify

binary Bgerror break catch

cd Clock close concat

continue Dde default else

elseif Encoding eof error

eval Exec exit expr

fblocked Fconfigure fcopy file

fileevent Flush for foreach

format Gets glob global

history If info interp

join Lappend lindex linsert

list Llength load lrange

lreplace Lsearch lsort namespace

open Package pid pkg_mkIndex

proc Puts pwd read

regexp Regsub rename resource

return Scan seek set


socket Source split string

subst Switch tclLog tell

time Trace unknown unset

update Uplevel upvar variable

vwait While

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

puts "Hello World!"

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 −
Live Demo

#!/usr/bin/tclsh

puts [expr 3 + 2] ;# print sum of the 3 and 2

When the above code is executed, it produces the following result −


5

No whitespace characters are necessary between 3 and +, or between + and 2; although, you
are free to include some if you wish for the readability purpose.
Variables and data in TCL
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,
set variableName value

A few examples of variables are shown below −


Live Demo

#!/usr/bin/tclsh

set variableA 10

set {variable B} test

puts $variableA

puts ${variable B}

When the above code is executed, it produces the following result −


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 −
Live Demo

#!/usr/bin/tclsh

set variableA "10"

puts $variableA
set sum [expr $variableA +20];

puts $sum

When the above code is executed, it produces the following result −


10
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.
Live Demo

#!/usr/bin/tclsh

set variableA "10"

set result [expr $variableA / 9];

puts $result

set result [expr $variableA / 9.0];

puts $result

set variableA "10.0"

set result [expr $variableA / 9];

puts $result

When the above code is executed, it produces the following result −


1
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 −
Live Demo

#!/usr/bin/tclsh
set variableA "10"

set tcl_precision 5

set result [expr $variableA / 9.0];

puts $result

When the above code is executed, it produces the following result −


1.1111

Control Flow
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.

Tcl language provides following types of decision making statements −


Sr.No. Statement & Description

1 if statement

An 'if' statement consists of a Boolean expression followed by one or more


statements.

2 if...else statement
An 'if' statement can be followed by an optional 'else' statement, which
executes when the Boolean expression is false.

3 nested if statements
You can use one 'if' or 'else if' statement inside another 'if' or 'else if'
statement(s).

4 switch statement
A switch statement allows a variable to be tested for equality against a list
of values.

5 nested switch statements


You can use one switch statement inside anotherswitch statement(s).

The ? : Operator
We have covered conditional operator ? : in previous chapter, which can be used to
replace if...else statements. It has the following general form −
Exp1 ? Exp2 : Exp3;

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.
Live Demo

#!/usr/bin/tclsh

set a 10;

set b [expr $a == 1 ? 20: 30]


puts "Value of b is $b\n"

set b [expr $a == 10 ? 20: 30]

puts "Value of b is $b\n"

When you compile and execute the above program, it produces the following result −
Value of b is 30
Value of b is 20

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.

Sr.No. Loop Type & Description

1 while loop

Repeats a statement or group of statements while a given condition is true.


It tests the condition before executing the loop body.
2 for loop
Executes a sequence of statements multiple times and abbreviates the code
that manages the loop variable.

3 nested loops
You can use one or more loop inside any another while, for or do..while
loop.

Loop Control Statements


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.

Tcl supports the following control statements.

Sr.No. Control Statement & Description

1 break statement

Terminates the loop or switch statement and transfers execution to the


statement immediately following the loop or switch.

2 continue statement
Causes the loop to skip the remainder of its body and immediately retest its
condition prior to reiterating.

The Infinite Loop


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} {
puts "This loop will run forever."
}

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.

NOTE − you can terminate an infinite loop by pressing Ctrl + C keys.

Data Structures
Tcl lists are first class objects -- you can use them in just about any context without having to create code
to explicitly convert them into some stringrepresentation or back again.

Tcl has a few other ways of dealing with complex data which are not first class:

 Arrays
 Namespaces
 Procedures
 Interpreters
 Dictionaries

Arrays and namespaces are conceptually somewhat similar -- in both cases they provide a syntax that lets
you start with some a name and get some specific data. Arrays are a bit simpler than namespaces, however.
More generally, some things are easier to do with arrays and other things are easier to do with namespaces.

Procedures are the ultimate in "being able to deal with something complex" in tcl. In principle, anything
you can do with a list, an array or a namespace you could also do with a procedure (but almost always, if a
list, an array or a namespace will work, it's a bad idea to use a proc -- or, really: use procs in conjunction
with numbers/strings/lists/arrays/namespaces).

Interpreters are vaugely analogous to namespaces in some ways and vaugely analogous to procs in other
ways. Mostly, I think these are used for isolation rather than data representation.

Arrays:

An array is a systematic arrangement of a group of elements using indices. The


syntax for the conventional array is shown below.
set ArrayName(Index) value

An example for creating simple array is shown below.


Live Demo

#!/usr/bin/tclsh

set languages(0) Tcl

set languages(1) "C Language"

puts $languages(0)

puts $languages(1)

When the above code is executed, it produces the following result −


Tcl
C Language

Size of Array
The syntax for calculating size array is shown below.
[array size variablename]
An example for printing the size is shown below.
Live Demo

#!/usr/bin/tclsh

set languages(0) Tcl

set languages(1) "C Language"

puts [array size languages]

When the above code is executed, it produces the following result −


2

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.
Live Demo

#!/usr/bin/tclsh

set languages(0) Tcl

set languages(1) "C Language"

for { set index 0 } { $index < [array size languages] } { incr index } {

puts "languages($index) : $languages($index)"

When the above code is executed, it produces the following result −


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.
Live Demo
#!/usr/bin/tclsh

set personA(Name) "Dave"

set personA(Age) 14

puts $personA(Name)

puts $personA(Age)

When the above code is executed, it produces the following result −


Dave
14

Indices of Array
The syntax for retrieving indices of array is shown below.

[array names variablename]

An example for printing the size is shown below.


Live Demo

#!/usr/bin/tclsh

set personA(Name) "Dave"

set personA(Age) 14

puts [array names personA]

When the above code is executed, it produces the following result −


Age Name

Iteration of Associative Array


You can use the indices of array to iterate through the associative array. An example is
shown below.
Live Demo

#!/usr/bin/tclsh

set personA(Name) "Dave"

set personA(Age) 14
foreach index [array names personA] {

puts "personA($index): $personA($index)"

When the above code is executed, it produces the following result −


personA(Age): 14
personA(Name): Dave

List:
List is one of the basic data-type available in Tcl. It is used for representing an ordered
collection of items. It can include different types of items in the same list. Further, a list can
contain another list.

An important thing that needs to be noted is that these lists are represented as strings
completely and processed to form individual items when required. So, avoid large lists and
in such cases; use array.

Creating a List
The general syntax for list is given below −
set listName { item1 item2 item3 .. itemn }
# or
set listName [list item1 item2 item3]
# or
set listName [split "items separated by a character" split_character]

Some examples are given below −


Live Demo

#!/usr/bin/tclsh

set colorList1 {red green blue}

set colorList2 [list red green blue]

set colorList3 [split "red_green_blue" _]

puts $colorList1

puts $colorList2

puts $colorList3

When the above code is executed, it produces the following result −


red green blue
red green blue
red green blue

Appending Item to a List


The syntax for appending item to a list is given below −
append listName split_character value
# or
lappend listName value

Some examples are given below −


Live Demo

#!/usr/bin/tclsh

set var orange

append var " " "blue"

lappend var "red"

lappend var "green"

puts $var

When the above code is executed, it produces the following result −


orange blue red green

Length of List
The syntax for length of list is given below −
llength listName

Example for length of list is given below −


Live Demo

#!/usr/bin/tclsh

set var {orange blue red green}

puts [llength $var]

When the above code is executed, it produces the following result −


4

List Item at Index


The syntax for selecting list item at specific index is given below −
lindex listname index

Example for list item at index is given below −


Live Demo

#!/usr/bin/tclsh

set var {orange blue red green}

puts [lindex $var 1]

When the above code is executed, it produces the following result −


blue

Insert Item at Index


The syntax for inserting list items at specific index is given below.
linsert listname index value1 value2..valuen

Example for inserting list item at specific index is given below.


Live Demo

#!/usr/bin/tclsh

set var {orange blue red green}

set var [linsert $var 3 black white]

puts $var

When the above code is executed, it produces the following result −


orange blue red black white green

Replace Items at Indices


The syntax for replacing list items at specific indices is given below −
lreplace listname firstindex lastindex value1 value2..valuen

Example for replacing list items at specific indices is given below.


Live Demo

#!/usr/bin/tclsh

set var {orange blue red green}


set var [lreplace $var 2 3 black white]

puts $var

When the above code is executed, it produces the following result −


orange blue black white

Set Item at Index


The syntax for setting list item at specific index is given below −
lset listname index value

Example for setting list item at specific index is given below −


Live Demo

#!/usr/bin/tclsh

set var {orange blue red green}

lset var 0 black

puts $var

When the above code is executed, it produces the following result −


black blue red green

Transform List to Variables


The syntax for copying values to variables is given below −
lassign listname variable1 variable2.. variablen

Example for transforming list into variables is given below −


Live Demo

#!/usr/bin/tclsh

set var {orange blue red green}

lassign $var colour1 colour2

puts $colour1

puts $colour2

When the above code is executed, it produces the following result −


orange
blue

Sorting a List
The syntax for sorting a list is given below −
lsort listname

An example for sorting a list is given below −


Live Demo

#!/usr/bin/tclsh

set var {orange blue red green}

set var [lsort $var]

puts $var

When the above code is executed, it produces the following result −


blue green orange red

Dictionary:
A dictionary is an arrangement for mapping values to keys. The syntax for the
conventional dictionary is shown below −
dict set dictname key value
# or
dict create dictname key1 value1 key2 value2 .. keyn valuen

Some examples for creating a dictionary are shown below −


Live Demo

#!/usr/bin/tclsh

dict set colours colour1 red

puts $colours

dict set colours colour2 green

puts $colours

set colours [dict create colour1 "black" colour2 "white"]

puts $colours
When the above code is executed, it produces the following result −
colour1 red
colour1 red colour2 green
colour1 black colour2 white

Size of Dict
The syntax for getting size of dict is shown below −
[dict size dictname]

An example for printing the size is shown below −


Live Demo

#!/usr/bin/tclsh

set colours [dict create colour1 "black" colour2 "white"]

puts [dict size $colours]

When the above code is executed, it produces the following result −


2

Dictionary Iteration
A simple dictionary iteration for printing keys and valued of the dictionary is shown below −
Live Demo

#!/usr/bin/tclsh

set colours [dict create colour1 "black" colour2 "white"]

foreach item [dict keys $colours] {

set value [dict get $colours $item]

puts $value

When the above code is executed, it produces the following result −


black
white

Value for Key in Dict


The syntax for retrieving value for key in dict is shown below −
[dict get $dictname $keyname]

An example for retrieving value for key is given below −


Live Demo

#!/usr/bin/tclsh

set colours [dict create colour1 "black" colour2 "white"]

set value [dict get $colours colour1]

puts $value

When the above code is executed, it produces the following result −


black

All Keys in Dict


The syntax for retrieving all keys in dict is shown below −
[dict keys $dictname]

An example for printing all keys is shown below −


Live Demo

#!/usr/bin/tclsh

set colours [dict create colour1 "black" colour2 "white"]

set keys [dict keys $colours]

puts $keys

When the above code is executed, it produces the following result −


colour1 colour2

All Values in Dict


The syntax for retrieving all values in dict is shown below −
[dict values $dictname]

An example for printing all values is shown below −


Live Demo

#!/usr/bin/tclsh
set colours [dict create colour1 "black" colour2 "white"]

set values [dict values $colours]

puts $values

When the above code is executed, it produces the following result −


black white

Key Exists in Dict


The syntax for checking if a key exists in dict is shown below −
[dict exists $dictname $key]

An example for checking if a key exists in dict is shown below −


Live Demo

#!/usr/bin/tclsh

set colours [dict create colour1 "black" colour2 "white"]

set result [dict exists $colours colour1]

puts $result

When the above code is executed, it produces the following result −


1

Input/output
Tcl uses objects called channels to read and write data. The channels can be created using
the openor socket command. There are three standard channels available to Tcl scripts without
explicitly creating them. They are automatically opened by the OS for each new application.
They are stdin,stdout and stderr. The standard input, stdin, is used by the scripts to read data. The
standard output, stdout, is used by scripts to write data. The standard error, stderr, is used by
scripts to write error messages.

In the first example, we will work with the puts command. It has the following synopsis:

puts ?-nonewline? ?channelId? string

The channelId is the channel where we want to write text. The channelId is optional. If not
specified, the default stdout is assumed.

#!/usr/bin/tclsh

puts "Message 1"


puts stdout "Message 2"
puts stderr "Message 3"

The puts command writes text to the channel.

puts "Message 1"

If we do not specify the channelId, we write to stdout by default.

puts stdout "Message 2"

This line does the same thing as the previous one. We only have explicitly specified the
channelId.

puts stderr "Message 3"

We write to the standard error channel. The error messages go to the terminal by default.

$ ./printing.tcl
Message 1
Message 2
Message 3

Example output.

The read command


The read command is used to read data from a channel. The optional argument specifies the
number of characters to read. If omitted, the command reads all of the data from the channel
up to the end.

#!/usr/bin/tclsh

set c [read stdin 1]

while {$c != "q"} {

puts -nonewline "$c"


set c [read stdin 1]
}

The script reads a character from the standard input channel and then writes it to the standard
output until it encounters the q character.

set c [read stdin 1]

We read one character from the standard input channel (stdin).

while {$c != "q"} {


We continue reading characters until the q is pressed.

The gets command


The gets command reads the next line from the channel, returns everything in the line up to
(but not including) the end-of-line character.

#!/usr/bin/tclsh

puts -nonewline "Enter your name: "


flush stdout
set name [gets stdin]

puts "Hello $name"

The script asks for input from the user and then prints a message.

puts -nonewline "Enter your name: "

The puts command is used to print messages to the terminal. The -nonewline option supresses
the new line character.

flush stdout

Tcl buffers output internally, so characters written with puts may not appear immediately on
the output file or device. The flush command forces the output to appear immediately.

set name [gets stdin]

The gets command reads a line from a channel.

$ ./hello.tcl
Enter your name: Jan
Hello Jan

Sample output of the script.

The pwd and cd commands


Tcl has pwd and cd commands, similar to shell commands. The pwd command returns the
current working directory and the cd command is used to change the working directory.

#!/usr/bin/tclsh

set dir [pwd]

puts $dir
cd ..

set dir [pwd]


puts $dir

In this script, we will print the current working directory. Then we change the working
directory and print the working directory again.

set dir [pwd]

The pwd command returns the current working directory.

cd ..

We change the working directory to the parent of the current directory. We use
the cd command.

$ ./cwd.tcl
/home/janbodnar/prog/tcl/io
/home/janbodnar/prog/tcl

Procedures
Procedures are nothing but code blocks with series of commands that provide a
specific reusable functionality. It is used to avoid same code being repeated in multiple
locations. Procedures are equivalent to the functions used in many programming languages
and are made available in Tcl with the help of proc command.

The syntax of creating a simple procedure is shown below −


proc procedureName {arguments} {
body
}

A simple example for procedure is given below −


Live Demo

#!/usr/bin/tclsh

proc helloWorld {} {

puts "Hello, World!"

helloWorld
When the above code is executed, it produces the following result −
Hello, World!

Procedures with Multiple Arguments


An example for procedure with arguments is shown below −
Live Demo

#!/usr/bin/tclsh

proc add {a b} {

return [expr $a+$b]

puts [add 10 30]

When the above code is executed, it produces the following result −


40

Procedures with Variable Arguments


An example for procedure with arguments is shown below −
Live Demo

#!/usr/bin/tclsh

proc avg {numbers} {

set sum 0

foreach number $numbers {

set sum [expr $sum + $number]

set average [expr $sum/[llength $numbers]]

return $average

puts [avg {70 80 50 60}]

puts [avg {70 80 50 }]

When the above code is executed, it produces the following result −


65
66

Procedures with Default Arguments


Default arguments are used to provide default values that can be used if no value is
provided. An example for procedure with default arguments, which is sometimes referred as
implicit arguments is shown below −
Live Demo

#!/usr/bin/tclsh

proc add {a {b 100} } {

return [expr $a+$b]

puts [add 10 30]

puts [add 10]

When the above code is executed, it produces the following result −


40
110

Recursive Procedures
An example for recursive procedures is shown below −
Live Demo

#!/usr/bin/tclsh

proc factorial {number} {

if {$number <= 1} {

return 1

return [expr $number * [factorial [expr $number - 1]]]

puts [factorial 3]
puts [factorial 5]

When the above code is executed, it produces the following result −


6
120

Strings
The primitive data-type of Tcl is string and often we can find quotes on Tcl as
string only language. These strings can contain alphanumeric character, just numbers,
Boolean, or even binary data. Tcl uses 16 bit unicode characters and alphanumeric characters
can contain letters including non-Latin characters, number or punctuation.

Boolean value can be represented as 1, yes or true for true and 0, no, or false for false.

String Representations
Unlike other languages, in Tcl, you need not include double quotes when it's only a single
word. An example can be

#!/usr/bin/tclsh

set myVariable hello

puts $myVariable

When the above code is executed, it produces the following result −


hello

When we want to represent multiple strings, we can use either double quotes or curly braces.
It is shown below −

#!/usr/bin/tclsh

set myVariable "hello world"

puts $myVariable

set myVariable {hello world}

puts $myVariable

When the above code is executed, it produces the following result −


hello world
hello world

String Escape Sequence


A character literal can be a plain character (e.g., 'x'), an escape sequence (e.g., '\t'), or a
universal character (e.g., '\u02C0').

There are certain characters in Tcl when they are preceded by a backslash they will have
special meaning and they are used to represent like newline (\n) or tab (\t). Here, you have a
list of some of such escape sequence codes −

Escape sequence Meaning

\\ \ character

\' ' character

\" " character

\? ? character

\a Alert or bell

\b Backspace

\f Form feed

\n Newline

\r Carriage return

\t Horizontal tab

\v Vertical tab

Following is the example to show a few escape sequence characters −


#!/usr/bin/tclsh

puts "Hello\tWorld\n\nTutorialspoint";

When the above code is compiled and executed, it produces the following result −
Hello World

Tutorialspoint

String Command
The list of subcommands for string command is listed in the following table −

Sr.No. Methods & Description

1
compare string1 string2

Compares string1 and string2 lexographically. Returns 0 if equal, -1 if


string1 comes before string2, else 1.

2
first string1 string2

Returns the index first occurrence of string1 in string2. If not found, returns
-1.

3
index string index

Returns the character at index.

4
last string1 string2

Returns the index last occurrence of string1 in string2. If not found, returns
-1.

5
length string

Returns the length of string.

6
match pattern string

Returns 1 if the string matches the pattern.


7
range string index1 index2

Return the range of characters in string from index1 to index2.

8
tolower string

Returns the lowercase string.

9
toupper string

Returns the uppercase string.

10
trim string ?trimcharacters?

Removes trimcharacters in both ends of string. The default trimcharacters


is whitespace.

11
trimleft string ?trimcharacters?

Removes trimcharacters in left beginning of string. The default


trimcharacters is whitespace.

12
trimright string ?trimcharacters?

Removes trimcharacters in left end of string. The default trimcharacters is


whitespace.

13
wordend findstring index

Return the index in findstring of the character after the word containing the
character at index.

14
wordstart findstring index

Return the index in findstring of the first character in the word containing
the character at index.

Examples of some commonly used Tcl string sub commands are given below.

String Comparison
#!/usr/bin/tclsh
set s1 "Hello"

set s2 "World"

set s3 "World"

puts [string compare s1 s2]

if {[string compare s2 s3] == 0} {

puts "String \'s1\' and \'s2\' are same.";

if {[string compare s1 s2] == -1} {

puts "String \'s1\' comes before \'s2\'.";

if {[string compare s2 s1] == 1} {

puts "String \'s2\' comes after \'s1\'.";

When the above code is compiled and executed, it produces the following result −
-1
String 's1' comes before 's2'.
String 's2' comes after 's1'.

Index of String
#!/usr/bin/tclsh

set s1 "Hello World"

set s2 "o"

puts "First occurrence of $s2 in s1"

puts [string first $s2 $s1]

puts "Character at index 0 in s1"

puts [string index $s1 0]

puts "Last occurrence of $s2 in s1"

puts [string last $s2 $s1]


puts "Word end index in s1"

puts [string wordend $s1 20]

puts "Word start index in s1"

puts [string wordstart $s1 20]

When the above code is compiled and executed, it produces the following result −
First occurrence of o in s1
4
Character at index 0 in s1
H
Last occurrence of o in s1
7
Word end index in s1
11
Word start index in s1
6

Length of String
#!/usr/bin/tclsh

set s1 "Hello World"

puts "Length of string s1"

puts [string length $s1]

When the above code is compiled and executed, it produces the following result −
Length of string s1
11

Handling Cases
#!/usr/bin/tclsh

set s1 "Hello World"

puts "Uppercase string of s1"

puts [string toupper $s1]

puts "Lowercase string of s1"

puts [string tolower $s1]

When the above code is compiled and executed, it produces the following result −
Uppercase string of s1
HELLO WORLD
Lowercase string of s1
hello world

Trimming Characters
#!/usr/bin/tclsh

set s1 "Hello World"

set s2 "World"

puts "Trim right $s2 in $s1"

puts [string trimright $s1 $s2]

set s2 "Hello"

puts "Trim left $s2 in $s1"

puts [string trimleft $s1 $s2]

set s1 " Hello World "

set s2 " "

puts "Trim characters s1 on both sides of s2"

puts [string trim $s1 $s2]

When the above code is compiled and executed, it produces the following result −
Trim right World in Hello World
Hello
Trim left Hello in Hello World
World
Trim characters s1 on both sides of s2
Hello World

Matching Strings
#!/usr/bin/tclsh

set s1 "[email protected]"

set s2 "*@*.com"

puts "Matching pattern s2 in s1"

puts [string match "*@*.com" $s1 ]


puts "Matching pattern tcl in s1"

puts [string match {tcl} $s1]

When the above code is compiled and executed, it produces the following result −
Matching pattern s2 in s1
1
Matching pattern tcl in s1
0

Append Command
#!/usr/bin/tclsh

set s1 "Hello"

append s1 " World"

puts $s1

When the above code is compiled and executed, it produces the following result −
Hello World

Format command
The following table shows the list of format specifiers available in Tcl −

Specifier Use

%s String representation

%d Integer representation

%f Floating point representation

%e Floating point representation with mantissa-exponent form

%x Hexa decimal representation

Some simple examples are given below −

#!/usr/bin/tclsh
puts [format "%f" 43.5]

puts [format "%e" 43.5]

puts [format "%d %s" 4 tuts]

puts [format "%s" "Tcl Language"]

puts [format "%x" 40]

When the above code is compiled and executed, it produces the following result −
43.500000
4.350000e+01
4 tuts
Tcl Language
28

Scan command
Scan command is used for parsing a string based to the format specifier. Some examples are
shown below.

#!/usr/bin/tclsh

puts [scan "90" {%[0-9]} m]

puts [scan "abc" {%[a-z]} m]

puts [scan "abc" {%[A-Z]} m]

puts [scan "ABC" {%[A-Z]} m]

When the above code is compiled and executed, it produces the following result −
1
1
0
1

Pattern
The "regexp" command is used to match a regular expression in Tcl. A regular
expression is a sequence of characters that contains a search pattern. It consists of multiple
rules and the following table explains these rules and corresponding use.

Sr.No. Rule & Description


1
x

Exact match.

2
[a-z]

Any lowercase letter from a-z.

3
.

Any character.

4
^

Beginning string should match.

5
$

Ending string should match.

6
\^

Backlash sequence to match special character ^.Similarly you can use for
other characters.

7
()

Add the above sequences inside parenthesis to make a regular expression.

8
x*

Should match 0 or more occurrences of the preceding x.

9
x+

Should match 1 or more occurrences of the preceding x.

10
[a-z]?

Should match 0 or 1 occurrence of the preceding x.

11
{digit}
Matches exactly digit occurrences of previous regex expression. Digit that
contains 0-9.

12
{digit,}

Matches 3 or more digit occurrences of previous regex expression. Digit


that contains 0-9.

13
{digit1,digit2}

Occurrences matches the range between digit1 and digit2 occurrences of


previous regex expression.

Syntax
The syntax for regex is given below −
regexp optionalSwitches patterns searchString fullMatch subMatch1 ... subMatchn

Here, regex is the command. We will see about optional switches later. Patterns are the rules
as mentioned earlier. Search string is the actual string on which the regex is performed. Full
match is any variable to hold the result of matched regex result. Submatch1 to SubMatchn
are optional subMatch variable that holds the result of sub match patterns.

Let's look at some simple examples before diving into complex ones. A simple example for
a string with any alphabets. When any other character is encountered the regex, search will
be stopped and returned.
Live Demo

#!/usr/bin/tclsh

regexp {([A-Z,a-z]*)} "Tcl Tutorial" a b

puts "Full Match: $a"

puts "Sub Match1: $b"

When the above code is executed, it produces the following result −


Full Match: Tcl
Sub Match1: Tcl

Multiple Patterns
The following example shows how to search for multiple patterns. This is example pattern
for any alphabets followed by any character followed by any alphabets.
Live Demo

#!/usr/bin/tclsh

regexp {([A-Z,a-z]*).([A-Z,a-z]*)} "Tcl Tutorial" a b c

puts "Full Match: $a"

puts "Sub Match1: $b"

puts "Sub Match2: $c"

When the above code is executed, it produces the following result −


Full Match: Tcl Tutorial
Sub Match1: Tcl
Sub Match2: Tutorial

A modified version of the above code to show that a sub pattern can contain multiple
patterns is shown below −
Live Demo

#!/usr/bin/tclsh

regexp {([A-Z,a-z]*.([A-Z,a-z]*))} "Tcl Tutorial" a b c

puts "Full Match: $a"

puts "Sub Match1: $b"

puts "Sub Match2: $c"

When the above code is executed, it produces the following result −


Full Match: Tcl Tutorial
Sub Match1: Tcl Tutorial
Sub Match2: Tutorial

Switches for Regex Command


The list of switches available in Tcl are,

 nocase − Used to ignore case.

 indices − Store location of matched sub patterns instead of matched characters.

 line − New line sensitive matching. Ignores the characters after newline.

 start index − Sets the offset of start of search pattern.

 Marks the end of switches


In the above examples, I have deliberately used [A-Z, a-z] for all alphabets, you can easily
use -nocase instead of as shown below −
Live Demo

#!/usr/bin/tclsh

regexp -nocase {([A-Z]*.([A-Z]*))} "Tcl Tutorial" a b c

puts "Full Match: $a"

puts "Sub Match1: $b"

puts "Sub Match2: $c"

When the above code is executed, it produces the following result −


Full Match: Tcl Tutorial
Sub Match1: Tcl Tutorial
Sub Match2: Tutorial

Another example using switches is shown below −


Live Demo

#!/usr/bin/tclsh

regexp -nocase -line -- {([A-Z]*.([A-Z]*))} "Tcl \nTutorial" a b

puts "Full Match: $a"

puts "Sub Match1: $b"

regexp -nocase -start 4 -line -- {([A-Z]*.([A-Z]*))} "Tcl \nTutorial" a b

puts "Full Match: $a"

puts "Sub Match1: $b"

When the above code is executed, it produces the following result −


Full Match: Tcl
Sub Match1: Tcl
Full Match: Tutorial
Sub Match1: Tutorial

Files
Tcl supports file handling with the help of the built in commands open,
read, puts, gets, and close.
A file represents a sequence of bytes, does not matter if it is a text file or binary file.

Opening Files
Tcl uses the open command to open files in Tcl. The syntax for opening a file is as follows −
open fileName accessMode

Here, filename is string literal, which you will use to name your file and accessMode can
have one of the following values −

Sr.No. Mode & Description

1
r

Opens an existing text file for reading purpose and the file must exist. This
is the default mode used when no accessMode is specified.

2
w

Opens a text file for writing, if it does not exist, then a new file is created
else existing file is truncated.

3
a

Opens a text file for writing in appending mode and file must exist. Here,
your program will start appending content in the existing file content.

4
r+

Opens a text file for reading and writing both. File must exist already.

5
w+

Opens a text file for reading and writing both. It first truncate the file to
zero length if it exists otherwise create the file if it does not exist.

6
a+

Opens a text file for reading and writing both. It creates the file if it does
not exist. The reading will start from the beginning, but writing can only be
appended.

Closing a File
To close a file, use the close command. The syntax for close is as follows −
close fileName

Any file that has been opened by a program must be closed when the program finishes using
that file. In most cases, the files need not be closed explicitly; they are closed automatically
when File objects are terminated automatically.

Writing a File
Puts command is used to write to an open file.
puts $filename "text to write"

A simple example for writing to a file is shown below.

#!/usr/bin/tclsh

set fp [open "input.txt" w+]

puts $fp "test"

close $fp

When the above code is compiled and executed, it creates a new file input.txt in the
directory that it has been started under (in the program's working directory).

Reading a File
Following is the simple command to read from a file −
set file_data [read $fp]

A complete example of read and write is shown below −


Live Demo

#!/usr/bin/tclsh

set fp [open "input.txt" w+]

puts $fp "test"

close $fp

set fp [open "input.txt" r]

set file_data [read $fp]

puts $file_data
close $fp

When the above code is compiled and executed, it reads the file created in previous section
and produces the following result −
test

Here is another example for reading file till end of file line by line −
Live Demo

#!/usr/bin/tclsh

set fp [open "input.txt" w+]

puts $fp "test\ntest"

close $fp

set fp [open "input.txt" r]

while { [gets $fp data] >= 0 } {

puts $data

close $fp

When the above code is compiled and executed, it reads the file created in previous section
and produces the following result −
test
test
Advanced Tcl
Eval:
eval is one of the Tcl constructs that causes the Tcl interpreter to scan over its arguments another
time and evaluate what it scans.

Given the following assignment,

set b {the total is $20}

each of the following commands returns a is different result:

set a $b
eval {set a $b}
eval "set a $b"
eval [list set a $b]

Consider the first one,

set a $b

Prior to invoking set, Tcl performs its substitutions, so the command becomes

set a {the total is $20}

set receives the arguments a, and the total is $20, does not perform any internal parsing or substitution on
its own, and sets the value of $a tothe total is $20.

In the second one,

eval {set a $b}

Tcl does not perform any substitutions on the value in curly brackets, so the command remains
unchanged. eval receives one argument,

set a $b

which it then hands back to the interpreter. This time the interpreter sees set a $b, which becomes:

set a {the total is $20}

so the value of $a becomes the total is $20.

In the third example,

eval "set a $b"

Tcl changes the command to

eval {set a the total is $20}


eval receives the argument

set a the total is $20

and hands it to the interpreter for evaluation. This time, the interpreter sees

set a the total is $20

fails to find the variable $20 and raises an error. If there had been no variable error, the interpreter would
have invoked set with too many arguments, causing set to raise an error.

eval [list set a $b]

Tcl changes the command to eval {set a {the total is $20}}, set receives the arguments a {the total is $20},
and the value of $a becomes the total is $20.

Source and exec: source arranges for the Tcl interpreter to evaluate the contents of the
specified file or resource exactly as if eval were called at that point with an argument identical to the the
literal content of the given file, up to the first occurrence of ^Z (ASCII character 26) in that file, except that
any returnoccurring in the evaluated script is handled slightly differently. In contrast to eval,
any return does not cause the caller of source to return. uplevel0 ... is the better analogy.

If an error occurs in evaluating the contents of filename, source returns that error.

'\32' (dec 26) (^Z) is used as the end-of-stream character for all platforms. source reads only up to the first
instance of character. This restriction does not exist for read or gets, allowing files to contain both
containing code and data segments. If you require a ^Z in code for string comparison, you can use \032 or \
u001a, which will be safely substituted by the Tcl interpreter into ^Z.

filename is normally read using the system encoding. Override this behaviour with -encoding.

proc sourceOnce file {


upvar 1 sources sources

if {![info exists sources([file normalize $file])]} then {


# don't catch errors, since that may indicate we failed to load it...?
uplevel "1" [list source $file]
# mark it as loaded since it was source'd with no error...
set sources([file normalize $file]) "1"
}
}

exec processes any special control values in args and executes the program named by the first
remaining arg, passing it any additional remainingargs, each as a separate argument to the
program. exec does not use other shells to invoke the program, so it is not necessary to add any extra layers
of quoting to args.

Special control values are used to redirect stdout, stderr, and stdin, to form and execute a multi-program
pipeline, and/or to cause the execution to occur in the background. By default, exec captures and returns
the content the program produces on stdout. In background mode, exec returns the process identifiers of all
the processes in the pipeline.

Each program in a pipeline can have its own redirections. When a particular redirection operator is given
twice for a command, the last one wins.

When arguments passed to the program may be confused with exec operators such as <, >, |, or &,
use lexec instead.

Name Spaces
Namespace is a container for set of identifiers that is used to group variables and
procedures. Namespaces are available from Tcl version 8.0. Before the introduction of the
namespaces, there was single global scope. Now with namespaces, we have additional
partitions of global scope.

Creating Namespace
Namespaces are created using the namespace command. A simple example for creating
namespace is shown below −
Live Demo

#!/usr/bin/tclsh

namespace eval MyMath {

# Create a variable inside the namespace

variable myResult

# Create procedures inside the namespace

proc MyMath::Add {a b } {

set ::MyMath::myResult [expr $a + $b]

MyMath::Add 10 23

puts $::MyMath::myResult

When the above code is executed, it produces the following result −


33

In the above program, you can see there is a namespace with a variable myResult and a
procedure Add. This makes it possible to create variables and procedures with the same
names under different namespaces.

Nested Namespaces
Tcl allows nesting of namespaces. A simple example for nesting namespaces is given below

Live Demo

#!/usr/bin/tclsh

namespace eval MyMath {

# Create a variable inside the namespace

variable myResult

namespace eval extendedMath {

# Create a variable inside the namespace

namespace eval MyMath {

# Create a variable inside the namespace

variable myResult

set ::MyMath::myResult "test1"

puts $::MyMath::myResult

set ::extendedMath::MyMath::myResult "test2"

puts $::extendedMath::MyMath::myResult

When the above code is executed, it produces the following result −


test1
test2

Importing and Exporting Namespace


You can see in the previous namespace examples, we use a lot of scope resolution operator
and it's more complex to use. We can avoid this by importing and exporting namespaces. An
example is given below −
Live Demo

#!/usr/bin/tclsh

namespace eval MyMath {

# Create a variable inside the namespace

variable myResult

namespace export Add

# Create procedures inside the namespace

proc MyMath::Add {a b } {

return [expr $a + $b]

namespace import MyMath::*

puts [Add 10 30]

When the above code is executed, it produces the following result −


40

Forget Namespace
You can remove an imported namespace by using forgetsubcommand. A simple example is
shown below −
Live Demo

#!/usr/bin/tclsh

namespace eval MyMath {

# Create a variable inside the namespace

variable myResult
namespace export Add

# Create procedures inside the namespace

proc MyMath::Add {a b } {

return [expr $a + $b]

namespace import MyMath::*

puts [Add 10 30]

namespace forget MyMath::*

When the above code is executed, it produces the following result −


40

Trapping errors
Error handling in Tcl is provided with the help of error and catchcommands. The
syntax for each of these commands is shown below.

Error syntax
error message info code

In the above error command syntax, message is the error message, info is set in the global
variable errorInfo and code is set in the global variable errorCode.

Catch Syntax
catch script resultVarName

In the above catch command syntax, script is the code to be executed, resultVarName is
variable that holds the error or the result. The catch command returns 0 if there is no error,
and 1 if there is an error.

An example for simple error handling is shown below −


Live Demo

#!/usr/bin/tclsh
proc Div {a b} {

if {$b == 0} {

error "Error generated by error" "Info String for error" 401

} else {

return [expr $a/$b]

if {[catch {puts "Result = [Div 10 0]"} errmsg]} {

puts "ErrorMsg: $errmsg"

puts "ErrorCode: $errorCode"

puts "ErrorInfo:\n$errorInfo\n"

if {[catch {puts "Result = [Div 10 2]"} errmsg]} {

puts "ErrorMsg: $errmsg"

puts "ErrorCode: $errorCode"

puts "ErrorInfo:\n$errorInfo\n"

When the above code is executed, it produces the following result −


ErrorMsg: Error generated by error
ErrorCode: 401
ErrorInfo:
Info String for error
(procedure "Div" line 1)
invoked from within
"Div 10 0"

Result = 5

As you can see in the above example, we can create our own custom error messages.
Similarly, it is possible to catch the error generated by Tcl. An example is shown below −
Live Demo

#!/usr/bin/tclsh
catch {set file [open myNonexistingfile.txt]} result

puts "ErrorMsg: $result"

puts "ErrorCode: $errorCode"

puts "ErrorInfo:\n$errorInfo\n"

When the above code is executed, it produces the following result −


ErrorMsg: couldn't open "myNonexistingfile.txt": no such file or directory
ErrorCode: POSIX ENOENT {no such file or directory}
ErrorInfo:
couldn't open "myNonexistingfile.txt": no such file or directory
while executing
"open myNonexistingfile.txt"

Event driven programs


One of the reasons why Tcl is considered a highly powerful programming language is because of
its event driven input/output model which permeates into almost everything ranging from graphic user
interfaces (GUIs), networks, files and more. This lends a lot of credibility to the Tcl programming
language, making it highly consistent, without the need of relying on the more complex add-on packages
or mechanisms, which become a necessity in most languages.

When compared to other programming languages, dealing with asynchronous activities like GUI actions,
data availability on sockets, files becoming readable and more become a matter of pure joy in Tcl. Other
languages treat all such activities differently and require certain add-on packages or mechanisms for them.
What sets Tcl apart are:

Its unified model - All Tcl Tk programs dealing with input/output operations have a single event loop as
their main characteristic. Events get fired and Tcl callbacks get invoked when anything happens on the
timers, GUI, sockets, files or other kinds of input sources.

There’s no need of threads for performing basic asynchronous input/output operations, and no add-on
packages are needed for this functionality. In Tcl, all this is inbuilt and easy to comprehend. It’s all
handled in a very consistent manner across the board.

Its callbacks - Callbacks become very easy with the ‘string’ model of Tcl. They can be any Tcl command
by itself, an object invocation or a simple procedure call. There’s no rigid structure involved! Writing
callbacks in Tcl is natural and simple.

Internet aware Nuts and Bolts Internent programming


The public Internet is a worldwide computer network, that is, a network that
interconnects millions of computing devices throughout the world.
In Internet world, all of these devices are called hosts or end systems. Hosts or end
systems are connected together by communication links. They are indirectly connected to
each other through intermediate switching devices know as packed switches.

Different links can transmit data at different rates, with the transmission rate of a link
measured in bits/second.

Packet switches come in many shapes and flavors, but two most prominent types in today's
Internet are routers and link-layer switches. Both types of switches forward packets toward
their untlimate destinations.

From the sending end system to the receiving end system, the sequence of communication
links and packet switches traversed by a packet is known as a route or path through the
network. Rather than provide a dedicated path between communicating end systems, the
Internet uses a technique known as packet switching that allows multiple communicating
end systems to share a path, or parts of a path, at the same time.

End systems access the Internet through Internet Service Provider (ISPs) including
residential ISPs.End systems, packet switches, and other pieces of the Internet,
run protocols that control the sending and receiving of information within the INternet,
The Transmission Control Protocol (TCP)and the Internet Protocol(IP) are two of the
most important protocols in the Internet. The IP protocols specifices the format of the packets
that are sent and received among routers and end systems. The Internet's principal protocols
are collectively known ad TCP/IP.

Internet standards are developed by the Internet Engineering Task Force(IETF). The IETF
standards documents are called request for comments (RFCs). RFCs started out as general
request for comments to resolve architecture problems that faced the precursor to the Internet.
These private networks are often referred to as intranets, as they use the same types of host,
routers, links, and protocols as the public Internet.

You might also like