Quick Introduction to Tcl

From nsnam
Jump to: navigation, search

Tcl, the Tool Command Language, is an interpreted scripting language. It is used by the NS-2 network simulator to build simulation scenarios. This page provides enough information on Tcl to understand a simple simulation scenario for NS-2.

A simple Tcl script looks like this (assuming that the Tcl shell is installed in /usr/local/bin/tclsh):

  1:  #!/usr/local/bin/tclsh
  2:  set count 20                  ;# The number of Fibonacci numbers to print
  3:  set num(0) 0; set num(1) 1    ;# First and second seed values
  4:  puts "The first $count Fibonacci numbers:"
  5:  for { set i 1 } { $i < $count } { incr i 1 } {
  6:    puts "$i:\t$num(0)"
  7:    set num(1) [ expr $num(0) + $num(1) ]
  8:    incr i 1
  9:    puts "$i:\t$num(1)"
 10:    set num(0) [ expr $num(0) + $num(1) ]
 11:  }

For comparison, the equivilent C source is:

  1:  #include <stdio.h>
  2:  int main( int argc, char *argv[] ) {
  3:    int count = 20;             /* The number of Fibonacci numbers to print */
  4:    int num[] = { 0, 1 };       /* First and second seed values */
  5:    int i;
  6:    printf( "The first %d Fibonacci numbers:\n", count );
  7:    for ( i = 1; i < count; i++ ) {
  8:      printf( "%d:\t%d\n", i, num[0] );
  9:      num[1] = num[0] + num[1];
 10:      i++;
 11:      printf( "%d:\t%d\n", i, num[1] );
 12:      num[0] = num[0] + num[1];
 13:    }
 14:    return 0;
 15:  }

Here is the line-by-line breakdown of the TCL script:

  1:  #!/usr/local/bin/tclsh

This line runs the Tcl Shell, "tclsh". This is a shell that allows you to run Tcl scripts or interactively enter Tcl commands, similar to shells such as csh and bash.

Another way to write this is:

 #!/bin/sh
 # the next line restarts using tclsh \
 exec tclsh "$0" "$@"

This method uses the sh shell to initially run the script. sh then executes tclsh, passing the script name as the first argument. When tclsh executes the script, it skips over these three lines and executes the rest of the script. The trick to getting Tcl to skip the third line is the trailing backslash (\) on the second line. To sh, this backslash has no meaning, but for tclsh it causes the comment to span both the second and third lines. The tclsh man page, http://www.tcl.tk/man/tcl8.4/UserCmd/tclsh.htm, lists three advantages to using this method:

  • The location of the tclsh binary doesn't have to be hard-wired into the script: it can be anywhere in your shell search path.
  • It gets around the 30-character file name limit in the previous approach (some Unix systems only allow around 30 characters on #! line of a shell script).
  • This approach will work even if tclsh is itself a shell script.
  2:  set count 20 ;# The number of Fibonacci numbers to print

This line sets the value of the variable "count" to 20. The equivilent C command is:

 int count = 20; /* The number of Fibonacci numbers to print */

In Tcl, you do not need to declare a variable before using it, and variables are essentially untyped.

Note that we need a semicolon before the start of the comment. This is because Tcl requires comments to start at the beginning of a command. Normally this is the beginning of a line, but we can use a semicolon to force the beginning of a command in the middle of a line. Semicolons are also needed if there is more than one command on a line.

  3:  set num(0) 0; set num(1) 1 ;# First and second seed values

This is an example of a Tcl array. The first element of the array is set to zero, and the second element is set to 1. Notice that a semicolon is needed to separate the two commands. The equivilent C command is:

 int num[2]; num[0] = 0; num[1] = 1; /* First and second seed values */

or

 int num[] = { 0, 1 }; /* First and second seed values */
  4:  puts "The first $count Fibonacci numbers:"

This line prints a string, substituting in the current value of "count". This is one of two types of strings that Tcl supports.

  5:  for { set i 1 } { $i < $count } { incr i 1 } {</tt></p>

This line is the start of a for loop. The equivilent C command is:

 for ( i = 0; i < count; i++ ) {

Here we also see the second type of Tcl string. Each pair of curly braces ({}) is simply a string where variable substitution does not take place, similar to single quoted strings in most shell scripts. Also, a string surrounded by curly braces can span multiple lines. Double-quoted strings (and Tcl commands in general) require a backslash at the end of the line if they span multiple lines.

  6:  puts "$i:\t$num(0)"

This line prints a Fibonacci number.

  7:  set num(1) [ expr $num(0) + $num(1) ]

This line sets the second element of the array "num" to the sum of the first and second elements. The equivilent C command is:

 num[1] = num[0] + num[1];

In Tcl, the square braces ([]) indicate command substitution. The command inside the square braces is executed and the return value is substituted.

  8:  incr i 1

This line increments the variable i. The equivilent C command is:

 i++;

This can also be achived in Tcl with:

 set i [ expr i + 1 ]
  9:  puts "$i:\t$num(1)"

This line prints the next Fibonacci number.

 10:  set num(0) [ expr $num(0) + $num(1) ]

This line sets the first element of the array "num" to the sum of the first and second elements.

 11:  }

This line terminates the string started at the end of the "for" statement.

This covers most of the Tcl basics. One fundamental part of Tcl that was not covered is the procedure. In fact, almost everything in Tcl, including commands such as "for" and "incr" are procedures. "for" is a procedure that takes four strings as arguments, while "incr" is a procedure that takes a variable name followed by an optional increment amount.

What this means is that the syntax of Tcl is very simple and flexible. The four arguments you pass to "for" have no special meaning to Tcl itself. It is only the "for" procedure that cares about the contents of the arguments. You could even write your own for-like procedure that takes C code instead of Tcl.

Essentially, Tcl has untyped variables, strings (with and without variable substitution), command substition, and procedures. Object-oriented Tcl (OTcl, which NS-2 is based on) adds objects with methods.

With this knowledge, you should be able to tackle the NS-2 Tutorials. These tutorials use a little more advanced Tcl. In particular, you will see how to define your own procedures, how to use variables from the main script in the procedures, and how to call the methods of variables that are ojects (the "Simulator" object you will meet in the first script is one such object).