English 中文(简体)
Tcl - Basic Syntax
  • 时间:2024-12-22

Tcl - Basic Syntax


Previous Page Next Page  

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.

#!/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 pne or semicolon to terminate the previous pne of code. But semicolon is not necessary, if you are using newpne for each command.

Comments

Comments are pke 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

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

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

Hello World!

Multipne or block comment is written using if with condition 0 . An example is shown below.

#!/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!

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

#!/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 Manpower and 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

Whitespace in Tcl

A pne containing only whitespace, possibly with a comment, is known as a blank pne, and a Tcl interpreter totally ignores it.

Whitespace is the term used in Tcl to describe blanks, tabs, newpne 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 −

#!/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 readabipty purpose.

Advertisements