Writing Tcl Scripts - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Writing Tcl Scripts

Description:

Different commands assign different meanings to their arguments. set a 122. expr 24/3.2 ... Variable name is letters, digits, underscores. May occur anywhere ... – PowerPoint PPT presentation

Number of Views:117
Avg rating:3.0/5.0
Slides: 16
Provided by: dmhwa
Category:

less

Transcript and Presenter's Notes

Title: Writing Tcl Scripts


1
Writing Tcl Scripts
  • Outline
  • Syntax
  • Data Types
  • Control Structures
  • Variable Scoping
  • Goal
  • Understand Tcl syntax, data types, control
    structures, scoping
  • Reading
  • Ch. 10-13, Practical Programming in Tcl and Tk

2
Basics
  • Tcl script
  • sequence of commands
  • commands separated by newlines, semicolons
  • in place of command is comment line
  • Tcl command
  • one or more words separated by spaces
  • first word is command name, others are arguments
  • cmd arg1 arg2 arg3 . . .
  • Examples
  • set a 22 set b 33
  • set a 22
  • set b 33

3
Division of Responsibility
Command
Chops command into words, makes substitutions.
Does not interpret values of words.
Tcl Parser
Words
Command Procedure
Interprets words, produces string result.
Result
4
Arguments
  • Parser assigns no meaning to arguments (quoting
    by default, evaluation is special)
  • C x 4 y x10
  • y is 14
  • Tcl set x 4 set y x10
  • y is x10
  • Different commands assign different meanings to
    their arguments
  • set a 122
  • expr 24/3.2
  • eval set a 122
  • button .b -text Hello -fg red
  • string length Abracadabra

5
Variable Substitution
  • Syntax varName
  • Variable name is letters, digits, underscores
  • May occur anywhere within a word
  • Sample command Result
  • set b 66 66
  • set a b b
  • set a b 66
  • set a bbb 666666
  • set a b.3 66.3
  • set a b4 no such variable

6
Command Substitution
  • Syntax script
  • Execute script, substitute result
  • May occur anywhere within a word
  • Sample Command Result
  • set b 8 8
  • set a expr b2 10
  • set a b-3 is expr b-3 b-3 is 5

7
Controlling Word Structure
  • Words break at white space and semicolons except
  • double-quotes prevent breaks
  • set a This is one big word with spaces
  • curly braces prevent breaks and substitutions
  • set a nested braces
  • backslashes quote special characters
  • set a word\ with\ \\ and\ space
  • Substitutions do not change word structure
  • set a two words - a is one word
  • set b a - b is still one word

8
Syntax Summary
  • Script commands separated by newlines,
    semicolons
  • Command words separated by white space
  • at beginning of command is comment
  • causes variable substitution
  • causes command substitution
  • Only one level of substitution done by parser
  • quotes white space and semicolons
  • still do substitutions
  • \ quotes next character
  • also provides some special characters like \n,
    \t, etc.
  • quotes all characters - no substitutions

9
Expressions
  • C-like (int and double) plus strings
  • Used in expr, conditional commands
  • Commands perform substitutions within expression
    arguments
  • can and should put braces around arguments
  • Sample command Result
  • set b 5 5
  • expr (b4)-3 17
  • expr blt2 0
  • expr b fac 4 120
  • set a Bill Bill
  • expr a lt Anne 0

10
Lists
  • Zero or more elements separated by white space
  • red green blue
  • Braces and backslashes for grouping
  • zero one twoA twoB twoC three
  • one\ word two three
  • List-related commands
  • concat, foreach, lappend, lindex, linsert, list,
    llength, lrange, lreplace, lsearch, lsort
  • Example
  • lindex a b c d e f 2
  • returns c d e
  • Lists parse as commands each element becomes
    one word

11
Associative Arrays
  • Syntax
  • arrayname(index)
  • Strings as array indices
  • set x(fred) 44
  • set x(2) expr x(fred)6
  • result 50
  • Array command - searching, size queries
  • array names x
  • returns fred 2
  • foreach i array names x
  • set x(i) 0

12
Control Structures
  • C-like appearance
  • Just commands that take Tcl scripts as arguments
  • Example
  • if x lt 3
  • puts stdout x is too small!
  • set x 3
  • Commands
  • if, for, foreach, while, case, break, continue,
    eval

13
Control Structures (cont.)
  • Should usually put braces around control command
    arguments
  • set i 0
  • for set i 0 i lt 10 incr i 3
  • lappend aList i
  • set aList
  • gt 0 3 6 9
  • If no braces, then i lt 10 --gt 0 lt 10, always
    true, infinite loop

14
Procedures
  • proc command defines procedure
  • proc sub1 x expr x-1
  • Procedures behave just like built-in commands
  • sub1 3 returns 2
  • Arguments can have defaults
  • proc decr x y 1 expr x-y
  • Can have variable number of arguments
  • proc foo a b args . . .

name
body
list of argument names
gets list of extra args
15
Variable Scope
  • Variables in proc are local by default
  • Must explicitly reference global variables
  • global command makes global variables locally
    accessible
  • only in procedures where it appears
  • proc foo a
  • global x
  • set x a
  • proc bar a
  • global x
  • puts stdout x is x
  • foo 5
  • bar 3
  • gt x is 5
Write a Comment
User Comments (0)
About PowerShow.com