TCL Scripting Session 1
TCL Scripting Session 1
Scripting
SESSION - 1
- Akhilesh Mahajan
Todays Agenda
Why TCL?
TCL Commands
Variables Declaration
Substitution
Basics of Lists
Why TCL?
Simplicity
GUI
Development Tools
Internationalization
Extensibility
Embed ability
Compatibility
Event Handling
Comparison Chart
Getting Started
Tcl programs are usually called "scripts"
The binaries and libraries of Tcl/Tk and its
extensions are generally installed under /usr/bin
and /usr/lib.
You need to set up a few environment variables
in your .cshrc/.bashrc
setenv TCL_LIBRARY ~/tcl/itcl/lib/tcl7.4
setenv TK_LIBRARY ~/tcl/itcl/lib/tk4.0
setenv ITCL_LIBRARY ~/tcl/itcl/lib/itcl2.0
setenv ITK_LIBRARY ~/tcl/itcl/lib/itk2.0
setenv EXPECT_LIBRARY ~/tcl/expect/lib
TCL Commands
Built-in Commands
Extensions
Procedures
Parser
Init
Command
Loop
Application
Commands
Built-In
Commands
Tcl Application
Extension
Commands
Extension
Variable Declaration
Set varName ?value?
e.g. set x 40
Same command is use to Read and Write
You don't need to declare variables
Variables created automatically
Variables do not have types
Any variable can hold any value
Variable Declaration (Cont.)
Other Examples:
set a {Eggs: $3.5/dozen}
Eggs: $3.5/dozen
set i 3
set a [expr $i + 2]
5
set msg Starent Networks
Starent Networks
Unset a Variable
unset ?varname?
Substitution
TCL provides THREE types of
substitution
1. Variable Substitution
2. Command Substitution
3. Backslash Substitution
Substitution always occurs from left to
right
Variable Substitution
Syntax: $varName
Variable names are letters, digits, underscores.
May occur anywhere in a word.
Sample command Result
set b 66 66
set a b b
set a $b 66
set a $b+$b+$b 66+66+66
set a $b.3 66.3
set a $b4 no such variable
Command Substitution
Syntax: [script]
Evaluate script, substitute result.
May occur anywhere within a word.
Sample command Result
set b 8 8
set a [expr $b+2] 10
set a "b-3 is [expr $b-3]" b-3 is 5
Backslash Substitution
Used to insert special characters such as
newline
[ ]
$
without them being treated specially by TCL
parser.
set msg Eggs:\ \$2.18/dozen\nGasoline:\
\$1.49/gallon
Eggs: $2.18/dozen
Gasoline: $1.49/gallon
Quoting
Words break at white space and semi-colons, except:
Double-quotes prevent breaks:
set a "x is $x; y is $y"
Curly braces prevent breaks and substitutions:
set a {[expr $b*$c]}
Backslashes quote special characters:
set a word\ with\ \$\ and\ space
Expressions
expr arg ?arg arg ...?
Concatenates all the arg values together (with spaces in
between), evaluates the result as an expression, and returns
a string corresponding to the expressions value.
Sample command Result
set b 5 5
incr b -1 4
expr ($b*4) - 3 17
expr $b <= 2 0
expr $a * cos(2*$b) -5.03443
expr {$b * [fac 4]} 120
set a Bill Bill
expr {$a < "Anne"} 0
Operators
Available Operators:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Choice Operators
Identical to C, C++, Java.
List
Lists are used in Tcl to deal with collections of things
1. such as all the users in a group or
2. all the files in a directory
Lists allow you to collect together any
number of values in one place, pass
around the collection as a single entity,
and later get the component values back
again
Creating List
Four ways to create a list
Set x {a b c d e} or
Set y a b c d e or
Set z [list a b c d e]
Set x [concat $x $y $z]
concat removes one level of grouping before
forming the list, while list works directly from
the original arguments
List and Concat
The command
list a b "c d e " " f {g h}"
will return
a b {c d e } { f {g h}}
while concat with the same arguments will
return
a b c d e f {g h}
Split and Join
Split a string into a proper Tcl list
Join create a string by joining list elements
together
Set dir /usr/bin/radius
Set abc [Split $dir /]
usr bin radius
Set abc [join $abc ;]
usr;bin;radius
Operation on List
lappend
lindex
linsert
llength
lrange
lreplace
lsearch
lsort
lrepeat