Showing posts with label Tcl Tutorial. Show all posts
Showing posts with label Tcl Tutorial. Show all posts

Tuesday, April 4, 2017

Tcl - Overview

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 - Environment Setup

Try it Option Online

We have set up the Tcl/Tk Programming environment online, so that you can compile and execute all the available examples online. It gives you confidence in what you are reading and enables you to verify the programs with different options. Feel free to modify any example and execute it online.

Tcl - Special Variables

In Tcl, we classify some of the variables as special variables and they have a predefined usage/functionality. The list of specials variables is listed below.
S.No. Special Variable & Description
1 argc
Refers to a number of command-line arguments.
2 argv
Refers to the list containing the command-line arguments.

Tcl - Basic 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.
#!/usr/bin/tclsh

Tcl - Commands

As you know, Tcl is a Tool command language, commands are the most vital part of the language. Tcl commands are built in-to the language with each having its own predefined function. These commands form the reserved words of the language and cannot be used for other variable naming. The advantage with these Tcl commands is that, you can define your own implementation for any of these commands to replace the original built-in functionality.

Tcl - Data Types

The primitive data-type of Tcl is string and often we can find quotes on Tcl as string only language. These primitive data-types in turn create composite data-types for list and associative array. In Tcl, data-types can represent not only the simple Tcl objects, but also can represent complex objects such as handles, graphic objects (mostly widgets), and I/O channels. Let's look into the details about each of the above.

Tcl - Variables

In Tcl, there is no concept of variable declaration. Once, a new variable name is encountered, Tcl will define a new variable.

Variable Naming

The name of variables can contain any characters and length. You can even have white spaces by enclosing the variable in curly braces, but it is not preferred.
The set command is used for assigning value to a variable. The syntax for set command is,
set variableName value

Tcl - Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Tcl language is rich in built-in operators and provides the following types of operators −
  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Ternary Operator

Tcl - Decisions

Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
Following is the general form of a typical decision making structure found in most of the programming languages −

Tcl - Loops

There may be a situation, where you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.

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

Tcl - 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.

Tcl - Lists

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.

Tcl - 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 −
#!/usr/bin/tclsh

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.

Tcl - Packages

Packages are used for creating reusable units of code. A package consists of a collection of files that provide specific functionality. This collection of files is identified by a package name and can have multiple versions of same files. The package can be a collection of Tcl scripts, binary library, or a combination of both.
Package uses the concept of namespace to avoid collision of variable names and procedure names. Check out more in our next 'namespace' tutorial.

Tcl - Namespaces

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.

Tcl - File I/O

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

Tcl - Error Handling

Error handling in Tcl is provided with the help of error and catch commands. 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.

Tcl - Built-in Functions

Tcl provides a number of built-in functions (procedures) for various operations. This includes −
  • Functions for list handling.
  • Functions for string handling.
  • Functions for array handling.
  • Functions for dictionary handling.
  • Functions for File I/O handling.
  • Functions for creating namespaces and packages.
  • Functions for Math operations.
  • Functions for System operations.