Neatware Company

An ISV for Mobile, Cloud, and Video Technologies and Software.

1. Sequence

Sequence organizes the command flow in sequence. Each command is executed one after another. Following paragraphs introduce primitive sequence commands.

set

set command assigns a value to a variable. Its first argument is a variable and the second argument is a value. You can use a variable anywhere without declare its type. To access a variable uses the dollar sign $.

  set v "a string value"
  set u $v

incr

The first argument of incr command is a variable name. The second optional argument is increment integer. incr command increases variable value by increment. Negative increment will decrease a variable. The default value of increment is 1.

  incr x                 # x increase 1
  incr x -1              # x decrease 1
  incr x [expr {2*3}]    # x increment

expr

expr command evaluates math expression with C expression syntax. It concatenates all the arguments to form an input string. The expression may be integer, double, and boolean. The return value is a numeric string. Many basic math functions in the standard C math library has been built in.

you do not need to use $x to get the x value in the expr expression. This works more like the C expression. However you need to use $foo(0) as an element of an array foo to distinguish a function such as foo(x).

  expr {x+y}                         # return x+y
  expr {asin(1.0)*round(sqrt(2))}    # return 1.579079632679 

comment

The comment of MCL starts with # in the start of a line. It is better to treate comment # as a command. There are some quirks prohibited you from writing comment anywhere. (i.e. no # inside the switch command.)

  set x [expr 2*3];   # comment here.
  # comment start 

puts

puts command outputs its argument string to console. It has only one argument.

  puts "output a $string and a [compute $value]" 

2. Condition

Condition control may select a command to execute according to the variable value. It branches the command flow.

if

if command will execute truebody when the boolean-expression is true, otherwise it will execute elsebody. Its body is a group of commands. The else and elseif keyword are optional for if command. boolean-expression is an expr expression. Therefore you can use a variable to represent its value without using the $ substitution. Following codes are several forms of if command:

  if {boolean-expression} {truebody}

  if {boolean-expression} {truebody} else {elsebody}

  if {boolean-expression1} {
    truebody1
  } elseif {boolean-expression2} {
    truebody2
  } else {
    elsebody
  }

switch

The syntax of switch command is:

  switch option value pattern body ... 
  switch option value {pattern body ...}

switch command compares a value with patterns. If one of them is matched then program executes the related body. The first argument of the switch command is an option. The '-exact' attribute will match the value to the pattern exactly; '-glob' attribute will use glob pattern matching; and '-regexp' will match with regular expression pattern. '--' represents the end of the option. The last pattern 'default' will execute its body if no patterns are matched before.

In the following example, since pattern and body pairs are grouped into an argument, there is no substitution inside the pattern/body pairs.

  switch -exact -- $val {
    first   {doFirst}
    second  {doSecond}
    third   {doThird}
    default {doDefault}
  }

following example can substitute its patterns

  switch -glob -- $val  $v1 do_v1  $v2 do_v2  $v3 do_v3

3. Loop

Loop commands execute a group of commands in iteration. The iteration may terminate after all the elements are traversed or a condition expression becomes true.

foreach

foreach command repeatly executes its body until all the elements in a list have been traversed. Its form is,

  foreach var alist ... body

The var is the current loop variable that is assigned an element from the alist one after another. foreach will traverse all the elements in the alist. This command is a compact expression of iteration.

  foreach v {a b c d e} {
    puts $v
  }

You can declare two or more loop variables. The variables will orderly sample the elements in the list until all of them are traversed. Following example shows that varaiable (v1 v2) pair is assigned the value (a b) respectively, and then the value (c d), and so on.

  foreach {v1 v2} {a b c d e f} {
    puts "($v1 $v2)"
  }

To loop over multiple lists, you may organize arguments in var/list pair order. The variable var may also be multiple variables. A loop variable will be set to empty {} when its list has finished traveser but the entire loop did not terminated.

  foreach v {a b c d} {v1 v2} {1 2 3 4 5 6} {
    puts "($v) ($v1 $v2)"
  }

while

while command evaluates the boolean-expression, if it is true then execute the body, and then evaluates the boolean-expression again until the boolean-expression is not true. Its syntax is like while statement of C language.

  while boolean-expression body 

An example of while command,

  set count 7 
  while {count > 0} { 
    puts "2*$count" 
    incr count -1 
  }

for

The for command syntax is,

  for initial boolean-expression increment body

At first it evaluates the initail argument and then evaluates the boolean-expression. If the boolean-expression is true it executes the body and increment. Repeatlly, it evaluates the boolean-expression again and continues the loop until boolean-expression returns a false value.

  set len 7
  for {set count 0} {count < len} {incr count 1} {
    puts "2*$count"
  }

4. Return

return, break, continue

return command comes back from a procedure with a value; break command exits from a loop; and continue command will goto the start of a loop to execute the next iteration.

  set b 6
  set c 5
  set len 7
  for {set count 0} {count < len} {incr count 1} {
    if {count == b} {
      break
    } elseif {count == c} {
      continue
    } 
  }
  return $c 

5. Exception

Exceptions raise abnormal conditions during the execution of commands.

catch

catch command caught the exceptions of a command during its execution. Its syntax is,

  catch {command args ... } result

catch command sets trap to the command in the curly braces. When there is an exception during the command execution, the exception message is assigned to the variable 'result', otherwise the 'result' gets the return value of the command. catch command returns zero when no exception is raised, otherwise returns non-zero.

  if [catch {test $exception} result] {
    puts "Exception: $result"
  } else {
    puts "OK: $result"
  }

error

error command generates an error code. Its first argument is a string that indicates the reason of an error.

  catch {...} errmsg
  error $errmsg