Unit-Iii TCL
Unit-Iii TCL
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 −
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.
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,
Embedded applications.
TCL/Tk Structure and Syntax
Tcl is quite simple to learn and let's start creating our first Tcl program!
#!/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
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
Multiline or block comment is written using 'if' with condition '0'. An example is shown
below.
Live Demo
#!/usr/bin/tclsh
if 0 {
#!/usr/bin/tclsh
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.
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
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
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
#!/usr/bin/tclsh
set variableA 10
puts $variableA
puts ${variable B}
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
puts $variableA
set sum [expr $variableA +20];
puts $sum
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
puts $result
puts $result
puts $result
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
puts $result
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.
1 if statement
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.
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;
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.
1 while loop
3 nested loops
You can use one or more loop inside any another while, for or do..while
loop.
1 break statement
2 continue statement
Causes the loop to skip the remainder of its body and immediately retest its
condition prior to reiterating.
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.
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:
#!/usr/bin/tclsh
puts $languages(0)
puts $languages(1)
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
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
for { set index 0 } { $index < [array size languages] } { incr index } {
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(Age) 14
puts $personA(Name)
puts $personA(Age)
Indices of Array
The syntax for retrieving indices of array is shown below.
#!/usr/bin/tclsh
set personA(Age) 14
#!/usr/bin/tclsh
set personA(Age) 14
foreach index [array names personA] {
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]
#!/usr/bin/tclsh
puts $colorList1
puts $colorList2
puts $colorList3
#!/usr/bin/tclsh
puts $var
Length of List
The syntax for length of list is given below −
llength listName
#!/usr/bin/tclsh
#!/usr/bin/tclsh
#!/usr/bin/tclsh
puts $var
#!/usr/bin/tclsh
puts $var
#!/usr/bin/tclsh
puts $var
#!/usr/bin/tclsh
puts $colour1
puts $colour2
Sorting a List
The syntax for sorting a list is given below −
lsort listname
#!/usr/bin/tclsh
puts $var
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
#!/usr/bin/tclsh
puts $colours
puts $colours
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]
#!/usr/bin/tclsh
Dictionary Iteration
A simple dictionary iteration for printing keys and valued of the dictionary is shown below −
Live Demo
#!/usr/bin/tclsh
puts $value
#!/usr/bin/tclsh
puts $value
#!/usr/bin/tclsh
puts $keys
#!/usr/bin/tclsh
set colours [dict create colour1 "black" colour2 "white"]
puts $values
#!/usr/bin/tclsh
puts $result
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:
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
This line does the same thing as the previous one. We only have explicitly specified the
channelId.
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.
#!/usr/bin/tclsh
The script reads a character from the standard input channel and then writes it to the standard
output until it encounters the q character.
#!/usr/bin/tclsh
The script asks for input from the user and then prints a message.
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.
$ ./hello.tcl
Enter your name: Jan
Hello Jan
#!/usr/bin/tclsh
puts $dir
cd ..
In this script, we will print the current working directory. Then we change the working
directory and print the working directory again.
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.
#!/usr/bin/tclsh
proc helloWorld {} {
helloWorld
When the above code is executed, it produces the following result −
Hello, World!
#!/usr/bin/tclsh
proc add {a b} {
#!/usr/bin/tclsh
set sum 0
return $average
#!/usr/bin/tclsh
Recursive Procedures
An example for recursive procedures is shown below −
Live Demo
#!/usr/bin/tclsh
if {$number <= 1} {
return 1
puts [factorial 3]
puts [factorial 5]
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
puts $myVariable
When we want to represent multiple strings, we can use either double quotes or curly braces.
It is shown below −
#!/usr/bin/tclsh
puts $myVariable
puts $myVariable
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 −
\\ \ character
\? ? character
\a Alert or bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
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 −
1
compare string1 string2
2
first string1 string2
Returns the index first occurrence of string1 in string2. If not found, returns
-1.
3
index string index
4
last string1 string2
Returns the index last occurrence of string1 in string2. If not found, returns
-1.
5
length string
6
match pattern string
8
tolower string
9
toupper string
10
trim string ?trimcharacters?
11
trimleft string ?trimcharacters?
12
trimright string ?trimcharacters?
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"
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 s2 "o"
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
When the above code is compiled and executed, it produces the following result −
Length of string s1
11
Handling Cases
#!/usr/bin/tclsh
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 s2 "World"
set s2 "Hello"
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"
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"
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
#!/usr/bin/tclsh
puts [format "%f" 43.5]
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
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.
Exact match.
2
[a-z]
3
.
Any character.
4
^
5
$
6
\^
Backlash sequence to match special character ^.Similarly you can use for
other characters.
7
()
8
x*
9
x+
10
[a-z]?
11
{digit}
Matches exactly digit occurrences of previous regex expression. Digit that
contains 0-9.
12
{digit,}
13
{digit1,digit2}
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
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
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
line − New line sensitive matching. Ignores the characters after newline.
#!/usr/bin/tclsh
#!/usr/bin/tclsh
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 −
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"
#!/usr/bin/tclsh
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]
#!/usr/bin/tclsh
close $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
close $fp
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.
set a $b
eval {set a $b}
eval "set a $b"
eval [list set a $b]
set a $b
Prior to invoking set, Tcl performs its substitutions, so the command becomes
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.
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:
and hands it to the interpreter for evaluation. This time, the interpreter sees
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.
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.
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
variable myResult
proc MyMath::Add {a b } {
MyMath::Add 10 23
puts $::MyMath::myResult
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
variable myResult
variable myResult
puts $::MyMath::myResult
puts $::extendedMath::MyMath::myResult
#!/usr/bin/tclsh
variable myResult
proc MyMath::Add {a b } {
Forget Namespace
You can remove an imported namespace by using forgetsubcommand. A simple example is
shown below −
Live Demo
#!/usr/bin/tclsh
variable myResult
namespace export Add
proc MyMath::Add {a b } {
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.
#!/usr/bin/tclsh
proc Div {a b} {
if {$b == 0} {
} else {
puts "ErrorInfo:\n$errorInfo\n"
puts "ErrorInfo:\n$errorInfo\n"
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 "ErrorInfo:\n$errorInfo\n"
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.
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.