Sei sulla pagina 1di 26

TCL

OVERVIEW
Outline

Intro/Environment

Basic Syntax

Data Types

Flow Control

Procedure

Application of Tcl in ATVN


Introduction
Environment
Introduction

TCL stand for Tool Command Language

TCL is Interpreted
(process at run-time and no need to compile)

TCL is Interactive

TCL is a String-based command language


(Little syntax, simple and easy to learn, ..)
Environment
There are three different ways to work with TCL
Interactive (through TCL Shell : tclsh)
Script
Integrated Development Environment (IDE)
(DLTK of Eclipse)
Environment - Interactive
You can enter tclsh (tclsh8.4, tclsh8.5, ..) and start
coding right away in the interactive interpreter
[nguyenndk@linuxsws002 ~]$ tclsh8.4
% puts "Hello TCL
Hello TCL
% set a 5
5
% set b 10
10
% puts "$a + $b = [expr $a+$b]"
5 + 10 = 15
%
Environment - Script
You can create a tcl script and run it

[nguyenndk@linuxsws002 temp]$ tclsh8.4 tcl_sample.tcl


Hello TCL
5 + 10 = 15
[nguyenndk@linuxsws002 temp]$
[nguyenndk@linuxsws002 temp]$ ./tcl_sample.tcl
Hello TCL
5 + 10 = 15
[nguyenndk@linuxsws002 temp]$
Environment - IDE
You can run TCL from a graphical user interface
(GUI) environment
Eclipse with DLTK
(Dynamic Languages Toolkit TCL IDE)
Basic Syntax
Text Output & Assigning values
Evaluation & Substitutions
Basic Syntax
Text Output & Assigning Values
1. Simple Text Output
[Syntax] puts {TextOutput}
[Example] puts Hello TCL
2. Assigning Values
[Command] set {VariableName} {Value}
[Example] set a 5
Notes:
- String without space can be used without quotes
- Multi commands in 1 line separate by semicolon ;
- Comment use # (need semicolon if after command)
[Example] set a 5; puts HelloTCL; # 2 command in 1 lines
Basic Syntax
Evaluation & Substitutions (1)
1. Value substitution:
[Format] $variableName
[Example] set a 5
set b $a; # value of b is 5
set b a; # value of b is a

2. Command substitution:
The Tcl interpreter takes everything between the brackets and evaluates it as a
command

[Format] [ command substitution ]


[Example] set a [string length Hello TCL]
# First, interpreter evaluate command [string length Hello TCL] => 9;
then assign 9 to a.
Basic Syntax
Evaluation & Substitutions (2)
Double quotes and braces are used to group words together. The
difference between double quotes and curly braces { } is that
quotes allow substitutions to occur in the group, while curly braces
prevent substitutions.
[Example]
% set s Hello
Hello
% puts stdout "The length of $s is [string length $s]."
The length of Hello is 5.
% puts stdout {The length of $s is [string length $s].}
The length of $s is [string length $s].
%
Basic Syntax - Others
Use expr to evaluate math expressions
[Example] expr 7.2 / 3 => 2.4
For some special character, use backslash
[Example] puts \$ => $
You can concatenates 2 command result without space
[Example] puts [set b "Hello "][set c TCL] => Hello TCL
You can delete value of variable by command unset

% set a {Hello TCL}


Hello TCL
% set b a
a
% set b [set a]
Hello TCL
% unset a
% set b [set a]
can't read "a": no such variable
%
Data Types
The basic data structure in Tcl is a string.
In addition, there are two higher-level data
structures, lists and arrays.
Data Types - List
In Tcl, a list is simply a string with list elements separated by white space. There are several
Tcl commands related to lists:

Syntax Description
list arg1 arg2 ... Creates a list out of all its arguments.
lindex list i Returns the ith element from list.
llength list Returns the number of elements in list.
lrange list i j Returns the ith through jth elements from list.
lappend listVar arg arg ... Append a elements to the value of listVar.
linsert list index arg arg .. Insert elements into list before position index. Returns a new list.
lreplace list i j arg arg ... Replace elements i through j of list with the args. Returns a new list.
lsearch mode list value Return the index of the element in list. Return -1 if not found.
lsort switches list Sort elements of the list. Returns a new list.
concat arg arg arg ... Join multiple lists together into one list.
split string splitChars Split a string up into list elements.

Notes : You can create list by using group like {} (without using command list)
Example: set listA 01 02 03
set listA {01 02 03}
set listA [list 01 02 03]

Notes: The lappend command is unique among the list-related commands because its first
argument is the name of a list-valued variable, while all the other commands take list
values as arguments.
Example: llength $listA
lappend listA 04
Data Types - Array
The other primary data structure that Tcl has is arrays. An array is a variable with a string-
valued index, so you can think of an array as a mapping from strings to strings :
set array1(index1) value1
set array1(index2) value2
Some basic array commands:
Syntax Description
array exists arr Returns 1 if arr is an array variable.
array get arr Returns a list that corresponding array.
array names arr Return the list of all indices defined for arr
array size arr Return the number of indices defined for arr.
Example:
% set arr(idx1) 12345
12345
% set arr(idx2) HelloTcl
HelloTcl
% array exist arr
1
% array get arr
idx1 12345 idx2 HelloTcl
% array names arr
idx1 idx2
% array size arr
2
Flow Control
Decision (if, elseif, else)
Loops (for, foreach, while, break,
continue)
Exceptions (try, except)
Flow Control - Decision
if boolean body1 elseif body2 else body3

% set x -6
-6
% if {$x > 0} {
puts Positive
} elseif {$x == 0} {
puts Zero
} else {
puts Negative
}
Negative
%
Flow Control Loops (1)
for initial test next body
(Notes: Use incr command for next)

% for {set i 4} {$i<6} {incr i} {


puts "i = $i
}
i = 4
i = 5
%

foreach loopVar valueList commandBody

% set listA [list 4 5]


4 5
% foreach i $listA {
puts "i = $i"
}
i = 4
i = 5
%
Flow Control Loops (2)
while booleanExpr body

% set i 4
4
% while {$i<6} {
puts "i = $i
incr i
}
i = 4
i = 5
%

[Notes]
break : causes immediate exit from a loop
continue : command causes the loop to continue with the next iteration.
Procedure
syntax, scope
Function - Syntax
[Define] proc name params body

[Call] name params

% proc sum { a b } {
puts "$a + $b = [expr $a+$b]
}
%
% sum 5 10
5 + 10 = 15
%
Function - Scope
Local scope:
-Each procedure has a local scope for variables.
-Variables defined outside the procedure are not visible to a procedure.

Global Scope
- For variables defined outside the procedure be able to visible outside use global
command

% set myGlobalList [list 00 01]


00 01
% proc updateList02 { } {
global myGlobalList
lappend myGlobalList 02
}
% updateList02
00 01 02
%
Application in ATVN
In ATVN, TCL is used for create
manual script to configure ATVN SDK
(AF5, AF6, ..)
Reference

http://www.tcl.tk/man/tcl8.5/tutorial/tcltutorial.html
http://www.beedub.com/book/
Practical Programming in Tcl and Tk (book).pdf
- The End -
Thank You

Potrebbero piacerti anche