1 / 14

Writing Tcl Scripts (cont.)

Outline Variable Scoping Strings Eval File/Channel I/O Processes System Info Errors Reflection/Debugging Libraries Goal Understand how to write Tcl programs Reading Ch. 14-18, Practical Programming in Tcl and Tk. Writing Tcl Scripts (cont.). upvar - call by reference

perrin
Download Presentation

Writing Tcl Scripts (cont.)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Outline Variable Scoping Strings Eval File/Channel I/O Processes System Info Errors Reflection/Debugging Libraries Goal Understand how to write Tcl programs Reading Ch. 14-18, Practical Programming in Tcl and Tk Writing Tcl Scripts (cont.)

  2. upvar - call by reference use to pass variable name rather than value useful for when passing arrays to procs upvar associates local variable with variable in higher context upvar ?level? varName localvar #0 is global scope level is 1 (one level up) by default example proc incrArray { array index amount } { upvar $array A incr A($index) $amount } Variable Scoping (cont.)

  3. format - sprintf-style string generation format “Value is %.3f” [expr sqrt(10)] => Value is 3.162 Pattern matching regexp/regsub - regular subexpression matching/replacement scan - scanf-style matching string match - glob-style matching Query string length $str Search string first $str John String-list conversion split, join String Manipulation

  4. eval str - evaluate strings as commands set string “Hello, World!” set cmd [list puts stdout $string] unset string eval $cmd => Hello, World! eval concatenates several arguments into one list to evaluate uplevel ?level? cmd - do eval in another context subststr - do substitutions, but do not evaluate Eval

  5. Open/close open, close - open/close file channel socket - open TCP socket to port on host Search - cd, glob, pwd I/O - read, write, gets, puts, flush, seek Control fconfigure - blocking, eof character, etc. Status file, eof, tell, fblocked fileevent - execute script when file/channel is readable or writeable Example set f [open [lindex $argv 1] r] if {[gets $f line] >= 0} { puts stdout $line } close $f File/Channel I/O

  6. Execute other programs exec grep foo << $input | wc Specifies a process pipeline using C shell-like syntax can put processes in background exec lpr -Plw501 motd.txt & returns stdout of last subprocess exec date returns: Wed Jan 25 15:23:25 CST 1995 returns process IDs if in background exec cat motd.txt | wc & returns: 576 577 Note piping done in Tcl, not a string passed to csh Processes

  7. Environment array env contains environment variables env(EDITOR) Process pid command returns process ID Time clock command interfaces to system time get time in clocks or seconds convert to/from date/time strings after - execute command after time delay System Information

  8. Errors normally abort commands in progress, application displays error message set n 0 foreach i {1 2 3 4 5} { set n [expr {$n + i*i}] } expr command prints: syntax error in expression “$n + i*i” Errors

  9. Global variable errorInfo provides stack trace set errorInfo => syntax error in expression “$n + i*i” while executing “expr {$n + i*i}” invoked from within “set n [expr {$n + i*i] . . .” (“foreach” body line 2) . . . Errors (cont.)

  10. Can catch errors with catch command catch {expr {2 +}} msg => 1 set msg => syntax error in expression “2 +” Can generate errors error “bad argument” Global variable errorCode holds machine-readable information about errors (e.g. UNIX errno value) Errors (cont.)

  11. History like csh history mostly for interactive use Info information about internal state info exists A - does variable A exist? Command names rename - rename/delete a command Timing time - time a script for performance analysis Variable tracing trace variable foo r PrintVar calls PrintVar whenever foo is read Reflection and Debugging

  12. Loading source code into Tcl source init.tcl Autoloading source code Tcl procedures loaded on demand using search path of directories auto_mkindex - generate a file-proc autoload index Packages load file ?pkgname? ?interp? load binary package from file and initialize pkg_mkIndex - generate autoload index package command - version control Code Libraries

  13. proc Random {} { global NextRandom set NextRandom [expr $NextRandom * 1103515245 + 12345] return [expr abs($NextRandom % (0x7fffffff + 1))] } proc MakeRandomList len { for {set i 0} {$i < $len} {incr i} { lappend lst [Random] } return $lst } set NextRandom [expr {[pid]+[clock clicks]}] set rndlst [MakeRandomList 52] Example: Random Numbers

  14. #/user/walker/bin/tclsh if {$argc != 2} { error “Usage: tgrep pattern fileName” } set f [open [lindex $argv 1] r] set pat [lindex $argv 0] while {[gets $f line] >= 0} { if [regexp $pat $line] { puts $line } } catch { close $f } Example: tgrep

More Related