Practical Programming in Tcl and Tk
Total Page:16
File Type:pdf, Size:1020Kb
Practical Programming in Tcl and Tk Brent Welch DRAFT, January 13, 1995 Updated for Tcl 7.4 and Tk 4.0 THIS IS NOT THE PUBLISHED TEXT THE INDEX IS INCOMPLETE SOME SECTIONS ARE MISSING THE MANUSCIRPT HAS NOT BEEN EDITED GET THE REAL BOOK: ISBN 0-13-182007-9 An enhanced version of this text has been published by Prentice Hall: ISBN 0-13-182007-9 Send comments via email to [email protected] with the word “book” in the subject. http://www.sunlabs.com/~bwelch/book/index.html The book is under copyright. Print for personal use only. This on-line DRAFT is available curtesty the kind folks at PH. Table of Contents 1. Tcl Fundamentals ............................................. 1 Getting Started ............................................................1 Tcl Commands .............................................................2 Hello World ..................................................................3 Variables ......................................................................3 Command Substitution ................................................4 Math Expressions ........................................................4 Backslash Substitution ................................................6 Double Quotes .............................................................7 Procedures ...................................................................7 A While Loop Example ..................................................8 Grouping And Command Substitution .......................10 More About Variable Substitution ..............................11 Substitution And Grouping Summary ........................11 Fine Points .................................................................12 Comments .................................................................13 Command Line Arguments .........................................13 Reference ...................................................................14 Backslash Sequences ...........................................14 Arithmetic Operators ...........................................14 Built-in Math Functions ......................................15 Core Tcl Commands .............................................15 Predefined Variables ............................................18 2. Strings and Pattern Matching ......................... 19 The string Command ..................................................19 Strings And Expresssions ...........................................20 The append Command ...............................................21 The format Command ................................................21 The scan Command ...................................................23 String Matching .........................................................24 Regular Expressions ..................................................25 The regexp Command ................................................26 The regsub Command ................................................28 Created: May 9, 1994 —bookTOC.doc—Copyright Prentice Hall—DRAFT: 1/13/95 ii 3. Tcl Data Structures .........................................29 More About Variables .................................................29 The unset command ............................................30 Using info to find out about variables ...................30 Tcl Lists .....................................................................31 Constructing Lists: list, lappend, and concat ..............32 Getting List Elements: llength, lindex, and lrange .......33 Modifying Lists: linsert and lreplace ...........................34 Searching Lists: lsearch .............................................34 Sorting Lists: lsort ......................................................35 The split And join Commands ....................................35 Arrays ........................................................................36 The array Command ..................................................37 Environment Variables ...............................................38 Tracing Variable Values .............................................39 4. Control Flow Commands .................................41 If Then Else ................................................................42 Switch ........................................................................43 Foreach ......................................................................44 While .........................................................................45 For .............................................................................46 Break And Continue ...................................................46 Catch .........................................................................46 Error ..........................................................................48 Return .......................................................................49 5. Procedures and Scope .....................................51 The proc Command ....................................................51 Changing command names with rename ....................52 Scope .........................................................................53 The global Command .................................................53 Use Arrays for Global State ........................................55 Call By Name Using upvar ..........................................55 Passing arrays by name .......................................56 The uplevel Command ................................................57 6. Eval ................................................................59 Eval And List .............................................................59 Created: May 9, 1994 —bookTOC.doc—Copyright Prentice Hall—DRAFT: 1/13/95 iii Eval And Concat ........................................................61 Double-quotes and eval .......................................62 Commands That Concat Their Arguments ..................62 The subst Command ..................................................63 7. Working with UNIX ......................................... 65 Running Unix Programs With exec .............................65 auto_noexec .........................................................67 Looking At The File System ........................................67 Input/Output ............................................................70 Opening Files For I/O ................................................70 Reading And Writing ..................................................72 The puts and gets commands ..............................72 The read command ..............................................73 Random access I/O .............................................73 Closing I/O streams .............................................74 The Current Directory - cd And pwd ...........................74 Matching File Names With glob ..................................74 The exit And pid commands .......................................75 8. Reflection and Debugging ............................... 77 The info Command .....................................................77 Variables .............................................................78 Procedures ...........................................................79 The call stack ......................................................79 Command evaluation ...........................................80 Scripts and the library .........................................80 Version numbers .................................................80 Interactive Command History .....................................81 History syntax .....................................................82 A comparision to /bin/csh history syntax ............ 82 Debugging ..................................................................83 Don Libes’ debugger ...................................................84 Breakpoints by pattern matching .........................85 Deleting break points ...........................................86 The tkerror Command ................................................87 The tkinspect Program ...............................................87 Performance Tuning ...................................................87 Created: May 9, 1994 —bookTOC.doc—Copyright Prentice Hall—DRAFT: 1/13/95 iv 9. Script Libraries ...............................................89 The unknown Command ............................................89 The tclIndex File .........................................................90 Using A Library: auto_path .........................................90 Disabling the library facility: auto_noload ............91 How Auto Loading Works ...........................................91 Interactive Conveniences ............................................92 Auto Execute .......................................................92 History .................................................................92 Abbreviations .......................................................92 Tcl Shell Library Environment ....................................93 Coding Style ...............................................................93 A module prefix for procedure names ...................94 A global array for state variables ..........................94 10. Tk Fundamentals ..........................................95 Hello World In Tk .......................................................96 Naming Tk Widgets ....................................................98 Configuring Tk Widgets ..............................................98 About The Tk Man Pages ............................................99