64
Tango Training 1 Tcl Programming CyberDaemons

tcl語法介紹

  • Upload
    cooper

  • View
    47

  • Download
    3

Embed Size (px)

Citation preview

Tcl ProgrammingCyberDaemons

Tango Training

1

AgendaTcl (List)

Tango Training

2

TCL Tcl Tool Control Language Tk Tcl(Extension) GUI Tcl shell script C tk buttons menuslistboxes scrollbars Tcl Basic Tcl Script Tcl Tcl Script Tcl tclsh Tcl (Extension)

Tango Training

3

Tcl John K. Ousterhout 1988 Tcl/Tk Tk 1989 1991 Tk usable around 1992 see http://www.tcl.tk/doc/tclHistory.html

Tango Training

4

Tcl high-level scripting language less code than Motif or Win32

most platforms Unix, Mac, Windows hides UI, system call differences

interpreted execute directly, without compiling or linking

autoloading automatically load libraries

extensible commands in Tcl or C

free source no royalties

embeddable Tcl interpreter callable from CTango Training 5

Tcl

Tango Training

6

tcltclsh$ tclsh % set x 7 7

wish$ wish % button .b text Hello command exit % pack .b

Tango Training

7

tclC#include main(int argc, char *argv[]) { Tcl_interp *interp = Tcl_CreateInterp(); code = Tcl_EvalFile(interp, argv[1]); if (*interp->result != 0) { printf(%s\n, interp->result); } }Tango Training 8

TCLhello.tcl:set s "Hello World" puts $s Command: $tclsh hello.tcl Result: tclsh (tclsh) Hello World

or tcl

Tango Training

9

Tcl Everything is a list of words no fixed grammarfirst word is command

{} delay evaluation, may nest "" only needed when spaces: set x "foo" = set x foo

everything can be done dynamically: new procedures, variables, name spaces, ... interpreter model, but internally compiled into bytecodeTango Training 10

(variable)setEx.: set a 101

unsetEx.: unset a

Tango Training

11

(variable substituion) variable substitution: set a 17 command substitution, evaluated as separate script: set b [expr $a*4] backslash substitution: set x \$a

set b 18 set a [expr $b+2] set a "b-3 is [expr $b-3]"Tango Training

18 20 b-3 is 1512

incr increments variable append adds strings to end: append msg "more text" argv variable is list of command line arguments env is list of environment variables

Tango Training

13

(Math expressions) expr : expr arg ?arg arg ...?

:if {$x == $y} { ...}set x [expr {$x + 7}] set y [expr {log($y)}][]

Tango Training

14

set b 5 expr ($b*4) - 3 expr $b = 90} { puts "Your level is A" } elseif {$grade < 90} { puts "Your level isnt A" } else { puts "You set wrong data" }

Result:Your level is A

Tango Training

44

switchswitch ?option? string pattern body ?pattern body. switch ?option? string {pattern body ?pattern body}

Tango Training

45

(switch)Ex.:set num 1 switch $num { 1 {puts "Im 2 {puts "Im 3 {puts "Im default {puts } David."} Terry."} Ruby."} "Whats your name?"}

Result:Im David.

Tango Training

46

While Loop while booleanExpr body

Tango Training

47

(while loop)1+2++100Ex.: set a 0 set ans 0 while {$a < 101} { set ans [expr $ans+$a] incr a 1 } puts $ans Result: 5050Tango Training 48

Foreach loop foreach loopVar valueList commandBody

Tango Training

49

(foreach loop)1+2++10Ex.: Set j 0 foreach i {1 2 3 4 5 6 7 8 9 10} { set j [expr $i+$j] } puts $j Result: 55

Tango Training

50

For loop for initial test final body

Tango Training

51

(for loop)1+2++100Ex.: set ans 0 for {set a 0} {$a < 101} {incr a 1} { set ans [expr $ans+$a] } puts $ans Result: 5050

Tango Training

52

!

Tango Training

53

Tcl(Tcl procedures)proc {1 2} { Body }

Procedures behave just like built-in commands:sub1 3 => 2

Arguments can have default values:proc decr {x {y 1}} { expr $x-$y }Tango Training 54

procedures can be created dynamicallyproc power {base p} { set result 1 while {$p > 0} { set result [expr $result * $base] set p [expr $p-1] } return $result }

power 2 6

Tango Training

55

Regular expressionsTclregexp regsub egrep ^$.+?\>\returns 1 regexp T(cl|k) "I mention Tk" w t => returns 1 (match), w becomes "Tk", t gets "k"Tango Training 57

. (period) ^ $ \x [chars] (regexp) * + ? | matches any character matches start of a string matches end of a string single character escape matches any of chars. ^: not. -: range. matches the regexp matches 0 or more of the preceding matches 1 or more of the preceding matches 0 or 1 of the preceding can be used to divide alternatives.Tango Training 58

catch catch,TCL_OK, 10 Can intercept errors (like exception handling):catch {expr {2 +}} msg 1 (catch returns 0=OK, 1=err, other values...) puts msg syntax error in expression "2 +"....

Tango Training

59

(File I/O)Tcl file I/O commands:open close fblocked eof gets seek flush read tell cd fileevent puts pwd filename glob fconfigure source

File commands use 'tokens' to refer to files set f [open "myfile.txt" "r"] => file1 () puts $f "Write this text into file" close $fTango Training 60

gets and puts are line oriented set x [gets $f] reads one line of $f into x set f [open /etc/passwd r] => file2 () gets $f => root:x:0:0:root:/root:/bin/bash

Tango Training

61

read can read specific numbers of bytes read $f 100 => (up to 100 bytes of file $f) seek, tell, and read can do random-access I/O set f [open "database" "r"] seek $f 1024 read $f 100 => (bytes 1024-1123 of file $f)

Tango Training

62

infowhat variables are there?info vars, info globals, info locals, info exists

what procedures have I defined, and how?info procs, info args, info default, info body, info commands

proc ff {abc} {puts Hello} info args ff =>abc

Tango Training

63

Tango Training

64