TCL Introduction
TCL Introduction
Introduction Simple Text Output Assigning values to variables Evaluation & Substitutions 1: Grouping arguments with "" Evaluation & Substitutions 2: Grouping arguments with {} Evaluation & Substitutions 3: Grouping arguments with [] Results of a command - Math 101 Numeric Comparisons 101 - if Textual Comparison - switch Looping 101 - While loop Looping 102 - For and incr Adding new commands to Tcl - proc Variations in proc arguments and return values Variable scope - global and upvar Tcl Data Structures 101 - The list Adding & Deleting members of a list More list commands - lsearch, lsort, lrange String Subcommands - length range String comparisons - compare match first last wordend Modifying Strings - tolower, toupper, trim, format Regular Expressions 101 More Examples Of Regular Expressions More Quoting Hell - Regular Expressions 102 Associative Arrays More On Arrays - Iterating and use in procedures File Access 101 Information about Files - file, glob Invoking Subprocesses from Tcl - exec, open Learning the existence of commands and variables ? - info State of the interpreter - info Information about procs - info Modularization - source Building reusable libraries - packages and namespaces Creating Commands - eval More command construction - format, list Substitution without evaluation - format, subst Changing Working Directory - cd, pwd Debugging & Errors - errorInfo errorCode catch error return More Debugging - trace Command line arguments and environment strings Leftovers - time, unset Channel I/O: socket, fileevent, vwait Time and Date - clock
Introduction
Welcome to the Tcl tutorial. We wrote it with the goal of helping you to learn Tcl. It is aimed at those who have some knowledge of programming, although you certainly don't have to be an expert. The tutorial is intended as a companion to the Tcl manual pages which provide a reference for all Tcl commands. It is divided into brief sections covering different aspects of the language. Depending on what system you are on, you can always look up the reference documntation for commands that you are curious about. On Unix for example, man while would bring up the man page for the while command. Each section is accompanied by relevant examples showing you how to put to use the material covered.
Additional Resources
The Tcl community is an exceedingly friendly one. It's polite to try and figure things out yourself, but if you're struggling, we're more than willing to help. Here are some good places to get help:
The comp.lang.tcl newsgroup. Accessible via a newsreader, or Google Groups. The Wiki has a great deal of useful code, examples and discussions of the finer points of Tcl usage. If you need help right away, there is often someone on the #tcl channel on irc.freenode.net who can help you out, but please don't be impatient if no one can help you instantly - if you need that level of support, consider hiring a consultant. There are several recommended books for those who wish to gain more in-depth knowledge of Tcl. Clif Flynt, the original author of this tutorial is also the author of Tcl/Tk: A Developer's Guide. Other popular books: Practical Programming in Tcl and Tk.
Credits
Thanks first and foremost to Clif Flynt for making his material available under a BSD license. The following people also contributed:
Of course, we also welcome comments and suggestions about how it could be improved or if it's great the way it is, we don't mind a bit of thanks, either!
The traditional starting place for a tutorial is the classic "Hello, World" program. Once you can print out a string, you're well on your way to using Tcl for fun and profit! The command to output a string in Tcl is the puts command. A single unit of text after the puts command will be printed to the standard output device (in this case, the lower window). The default behavior is to print a newline character ("return") appropriate for the system after printing the text. If the string has more than one word, you must enclose the string in double quotes or braces ({}). A set of words enclosed in quotes or braces is treated as a single unit, while words separated by whitespace are treated as multiple arguments to the command. Quotes and braces can both be used to group several words into a single unit. However, they actually behave differently. In the you'll start to learn some of the differences between their behaviors. Note that in Tcl, single quotes are not significant, as they are in other programming languages such as C, Perl and Python. Many commands in Tcl (including puts) can accept multiple arguments. If a string is not enclosed in quotes or braces, the Tcl interpreter will consider each word in the string as a separate argument, and pass each individually to the puts command. The puts command will try to evaluate the words as optional arguments. This will probably result in an error. A command in Tcl is a list of words terminated by a newline or semicolon. Tcl comments are a # at the beginning of the line, or after the command is closed with a ; semicolon.
Example
puts "Hello, World - In quotes" command. puts {Hello, World - In Braces} semicolon! ;# This is a comment after the # *Error* - there is no
puts "This is line 1"; puts "this is line 2" puts "Hello, World; - With a semicolon inside the quotes"
it places the second argument ("Cauliflower") in the memory space referenced by the first argument (fruit). Set always returns the contents of the variable named in the first argument. Thus, when set is called with two arguments, it places the second argument in the memory space referenced by the first argument and then returns the second argument. In the above example, for instance, it would return "Cauliflower", without the quotes. The first argument to a set command can be either a single word, like fruit or pi , or it can be a member of an array. Arrays will be discussed in greater detail later, for the time being just remember that many data can be collected under a single variable name, and an individual datum can be accessed by its within that array. ing into an array in Tcl is handled by putting the within parentheses after the name of the variable.
Set can also be invoked
with only one argument. When called with just one argument, it will return the contents of that argument. Here's a summary of the set command.
set varName ?value?
If value is specified, then the contents of the variable varName are set equal to value. If varName consists only of alphanumeric characters, and no parenthese, it is a scalar variable. If varName has the form varName() , it is a member of an associative array.
If you look at the example code, you'll notice that in the set command the first argument is typed with only its name, but in the puts statement the argument is preceeded with a $. The dollar sign tells Tcl to use the value of the variable - in this case X or Y. Tcl passes data to subroutines either by name or by value. Commands that don't change the contents of a variable usually have their arguments passed by value. Commands that do change the value of the data must have the data passed by name.
Example
set X "This is a string" set Y 1.24 puts $X puts $Y puts "..............................." set label "The value in Y is: " puts "$label $Y"
the contents of the proper variable are substituted for $varName, and then the command is executed. Assuming we have set varName to "Hello World", the sequence would look like this: puts $varName puts "Hello World", which is then executed and prints out Hello World. During the substitution phase, several types of substitutions occur. A command within square brackets ([]) is replaced with the result of the execution of that command. (This will be explained more fully in the lesson "Results of a Command Math 101.") Words within double quotes or braces are grouped into a single argument. However, double quotes and braces cause different behavior during the substitution phase. In this lesson, we will concentrate on the behavior of double quotes during the substitution phase. Grouping words within double quotes allows substitutions to occur within the quotations - or, in fancier terms, "interpolation". The substituted group is then evaluated as a single argument. Thus, in the command:
puts "The current stock value is $varName"
the current contents of varName are substituted for $varName, and then the entire string is printed to the output device, just like the example above. In general, the backslash (\) disables substitution for the single character immediately following the backslash. Any character immediately following the backslash will stand without substitution. However, there are specific "Backslash Sequence" strings which are replaced by specific values during the substitution phase. The following backslash strings will be substituted as shown below.
String Output Hex Value \a Audible Bell 0x07 \b Backspace 0x08 \f Form Feed (clear screen) 0x0c \n New Line 0x0a \r Carriage Return 0x0d \t Tab 0x09 \v Vertical Tab 0x0b \0dd Octal Value d is a number from 1-7 \xhh Hex Value h is a hex digit 0-9,A-F,a-f The final exception is the backslash at the end of a line of text. This causes the interpreter to ignore the newline, and treat the text as a single line of text. The interpreter will insert a blank space at the location of the ending backslash.
Example
set Z "Albany" set Z_LABEL "The Capitol of New York is: " puts "$Z_LABEL $Z" puts "$Z_LABEL \$Z" ;# Prints the value of Z ;# Prints a literal $Z instead of the value
of Z
puts "\nBen Franklin is on the $100.00 bill" set a 100.00 puts "Washington is not on the $a bill" you want puts "Lincoln is not on the $$a bill" puts "Hamilton is not on the \$a bill" you want puts "Ben Franklin is on the \$$a bill" puts puts puts puts on a ;# This is not what ;# This is OK ;# This is not what ;# But, this is OK
"\n................. examples of escape strings" "Tab\tTab\tTab" "This string prints out \non two lines" "This string comes out\ single line"
Example
set Z "Albany" set Z_LABEL "The Capitol of New York is: " \{" puts "\n................. examples of differences between puts "$Z_LABEL $Z" puts {$Z_LABEL $Z} \" and
puts "\n....... examples of differences in nesting \{ and \" " puts "$Z_LABEL {$Z}" puts {Who said, "What this country needs is a good $0.05 cigar!"?} puts "\n................. examples of escape strings" puts {There are no substitutions done within braces \n \r \x0a \f puts {But, the escaped newline at the end of a\ string is still evaluated as a space}
\v}
The parser scans the entire command, and sees that there is a command substitution to perform: readsensor [selectsensor] , which is sent to the interpreter for evaluation. The parser once again finds a command to be evaluated and substituted,
selectsensor The fictitious selectsensor command is
sensor to read. At this point, readsensor has a sensor to read, and the readsensor command is evaluated. Finally, the value of readsensor is passed on back to the puts command, which prints the output to the screen.
A square bracket that is escaped with a \ is considered as a literal square bracket. A square bracket within braces is not modified during the substitution phase.
Example
set x "abc" puts "A simple substitution: $x\n" set y [set x "def"] puts "Remember that set returns the new value of the variable: X: $x Y: $y\n" set z {[set x "This is a string within quotes within braces"]} puts "Note the curly braces: $z\n"
set a "[set x {This is a string within braces within quotes}]" puts "See how the set is executed: $a" puts "\$x is: $x\n" set b "\[set y {This is a string within braces within quotes}]" puts "Note the \\ escapes the bracket:\n \$b is: $b" puts "\$y is: $y"
all of its arguments ("2 + 2" for example) and evaluates the result as a Tcl "expression" (rather than a normal command), and returns the value. The operators permitted in Tcl expressions include all the standard math functions, logical operators, bitwise operators, as well as math functions like rand(), sqrt(), cosh() and so on. Expressions almost always yield numeric results (integer or floating-point values). Performance tip: enclosing the arguments to expr in curly braces will result in faster code. So do expr {$i * 10} instead of simply expr $i * 10
OPERANDS
A Tcl expression consists of a combination of operands, operators, and parentheses. White space may be used between operands, operators and parentheses; it is ignored by the expression processor. Where possible, operands are interpreted as integer values. Integer values may be specified in decimal (the normal case), in octal (if the first character of the operand is 0), or in hexadecimal (if the first two characters of the operand are 0x). Note that the octal and hexadecimal conversion takes place differently in the expr command than in the Tcl substitution phase. In the substitution phase, a \x32 would be converted to an ascii "2", while expr would covert 0x32 to a decimal 50. If an operand does not have one of the integer formats given above, then it is treated as a floating-point number, if that is possible. Floating-point numbers may be specified in any of the ways accepted by an ANSI-compliant C compiler. For example, all of the following are valid floating-point numbers: 2.1, 3., 6e4, 7.91e+16. If no numeric interpretation is possible, then an operand is left as a string (and only a limited set of operators may be applied to it). Operands may be specified in any of the following ways:
As an numeric value, either integer or floating- point. As a Tcl variable, using standard $ notation. The variable's value will be used as the operand.
OPERATORS
The valid operators are listed below, grouped in decreasing order of precedence: -+~!
*/%
Unary minus, unary plus, bit-wise NOT, logical NOT. None of these operators may be applied to string operands, and bit-wise NOT may be applied only to integers. Multiply, divide, remainder. None of these operators may be applied to string operands, and remainder may be applied only to integers. The remainder will always have the same sign as the divisor and an absolute value smaller than the divisor. Add and subtract. Valid for any numeric operands. Left and right shift. Valid for integer operands only. Bit-wise AND. Valid for integer operands only. Bit-wise exclusive OR. Valid for integer operands only. Bit-wise OR. Valid for integer operands only. Logical AND. Produces a 1 result if both operands are non-zero, 0 otherwise. Valid for numeric operands only (integers or floating-point). Logical OR. Produces a 0 result if both operands are zero, 1 otherwise. Valid for numeric operands only (integers or floating-point). If-then-else, as in C. If x evaluates to non-zero, then the result is the value of y. Otherwise the result is the value of z. The x operand must have a numeric value.
MATH FUNCTIONS
Tcl supports the following mathematical functions in expressions:
acos asin atan atan2 ceil cos cosh exp floor fmod hypot log log10 pow sin sinh sqrt tan tanh
TYPE CONVERSIONS
Tcl supports the following functions to convert from one representation of a number to another:
abs double int round
Example
set set set set X 100; Y 256; Z [expr "$Y + $X"] Z_LABEL "$Y plus $X is "
puts "$Z_LABEL $Z" puts "The square root of $Y is [expr sqrt($Y)]\n" puts "Because of the precedence rules \"5 + -3 * 4\" is: [expr -3 * 4 + 5]" puts "Because of the parentheses \"(5 + -3) * 4\" is: [expr (5 + -3) * 4]" puts \" and \{" puts puts puts "\n................. more examples of differences between {$Z_LABEL [expr $Y + $X]} "$Z_LABEL {[expr $Y + $X]}" "The command to add two numbers is: \[expr \$a + \$b]"
The words then and else are optional, although generally then is left out and else is used. The test expression following if should return one of: False True all others a numeric value 0 no yes yes/no false true true/false If the test expression returns a string "yes"/"no" or "true"/"false", the case of the return is not checked. True/FALSE or YeS/nO are legitimate returns. If the test expression evaluates to True, then body1 will be executed. If the test expression evaluates to False, then the word after body1 will be examined. If the next word is elseif, then the next test expression will be tested as a condition. If the next word is else then the final body will be evaluated as a command. The test expression following the word if is evaluated in the same manner as in the expr command. Hex strings 0xXX will be converted to their numeric equivalent before evaluation. The test expression following if may be enclosed within quotes, or braces. If it is enclosed within braces, it will be evaluated within the if command, and if enclosed within quotes it will be evaluated during the substitution phase, and then another round of substitutions will be done within the if command.
Example
set x 1 if {$x == 2} {puts "$x is 2"} else {puts "$x is not 2"} if {$x != 1} { puts "$x is != 1" } else {
if $x==1 {puts "GOT 1"} set y x if "$$y != 1" { puts "$$y is != 1" } else { puts "$$y is 1" }
String is
the string that you wish to test, and pattern1, pattern2, etc are the patterns that the string will be compared to. If string matches a pattern, then the code within the body associated with that pattern will be executed. The return value of the body will be returned as the return value of the switch statement. Only one pattern will be matched. If the last pattern argument is the string default, that pattern will match any string. This guarantees that some set of code will be executed no matter what the contents of string are. If there is no default argument, and none of the patterns match string, then the switch command will return an empty string. If you use the brace version of this command, there will be no substitutions done on the patterns. The body of the command, however, will be parsed and evaluated just like any other command, so there will be a pass of substitutions done on that, just as will be done in the first syntax. The advantage of the second form is that you can write multiple line commands more readably with the brackets. Note that you can use braces to group the body argument when using the switch or if commands. This is because these commands pass their body argument to the Tcl interpreter for evaluation. This evaluation includes a pass of substitutions just as it does for code not within a command body argument.
Example
set x "ONE" set y 1 set z "ONE" # This is probably the easiest and cleanest form of the command # to remember: switch $x { "$z" { set y1 [expr $y+1] puts "MATCH \$z. $y + $z is $y1" } ONE { set y1 [expr $y+1] puts "MATCH ONE. $y + one is $y1" } TWO { set y1 [expr $y+2] puts "MATCH TWO. $y + two is $y1" } THREE { set y1 [expr $y+3] puts "MATCH THREE. $y + three is $y1" } default { puts "$x is NOT A MATCH" } } switch $x "$z" { set y1 [expr $y+1] puts "MATCH \$z. $y + $z is $y1" } ONE { set y1 [expr $y+1] puts "MATCH ONE. $y + one is $y1" } TWO { set y1 [expr $y+2] puts "MATCH TWO. $y + two is $y1" } THREE { set y1 [expr $y+3] puts "MATCH THREE. $y + three is $y1" } default { puts "$x does not match any of these choices" } switch $x "ONE" "puts ONE=1" "TWO" "puts TWO=2" "default" "puts NO_MATCH" switch $x \ "ONE" "puts ONE=1" \ "TWO" "puts TWO=2" \ "default" "puts NO_MATCH";
The while command evaluates test as an expression. If test is true, the code in body is executed. After the code in body has been executed, testis evaluated again. A continue statement within body will stop the execution of the code and the test will be re-evaluated. A break within body will break out of the while loop, and execution will continue with the next line of code after body In Tcl everything is a command, and everything goes through the same substitution phase. For this reason, the test must be placed within braces. If test is placed within quotes, the substitution phase will replace any variables with their current value, and will pass that test to the while command to evaluate, and since the test has only numbers, it will always evaluate the same, quite probably leading to an endless loop! Look at the two loops in the example. If it weren't for the break command in the second loop, it would loop forever.
Example
set x 1 # This is a normal way to write a Tcl while loop. while {$x < 5} { puts "x is $x" set x [expr $x + 1] } puts "exited first loop with X equal to $x\n" # The next example shows the difference between ".." and {...} # How many times does the following loop run? Why does it not # print on each pass? set x 0 while "$x < 5" { set x [expr $x + 1] if {$x > 7} break if "$x > 3" continue
During evaluation of the for command, the start code is evaluated once, before any other arguments are evaluated. After the start code has been evaluated, the test is evaluated. If the test evaluates to true, then the body is evaluated, and finally, the next argument is evaluated. After evaluating the next argument, the interpreter loops back to the test, and repeats the process. If the test evaluates as false, then the loop will exit immediately.
Start is
the initialization portion of the command. It is usually used to initialize the iteration variable, but can contain any code that you wish to execute before the loop starts. The test argument is evaluated as an expression, just as with the expr while and if commands.
Next is Body is
commonly an incrementing command, but may contain any command which the Tcl interpreter can evaluate. the body of code to execute.
Since you commonly do not want the Tcl interpreter's substitution phase to change variables to their current values before passing control to the for command, it is common to group the arguments with curly braces. When braces are used for grouping, the newline is not treated as the end of a Tcl command. This makes it simpler to write multiple line commands. However, the opening brace must be on the line with the for command, or the Tcl interpreter will treat the close of the next brace as the end of the command, and you will get an error. This is different than other languages like C or Perl, where it doesn't matter where you place your braces. Within the body code, the commands break and continue may be used just as they are used with the while command. When a break is encountered, the loop exits immediately. When a continue is encountered, evaluation of the body ceases, and the test is re-evaluated. Because incrementing the iteration variable is so common, Tcl has a special command for this:
incr varName ?increment?
This command adds the value in the second argument to the variable named in the first argument. If no value is given for the second argument, it defaults to 1.
Example
for {set i 0} {$i < 10} {incr i} { puts "I inside first loop: $i" } for {set i 3} {$i < 2} {incr i} { puts "I inside second loop: $i" } puts "Start" set i 0 while {$i < 10} { puts "I inside first loop: $i" incr i puts "I after incr: $i" } set i 0 incr i # This is equivalent to: set i [expr $i + 1]
When proc is evaluated, it creates a new command with name name that takes arguments args. When the procedure name is called, it then runs the code contained in body.
Args is
a list of arguments which will be passed to name. When name is invoked, local variables with these names will be created, and the values to be passed to name will be copied to the local variables. The value that the body of a proc returns can be defined with the return command. The return command will return its argument to the calling program. If there is no return, then body will return to the caller when the last of its commands has been executed. The return value of the last command becomes the return value of the procedure.
Example
proc sum {arg1 arg2} { set x [expr {$arg1 + $arg2}]; return $x } puts " The sum of 2 + 3 is: [sum 2 3]\n\n" proc for {a b c} { puts "The for command has been replaced by a puts"; puts "The arguments were: $a\n$b\n$c\n" } for {set i 1} {$i < 10} {incr i}
Since there are default arguments for the b and c variables, you could call the procedure one of three ways: justdoit 10, which would set a to 10, and leave b set to its default 1, and c at -1. justdoit 10 20 would likewise set b to 20, and leave C to its default. A proc will accept a variable number of arguments if the last declared argument is the word args. If the last argument to a proc argument list is args, then any arguments that aren't already assigned to previous variables will be assigned to args. The example procedure below is defined with three arguments. At least one argument *must* be present when example is called. The second argument can be left out, and in that case it will default to an empty string. By declaring args as the last argument, example can take a variable number of arguments. Note that if there is a variable other than args after a variable with a default, then the default will never be used. For example, if you declare a proc such as: proc function { a {b 1} c} {...}, you will always have to call it with 3 arguments. Tcl assigns values to a proc's variables in the order that they are listed in the command. If you provide 2 arguments when you call function they will be assigned to a and b, and Tcl will generate an error because c is undefined. You can, however, declare other arguments that may not have values as coming after an argument with a default value. For example, this is valid:
proc example {required {default1 a} {default2 b} args} {...}
In this case, example requires one argument, which will be assigned to the variable required. If there are two arguments, the second arg will be assigned to default1. If there are 3 arguments, the first will be assigned to required, the second to default1, and the third to default2. If example is called with more than 3 arguments, all the arguments after the third will be assigned to args.
Example
proc example {first {second if {$second == ""} { puts "There is only return 1 } else { if {$args == ""} { puts "There are return 2 } else { puts "There are $args" return "many" } } } set set set set count1 count2 count3 count4 [example [example [example [example ""} args} { one argument and it is: $first"
two arguments - $first and $second" many arguments - $first and $second and
ONE] ONE TWO] ONE TWO THREE ] ONE TWO THREE FOUR]
puts "The example was called with $count1, $count2, $count3, and $count4 Arguments"
reference to otherVar1, and myVar2 to become a reference to otherVar2, etc. The otherVar variable is declared to be at level relative to the current procedure. By default level is 1, the next level up. If a number is used for the level, then level references that many levels up the stack from the current level. If the level number is preceeded by a # symbol, then it references that many levels down from the global scope. If level is #0, then the reference is to a variable at the global level. My personal opinion is that using upvar with anything except #0 or 1 is asking for trouble. The use of global is hard to avoid, but you should avoid having too many global variables. If you start needing lots of globals, you may want to look at your design again. Note that since there is only one global space it is surprisingly easy to have name conflicts if you are importing other peoples code and aren't careful. It is recommended that you start global variables with an identifiable prefix to help avoid unexpected conflicts.
Example
proc SetPositive {variable value } { upvar $variable myvar if {$value < 0} { set myvar [expr -$value] } else { set myvar $value } return $myvar } SetPositive x 5 SetPositive y -5 puts "X : $x Y: $y\n" ;# tie the calling value to ;# Tie variable x two levels up ;# Output the values, just to ;# Set z, the passed variable to ;# Set x, two layers up to 2;
proc two {y} { upvar 1 $y z variable z upvar 2 x a to a puts "two: Z: $z A: $a" confirm set z 1 1; set a 2 } proc one {y} { upvar $y z variable z puts "one: Z: $z" is 5 two z change the value } one y after the call. puts "\nX: $x
;# This ties the calling value to ;# Output that value, to check it ;# call proc two, which will
returns the 'th item from the list. The first item is 0. llength list Returns the number of elements in a list. The items in list can be iterated through using the foreach command. foreach varname list body execute the body code one time for each list item in list. On each pass, varname will contain the value of the next list item.
Foreach will
Example
set x "a b c" puts "Item 2 of the list {$x} is: [l $x 2]\n" set y [split 7/4/1776 "/"] puts "We celebrate on the [l $y 1]'th day of the [l $y 0]'th month\n" set z [list puts "arg 2 is $y" ] puts "A command resembles: $z\n" set i 0; foreach j $x { puts "$j is item number $i in list x" incr i; }
Example
set b [list a b {c d e} {f {g h}}] puts "Treated as a list: $b\n" set b [split "a b {c d e} {f {g h}}"] puts "Transformed by split: $b\n" set a [concat a b {c d e} {f {g h}}] puts "Concated: $a\n" lappend a {ij K lm} single element puts "After lappending: $a\n" set b [linsert $a 3 "1 element puts "After linsert at set b [lreplace $b 3 5 puts "After lreplacing $b\n" 2 3"] ;# Note: {ij K lm} is a
Example
set list [list {Washington 1789} {Adams 1797} {Jefferson 1801} \ {Madison 1809} {Monroe 1817} {Adams 1825} ] set x [lsearch $list Washington*] set y [lsearch $list Madison*] incr x; incr y -1 inclusive
set subsetlist [lrange $list $x $y] puts "The following presidents served between Washington and Madison" foreach item $subsetlist { puts "Starting in [l $item 1]: President [l $item 0] " } set x [lsearch $list Madison*] set srtlist [lsort $list] set y [lsearch $srtlist Madison*] puts "\n$x Presidents came before Madison chronologically" puts "$y Presidents came before Madison alphabetically"
Example
set string "this is my test string" puts "There are [string length $string] characters in \"$string\"" puts "[string $string 1] is the second character in \"$string\""
puts "\"[string range $string 5 10]\" are characters between the 5'th and 10'th"
-1 ..... If string1 is less than string2 0 ........ If string1 is equal to string2 1 ........ If string1 is greater than string2
These comparisons are done lexicographically, not numerically. string first string1 string2 Returns the of the character in string1 that starts the first match to string2, or -1 if there is no match to string2 in string1 string last string1 string2 Returns the of the character in string1 that starts the last match to string2, or -1 if there is no match to string2 in string1 string wordend string Returns the of the character just after the last one in the word which contains the 'th character of string. A word is any contiguous set of letters, numbers or underscore characters, or a single other character. string wordstart string Returns the of the character just before the first one in the word which contains the 'th character of string. A word is any contiguous set of letters, numbers or underscore characters, or a single other character. string match pattern string Returns 1 if the pattern matches string. Pattern is a glob style pattern. Globbing is the wildcarding technique that the shell uses. globbing wildcards are: * Matches any quantity of any character ? Matches one occurrence of any character \X The backslash escapes a special character in globbing just the way it does in Tcl substitutions. Using the backslash lets you use glob to match a * or ?. [...] Matches one occurrence of any character within the brackets. A range of characters can be matched by using a range between the brackets. For example, [a-z] will match any lower case letter.
Example
set fullpath "/usr/home/clif/TCL_STUFF/TclTutor/Lsn.17" set relativepath "CVS/Entries" set directorypath "/usr/bin/" set paths [list $fullpath $relativepath $directorypath] foreach path $paths { set first [string first "/" $path] set last [string last "/" $path] # Report whether path is absolute or relative if {$first != 0} { puts "$path is a relative path" } else { puts "$path is an absolute path" } word. and # If "/" is not the last character in $path, report the last # else, remove the last "/", and find the next to last "/", # report the last word.
incr last if {$last != [string length $path]} { set name [string range $path $last end] puts "The file referenced in $path is $name" } else { incr last -2; set tmp [string range $path 0 $last] set last [string last "/" $tmp] incr last; set name [string range $tmp $last end] puts "The final directory in $path is $name" } system. # CVS is a directory created by the CVS source code control # if {[string match "*CVS*" $path]} { puts "$path is part of the source code control tree" } # Compare to "a" to determine whether the first char is upper or lower case set comparison [string compare $name "a"] if {$comparison >= 0} { puts "$name starts with a lowercase letter\n" } else { puts "$name starts with an uppercase letter\n" } }
s... Data is a string d... Data is a decimal integer x... Data is a hexadecimal integer o... Data is an octal integer f... Data is a floating point number
-... Left justify the data in this field +... Right justify the data in this field
The justification value may be followed by a number giving the minimum number of spaces to use for the data.
Example
set upper "THIS IS A STRING IN UPPER CASE LETTERS" set lower "this is a string in lower case letters" set trailer "This string has trailing dots ...."
set leader "....This string has leading dots" set both "((this string is nested in parens )))" puts puts puts puts puts puts puts puts puts puts set set set set set puts puts puts puts puts puts "tolower converts this: $upper" " to this: [string tolower $upper]\n" "toupper converts this: $lower" " to this: [string toupper $lower]\n" "trimright converts this: $trailer" " to this: [string trimright $trailer .]\n" "trimleft converts this: $leader" " to this: [string trimleft $leader .]\n" "trim converts this: $both" " to this: [string trim $both "()"]\n" labels price1 price2 price3 price4 [format [format [format [format [format "%-20s "%-20s "%-20s "%-20s "%-20s %+10s " "Item" "Cost"] %10d Cents Each" "Tomatoes" "30"] %10d Cents Each" "Peppers" "20"] %10d Cents Each" "Onions" "10"] %10.2f per Lb." "Steak" "3.59997"]
Regular expressions can be expressed in just a few rules. ^ Matches the beginning of a string $ Matches the end of a string . Matches any single character * Matches any count (0-n) of the previous character + Matches any count, but at least 1 of the previous character [...] Matches any character of a set of characters [^...] Matches any character *NOT* a member of the set of characters following the ^. (...) Groups a set of characters into a subSpec.
Regular expressions are similar to the globbing that was discussed in lessons 16 and 18. The main difference is in the way that sets of matched characters are handled. In globbing the only way to select sets of unknown text is the * symbol. This matches to any quantity of any character. In regular expression parsing, the * symbol matches zero or more occurrences of the character immediately proceeding the *. For example a* would match a, aaaaa, or a blank string. If the character directly before the * is a set of characters within square brackets, then the * will match any quantity of all of these characters. For example, [a-c]* would match aa, abc, aabcabc, or again, an empty string.
The + symbol behaves roughly the same as the *, except that it requires at least one character to match. For example, [a-c]+ would match a, abc, or aabcabc, but not an empty string. Regular expression parsing is more powerful than globbing. With globbing you can use square brackets to enclose a set of characters any of which will be a match. Regular expression parsing also includes a method of selecting any character not in a set. If the first character after the [ is a caret (^), then the regular expression parser will match any character not in the set of characters between the square brackets. A caret can be included in the set of characters to match (or not) by placing it in any position other than the first. The regexp command is similar to the string match command in that it matches an exp against a string. It is different in that it can match a portion of a string, instead of the entire string, and will place the characters matched into the matchVar variable. If a match is found to the portion of a regular expression enclosed within parentheses, regexp will copy the subset of matching characters is to the subSpec argument. This can be used to parse simple strings.
Regsub will copy the contents of the string to a new variable, substituting the characters that match exp with the characters in subSpec. If subSpec contains a & or \0, then those characters will be replaced by the characters that matched exp. If the number following a
backslash is 1-9, then that backslash sequence will be replaced by the appropriate portion of exp that is enclosed within parentheses. Note that the exp argument to regexp or regsub is processed by the Tcl substitution pass. Therefore quite often the expression is enclosed in braces to prevent any special processing by Tcl.
Example
set sample "Where there is a will, There is a way." # # Match the first substring with lowercase letters only # set result [regexp {[a-z]+} $sample match] puts "Result: $result match: $match" # # Match the first two words, the first one allows uppercase set result [regexp {([A-Za-z]+) +([a-z]+)} $sample match sub1 sub2 ] puts "Result: $result Match: $match 1: $sub1 2: $sub2" # Replace a word regsub "way" $sample "lawsuit" sample2 puts "New: $sample2" # Use the -all option to count the number of "words" puts "Number of words: [regexp -all {[^ ]} $sample]"
Invalid numbers (that is, strings we do not want to recognise as numbers but superficially look like them): Questionable numbers are:
-, +., 0.0.1, 0..2, ++1 +0000 and 0001
We will accept them - because they normally are accepted and because excluding them makes our pattern more complicated. A pattern is beginning to emerge:
A number can start with a sign (- or +) or with a digit. This can be captured with the expression [-+]?, which matches a single "-", a single "+" or nothing. A number can have zero or more digits in front of a single period (.) and it can have zero or more digits following the period. Perhaps: [0-9]*\.[0-9]* will do ... A number may not contain a period at all. So, revise the previous expression to:
[0-9]*\.?[0-9]*
At this point we can do three things: 1. Try the expression with a bunch of examples like the ones above and see if the proper ones match and the others do not. 2. Try to make it look nicer, before we start off testing it. For instance the class of characters "[0-9]" is so common that it has a shortcut, "\d". So, we could settle for:
3. [-+]?\d*\.?\d*
[-+]?[0-9]*\.?[0-9]*
instead. Or we could decide that we want to capture the digits before and after the period for special processing:
[-+]?([0-9])*\.?([0-9]*)
4. Or, and that may be a good strategy in general!, we can carefully examine the pattern before we start actually using it. You see, there is a problem with the above pattern: all the parts are optional, that is, each part can match a null string - no sign, no digits before the period, no period, no digits after the period. In other words: Our pattern can match an empty string! Our questionable numbers, like "+000" will be perfectly acceptable and we (grudgingly) agree. But more surprisingly, the strings "--1" and "A1B2" will be accepted too! Why? Because the pattern can start anywhere in the string, so it would match the substrings "-1" and "1" respectively! We need to reconsider our pattern - it is too simple, too permissive:
The character before a minus or a plus, if there is any, can not be another digit, a period or a minus or plus. Let us make it a space or a tab or the beginning of the string: ^|[ \t] This may look a bit strange, but what it says is: either the beginning of the string (^ outside the square brackets) or (the vertical bar) a space or tab (remember: the string "\t" represents the tab character).
Any sequence of digits before the period (if there is one) is allowed: [0-9]+\.? There may be zero digits in front of the period, but then there must be at least one digit behind it: \.[0-9]+ And of course digits in front and behind the period: [0-9]+\.[0-9]+ The character after the string (if any) can not be a "+","-" or "." as that would get us into the unacceptable number-like strings: $|[^+-.] (The dollar sign signifies the end of the string)
Before trying to write down the complete regular expression, let us see what different forms we have:
No period: [-+]?[0-9]+ A period without digits before it: [-+]?\.[0-9]+ Digits before a period, and possibly digits after it: [-+]?[0-9]+\.[0-9]*
Or:
The parentheses are needed to distinguish the alternatives introduced by the vertical bar and to capture the substring we want to have. Each set of parentheses also defines a substring and this can be put into a separate variable:
regexp {.....} $line whole char_before number nosign char_after # # Or simply only the recognised number (x's as placeholders, the # last can be left out # regexp {.....} $line x x number
(^|[ \t])([-+]?(\d+|\.\d+|\d+\.\d*))($|[^+-.])
Tip: To identify these substrings: just count the opening parentheses from left to right. If we put it to the test:
set pattern {(^|[ \t])([-+]?(\d+|\.\d+|\d+\.\d*))($|[^+-.])} set examples {"1.0" " .02" " +0." "1" "+1" " -0.0120" "+0000" " - " "+." "0001" "0..2" "++1" "A1.0B" "A1"} foreach e $examples { if { [regexp $pattern $e whole \ char_before number digits_before_period] } { puts ">>$e<<: $number ($whole)" } else { puts ">>$e<<: Does not contain a valid number" } } }
>>1.0<<: 1.0 (1.0) >> .02<<: .02 ( .02) >> +0.<<: +0. ( +0.) >>1<<: 1 (1) >>+1<<: +1 (+1) >> -0.0120<<: -0.0120 ( -0.0120) >>+0000<<: +0000 (+0000) >> - <<: Does not contain a valid number >>+.<<: Does not contain a valid number >>0001<<: 0001 (0001) >>0..2<<: Does not contain a valid number >>++1<<: Does not contain a valid number >>A1.0B<<: Does not contain a valid number >>A1<<: Does not contain a valid number
So our pattern correctly accepts the strings we intended to be recognised as numbers and rejects the others. Let us turn to some other patterns now:
Text enclosed in a string: This is "quoted text". If we know the enclosing character in advance (double quotes, " in this case), then "([^"])*" will capture the text inside the double quotes. Suppose we do not know the enclosing character (it can be " or '). Then:
regexp {(["'])[^"']*\1} $string enclosed_string
You can use this technique to see if a word occurs twice in the same line of text:
set string "Again and again and again ..." if { [regexp {(\y\w+\y).+\1} $string => word] } { puts "The word $word occurs at least twice" }
(The pattern \y matches the beginning or the end of a word and \w+ indicates we want at least one character).
Suppose you need to check the parentheses in some mathematical expression: (1+a)/(1-b*x) for instance. A simple check is counting the open and close parentheses:
} {
# # Use the return value of [regexp] to count the number of # parentheses ... # if { [regexp -all {(} $string] != [regexp -all {)} $string] } puts "Parentheses unbalanced!"
Of course, this is just a rough check. A better one is to see if at any point while scanning the string there are more close parentheses than open parentheses. We can easily extract the parentheses and put them in a list (the -inline option does that):
set set set set parens [regexp -inline -all {[()]} $string] balance 0 change("(") 1 ;# This technique saves an if-block :) change(")") -1
foreach p $parens { incr balance $change($p) if { $balance < 0 } { puts "Parentheses unbalanced!" }
Finally: Regular expressions are very powerful, but they have certain theoretical limitations. One of these limitations is that they are not suitable for parsing arbitrarily nested text. You can experiment with regular expressions using the VisualRegexp or Visual REGEXP applications. More on the theoretical background and practical use of regular expressions (there is lots to cover!) can be found in the book Mastering Regular Expressions by J. Friedl.
The regular expression (exp) in the two regular expression parsing commands is evaluated by the Tcl parser during the Tcl substitution phase. This can provide a great deal of power, and also requires a great deal of care. These examples show some of the trickier aspects of regular expression evaluation. The fields in each example are discussed in painful detail in the most verbose level. The points to remember as you read the examples are:
A left square bracket ([) has meaning to the substitution phase, and to the regular expression parser. A set of parentheses, a plus sign, and a star have meaning to the regular expression parser, but not the Tcl substitution phase. A backslash sequence (\n, \t, etc) has meaning to the Tcl substitution phase, but not to the regular expression parser. A backslash escaped character (\[) has no special meaning to either the Tcl substitution phase or the regular expression parser.
The phase at which a character has meaning affects how many escapes are necessary to match the character you wish to match. An escape can be either enclosing the phrase in braces, or placing a backslash before the escaped character. To pass a left bracket to the regular expression parser to evaluate as a range of characters takes 1 escape. To have the regular expression parser match a literal left bracket takes 2 escapes (one to escape the bracket in the Tcl substitution phase, and one to escape the bracket in the regular expression parsing.). If you have the string placed within quotes, then a backslash that you wish passed to the regular expression parser must also be escaped with a backslash. Note: You can copy the code and run it in tclsh or wish to see the effects.
Example
# # Examine an overview of UNIX/Linux disks # set list1 [list \ {/dev/wd0a 17086 10958 5272 {/dev/wd0f 179824 127798 48428 {/dev/wd0h 1249244 967818 218962 {/dev/wd0g 98190 32836 60444
foreach line $list1 { regexp {[^ ]* *([0-9]+)[^/]*(/[a-z]*)} $line match size mounted; puts "$mounted is $size blocks" } # # Extracting a hexadecimal value ... # set line {Interrupt Vector? [32(0x20)]} regexp "\[^\t]+\t\\\[\[0-9]+\\(0x(\[0-9a-fA-F]+)\\)]" $line match hexval puts "Hex Default is: 0x$hexval" # # Matching the special characters as if they were ordinary # set str2 "abc^def" regexp "\[^a-f]*def" $str2 match puts "using \[^a-f] the match is: $match" regexp "\[a-f^]*def" $str2 match puts "using \[a-f^] the match is: $match" regsub {\^} $str2 " is followed by: " str3 puts "$str2 with the ^ substituted is: \"$str3\"" regsub "(\[a-f]+)\\^(\[a-f]+)" $str2 "\\2 follows \\1" str3 puts "$str2 is converted to \"$str3\""
Associative Arrays.
Languages like C, BASIC, FORTRAN and Java support arrays in which the value is an integer. Tcl, like most scripting languages (Perl, Python, PHP, etc...) supports associative arrays (also known as "hash tables") in which the value is a string. The syntax for an associative array is to put the within parentheses:
set name(first) "Mary" set name(last) "Poppins" puts "Full name: $name(first) $name(last)"
There are several array commands aside from simply accessing and creating arrays which will be discussed in this and the .
array exists arrayName Returns 1 if arrayName is an array variable. Returns 0 if arrayName is a scalar variable, proc, or does not exist. array names arrayName ?pattern Returns a list of the indices for the associative array arrayName. If pattern is supplied, only those indices that match pattern are returned. The match is done using the globbing technique from string match. array size arrayName Returns the number of elements in array arrayName. array get arrayName Returns a list in which each odd member of the list (1, 3, 5, etc) is an into the associative array. The list element following a name is the value of that array member. array set arrayName dataList Converts a list into an associative array. DataList is a list in the format of that returned by array get. Each odd member of the list (1, 3, 5, etc) is an into the associative array, and the list element following that is the value of that array member.
When an associative array name is given as the argument to the global command, all the elements of the associative array become available to that proc. For this reason, Brent Welch recommends (in Practical Programming in Tcl and Tk) using an associative array for the state structure in a package. This method makes it simpler to share data between many procs that are working together, and doesn't pollute the global namespace as badly as using separate globals for all shared data items. Another common use for arrays is to store tables of data. In the example below we use an array to store a simple database of names.
Example
proc addname {first last} { global name # Create a new ID (stored in the name array too for easy access) incr name(ID) set id $name(ID) set name($id,first) $first set name($id,last) $last } ;# The is simply a string! ;# So we can use both fixed and ;# varying parts
# # Initialise the array and add a few names # global name set name(ID) 0 addname addname addname addname Mary Poppins Uriah Heep Rene Descartes Leonardo "da Vinci"
# # Check the contents of our database # The parray command is a quick way to # print it # parray name # # Some array commands # array set array1 [list {123} {Abigail Aardvark} \ {234} {Bob Baboon} \ {345} {Cathy Coyote} \ {456} {Daniel Dog} ] puts "Array1 has [array size array1] entries\n" puts "Array1 has the following entries: \n [array names array1] \n" puts "ID Number 123 belongs to $array1(123)\n" if {[array exist array1]} { puts "array1 is an array" } else { puts "array1 is not an array" } if {[array exist array2]} { puts "array2 is an array" } else { puts "array2 is not an array" }
Note, however, that the elements will not be returned in any predictable order: this has to do with the underlying "hash table". If you want a particular ordering (alphabetical for instance), use code like:
foreach name [lsort [array names mydata]] { puts "Data on \"$name\": $mydata($name)" }
While arrays are great as a storage facility for some purposes, they are a bit tricky when you pass them to a procedure: they are actually collections of variables. This will not work:
proc print12 {a} { puts "$a(1), $a(2)" } set array(1) "A" set array(2) "B" print12 $array
The reason is very simple: an array does not have a value. Instead the above code should be:
proc print12 {array} { upvar $array a puts "$a(1), $a(2)" } set array(1) "A" set array(2) "B" print12 array
So, instead of passing a "value" for the array, you pass the name. This gets aliased (via the upvar command) to a local variable (that behaves the as original array). You can make changes to the original array in this way too.
Example
# The example of the revisited - to get a # more general "database" proc addname {db first last} { upvar $db name # Create a new ID (stored in the name array too for easy access) incr name(ID) set id $name(ID) set name($id,first) $first ;# The is simply a string! set name($id,last) $last ;# So we can use both fixed and ;# varying parts } proc report {db} { upvar $db name # Loop over the last names: make a map from last name to ID foreach n [array names name "*,last"] { # # Split the name to get the ID - the first part of the name! # regexp {^[^,]+} $n id # # Store in a temporary array: # an "inverse" map of last name to ID) # set last $name($n) set tmp($last) $id } # # Now we can easily print the names in the order we want! # foreach last [lsort [array names tmp]] { set id $tmp($last) puts " $name($id,first) $name($id,last)" } } # Initialise the array and add a few names set fictional_name(ID) 0 set historical_name(ID) 0 addname fictional_name Mary Poppins addname fictional_name Uriah Heep addname fictional_name Frodo Baggins addname historical_name Rene Descartes addname historical_name Richard Lionheart addname historical_name Leonardo "da Vinci" addname historical_name Charles Baudelaire addname historical_name Julius Caesar # Some simple reporting puts "Fictional characters:" report fictional_name puts "Historical characters:" report historical_name
FileName is the name of the file to open. access is the file access mode o r......Open the file for reading. The file must already exist. o r+...Open the file for reading and writing. The file must already exist. o w.....Open the file for writing. Create the file if it doesn't exist, or set the length to zero if it does exist. o w+..Open the file for reading and writing. Create the file if it doesn't exist, or set the length to zero if it does exist. o a......Open the file for writing. The file must already exist. Set the current location to the end of the file. o a+...Open the file for writing. The file does not exist, create it. Set the current location to the end of the file. permission is an integer to use to set the file access permissions. The default is rw-rw-rw- (0666). You can use it to set the permissions for the file's owner, the group he/she belongs to and for all the other users. For many applications, the default is fine.
close fileID Closes a file previously opened with open, and flushes any remaining output. gets fileID ?varName? Reads a line of input from FileID, and discards the terminating newline. If there is a varName argument, gets returns the number of characters read (or -1 if an EOF occurs), and places the line of input in varName. If varName is not specified, gets returns the line of input. An empty string will be returned if:
There is a blank line in the file. The current location is at the end of the file. (An EOF occurs.)
puts ?-nonewline? ?fileID? string Writes the characters in string to the stream referenced by fileID. FileID is one of: The value returned by a previous call to open with write access. stdout stderr
read ?-nonewline? fileID Reads all the remaining bytes from fileID, and returns that string. If -nonewline is set, then the last character will be discarded if it is a newline. Any existing end of file condition is cleared before the read command is executed. read fileID numBytes Reads up to numBytes from fileID, and returns the input as a Tcl string. Any existing end of file condition is cleared before the read command is executed. seek fileID offset ?origin? Change the current position within the file referenced by fileID. Note that if the file was opened with "a" access that the current position can not be set before the end of the file for writing, but can be set to the beginning of the file for reading. fileID is one of: o a File identifier returned by open o stdin o stdout o stderr offset is the offset in bytes at which the current position is to be set. The position from which the offset is measured defaults to the start of the file, but can be from the current location, or the end by setting origin appropriately. origin is the position to measure offset from. It defaults to the start of the file. Origin must be one of: o start.........Offset is measured from the start of the file. o current...Offset is measured from the current position in the file. o end...........Offset is measured from the end of the file.
tell fileID Returns the position of the access pointer in fileID as a decimal string. flush fileID Flushes any output that has been buffered for fileID.
The file I/O is buffered. The output may not be sent out when you expect it to be sent. Files will all be closed and flushed when your program exits normally, but may only be closed (not flushed) if the program is terminated in an unexpected manner. There are a finite number of open file slots available. If you expect the program to run in a manner that will cause it to open several files, remember to close the files when you are done with them. An empty line is indistinguishable from an EOF with the command:
set string [gets filename]. Use the eof command to determine if the file is at the end or use the other form of gets (see the example).
You can't overwrite any data in a file that was opened with a access. You can, however seek to the beginning of the file for gets commands. Opening a file with the w+ access will allow you to overwrite data, but will delete all existing data in the file. Opening a file with the r+ access will allow you to overwrite data, while saving the existing data in the file. By default the commands assume that strings represent "readable" text. If you want to read "binary" data, you will have to use the fconfigure command. Often, especially if you deal with configuration data for your programs, you can use the source command instead of the relatively low-level commands presented here. Just make sure your data can be interpreted as Tcl commands and "source" the file.
Example
# Count the number of lines in a text file set infile [open "myfile.txt" r] set number 0 # gets with two arguments returns the length of the line, # -1 if the end of the file is found while { [gets $infile line] >= 0 } { incr number } close $infile puts "Number of lines: $number" # Also report it in an external file set outfile [open "report.out" w] puts $outfile "Number of lines: $number" close $outfile
string manipulation appropriate to parsing file names o dirname ........ Returns directory portion of path o extension........ Returns file name extension o rootname ....... Returns file name without extension o tail .................... Returns filename without directory information about an entry in a directory: o atime ................ Returns time of last access o executable ..... Returns 1 if file is executable by user o exists ................ Returns 1 if file exists o isdirectory ...... Returns 1 if entry is a directory o isfile .................. Returns 1 if entry is a regular file o lstat ................... Returns array of file status information o mtime ............... Returns time of last data modification o owned ................ Returns 1 if file is owned by user o readable............ Returns 1 if file is readable by user o readlink............. Returns name of file pointed to by a symbolic link o size ..................... Returns file size in bytes o stat ..................... Returns array of file status information o type .................... Returns type of file o writable ............ Returns 1 if file is writeable by user
Between these two commands, a program can obtain most of the information that it may need. glob ?switches? pattern ?patternN? returns a list of file names that match pattern or patternN Switches may be one of: -nocomplain Allows glob to return an empty list without causing an error. Without this flag, an error would be generated when the empty list was returned. -Marks the end of switches. This allows the use of "-" in a pattern without confusing the glob parser. Pattern follows the same matching rules as the string match globbing rules with these exceptions:
{a,b,...} Matches any of the strings a,b, etc. A "." at the beginning of a filename must match a "." in the filename. The "." is only a wildcard if it is not the first character in a name. All "/" must match exactly. If the first two characters in pattern are ~/, then the ~ is replaced by the value of the HOME environment variable. If the first character in pattern is a ~, followed by a login id, then the ~loginid is replaced by the path of loginid's home directory.
Note that the filenames that match pattern are not in a sorted order. file atime name Returns the number of seconds since 1/1/1970 when the file name was last accessed. Generates an error if the file doesn't exist, or the access time cannot be queried. file dirname name Returns the directory portion of a path/filename string. If name contains no slashes, file dirname returns a ".". If the last "/" in name is also the first character, it returns a "/". file executable name Returns a 1 if file name is executable by the current user, otherwise returns a 0. file exists name Returns a 1 if the file name exists, and the user has search access in all the directories leading to the file. Otherwise, a 0 is returned. file extension name Returns the file extension. file isdirectory name Returns 1 if file name is a directory, otherwise returns 0. file isfile name Returns 1 if file name is a regular file, otherwise returns 0. file lstat name varName This returns the same information returned by the system call lstat. The results are placed in the associative array varName. The es in varName are:
atime.......time of last access ctime.......time of last file status change dev...........inode's device gid............group ID of the file's group ino............inode's number mode.......inode protection mode mtime.....time of last data modification nlink........number of hard links size...........file size, in bytes type..........Type of File uid.............user ID of the file's owner
Because this calls lstat, if name is a symbolic link, the values in varName will refer to the link, not the file that is linked to.
See stat also. file mtime name Returns the time of the last data modification in seconds since Jan 1, 1970. file owned name Returns 1 if the file is owned by the current user, otherwise returns 0. file readable name Returns 1 if the file is readable by the current user, otherwise returns 0. file readlink name Returns the name of the file a symlink is pointing to. If name isn't a symlink, or can't be read, an error is generated. file rootname name Returns all the characters in name up to but not including the last ".". Returns $name if name doesn't include a ".". file size name Returns the size of name in bytes. file stat name varName This returns the same information returned by the system call stat. The results are placed in the associative array varName. The es in varName are:
atime.......time of last access ctime.......time of last file status change dev...........inode's device gid............group ID of the file's group ino............inode's number mode.......inode protection mode mtime.....time of last data modification nlink........number of hard links size...........file size in bytes type..........Type of file uid.............user ID of the file's owner
file tail name Returns all of the characters in name after the last slash. Returns $name if name contains no slashes. file type name Returns a string giving the type of file name, which will be one of:
file...................................Normal file directory........................Directory characterSpecial.......Character oriented device blockSpecial.............. Block oriented device fifo...................................Named pipe link..................................Symbolic link socket............................Named socket
Example
set ail1 [glob PATTERN1] set ail2 [glob PATTERN2]
set fmt "%-12s %-16s %8s %-7s" puts "[format "$fmt Comment" "Directory" "Name" "Inode" "Type"]"
foreach name [concat $ail1 $ail2] { ;# split the name into pieces for display: set dir [file dirname $name] set filename [file tail $name] ;# Collect some status and type info. file stat $name arr set type [file type $name] ;# Display what we've learned. puts -nonewline "[format $fmt $dir $filename $arr(ino) $type]" ;# and particular data depending on whether item is a file or symbolic link. if {[string match [file type $name] "link"]} { puts " points to: [file readlink $name]" } if {[string match [file type $name] "file"]} { puts " Size: [file size $name] bytes " } }
open ...... run a new program with I/O connected to a file descriptor exec ...... run a new program as a subprocess
The open call is the same call that is used to open a file. If the first character in the file name argument is a pipe symbol (|), then open will treat the rest of the argument as a program name, and will exec that program with the standard input or output connected to a file descriptor. A pipe can be opened to a sub-process for reading, writing or both reading and writing. If the file is opened for both reading and writing you must be aware that the pipes are buffered. The output from a puts command will be saved in an I/O buffer until the buffer is full, or until you execute a flush command to force it to be transmitted to the subprocess. The output of the subprocess will not be available to a read or gets until the I/O buffer for the subprocess has filled its output buffer. The exec call is similar to invoking a program ( or a set of programs piped together) from the shell prompt or in a unix shell script. It supports several styles of output redirection, or it can return the output of the sub-process as the return of the exec call. open | progName ?access? Returns a file descriptor for the pipe. The progName argument must start with the pipe symbol. If progName is enclosed in quotes or braces, it can include arguments to the subprocess. exec ?switches? arg1 ?arg2? ... ?argN? Exec treats its arguments as the names and arguments for a set of subprocesses to execute. If the first args start with a "-", then they are treated as switches to the exec command, instead of being invoked as subprocesses or subprocess options. Switches are: -keepnewline Retains a trailing newline in the pipeline's output. Normally a trailing newline will be deleted. -Marks the end of the switches. The next string will be treated as arg1, even if it starts with a "-" Arg1 - argN can be one of:
the name of a program to execute an command line argument for the subprocess an I/O redirection instruction.
There are many permutations to the I/O redirection commands. The main subset of these commands is: | Pipes the standard output of the command preceeding the pipe symbol into the standard input of the command following the pipe symbol. < fileName The first program in the pipe will read input from fileName. < @ fileID The first program in the pipe will read input from the Tcl descriptor fileID. FileID is the value returned from an open ... "r" command. < < value The first program in the pipe will read value as its input. > fileName The output of the last program in the pipe will be sent to fileName. Any previous contents of fileName will be lost. > > fileName The output of the last program in the pipe will be appended to fileName. 2> fileName The standard error from all the programs in the pipe will be sent to fileName. Any previous contents of fileName will be lost. 2> > fileName The standard error from all the programs in the pipe will be appended to fileName. > @ fileID The output from the last program in the pipe will be written to fileID. FileID is the value returned from an open ... "w" command. If you are familiar with shell programming, there are a few differences to be aware of when you are writing Tcl scripts that use the exec and open calls.
You don't need the quotes that you would put around arguments to escape them from the shell expanding them. In the example, the argument to sed is not put in quotes. If it were put in quotes, the quotes would be passed to sed, instead of being stripped off (as the shell does), and sed would report an error. If you use the open |cmd "r+" construct, you must follow each puts with a flush to force Tcl to send the command from its buffer to the program. The output from the subprocess may be buffered in its output buffer. You can sometimes force the output from the sub-process to flush by sending an exit command to the process. You can also use the fconfigure command to make a channel unbuffered.
The expect extension to Tcl provides a much better interface to other programs, which handles the buffering problem.
If one of the commands in an open |cmd fails the open does not return an error. However, attempting to read input from the file descriptor with gets $file will return an empty string. Using the gets $file input construct will return a character count of -1. Put quotes around the s/.Q//g in the example to see this behavior.
If one of the commands in an exec call fails to execute, the exec will return an error, and the error output will include the last line describing the error.
Example
# Create a unique (mostly) file name for a Tcl program set tempFileName "TEMPDIR/inv_[pid].tcl" # Open the output file, and # write a simple program to it set outfl [open $tempFileName w] puts $outfl { set len [gets stdin line] if {$len < 5} {exit -1} for {set i $len} {$i >= 0} {incr i -1} { append l2 [string range $line $i $i] } puts $l2 exit 0; } # Flush and close the file flush $outfl close $outfl # Run the new Tcl file interactively # Open a pipe to the program set io [open "|TCL_INTERP $tempFileName" r+] # send a string to the new program # *MUST FLUSH* puts $io "This will come back backwards." flush $io # Get the reply, and display it. set len [gets $io line] puts "To reverse: 'This will come back backwards.'" puts "Reversed is: $line" puts "The line is $len characters long" # Run the program with input defined in an exec call set invert [exec TCL_INTERP $tempFileName << \ "ABLE WAS I ERE I SAW ELBA"] # display the results puts "The inversion of 'ABLE WAS I ERE I SAW ELBA' is \n $invert" # Clean up file delete $tempFileName
Example
proc safeIncr {val {amt 1}} { upvar $val v if {[info exists v]} { incr v $amt} }
if {[info procs safeIncr] == "safeIncr"} { safeIncr a } puts "After calling SafeIncr with a non existent variable: $a" set a 100 safeIncr a puts "After calling SafeIncr with a variable with a value of 100: $a" safeIncr b -3 puts "After calling safeIncr with a non existent variable by -3: $b" set b 100 safeIncr b -3 puts "After calling safeIncr with a variable whose value is 100 by -3: $b" puts "\nThese variables have been defined: [lsort [info vars]]" puts "\nThese globals have been defined: [lsort [info globals]]" set exist [info procs localproc]; if {$exist == ""} { puts "\nlocalproc does not exist at point 1" } proc localproc {} { global argv; set loc1 1; set loc2 2; puts "\nLocal variables accessible in this proc are: [lsort [info locals]]" puts "\nVariables accessible from this proc are: [lsort [info vars]]" puts "\nGlobal variables visible from this proc are: [lsort [info globals]]" } set exist [info procs localproc]; if {$exist != ""} { puts "localproc does exist at point 2" } localproc;
Example
puts "This is how many commands have been executed: [info cmdcount]" puts "Now *THIS* many commands have been executed: [info cmdcount]" puts "\nThis interpreter is revision level: [info tclversion]" puts "This interpreter is at patch level: [info patchlevel]" puts "The Pid for this program is [pid]" proc factorial {val} { puts "Current level: [info level] - val: $val" set lvl [info level] if {$lvl == $val} {return $val;} return [expr ($val-$lvl) * [factorial $val]]; } set count1 [info cmdcount] set fact [factorial 3] set count2 [info cmdcount] puts "The factorial of 3 is $fact" puts "Before calling the factorial proc, $count1 commands had been executed" puts "After calling the factorial proc, $count2 commands had been executed" puts "It took [expr $count2-$count1] commands to calculate this factorial"
Access the contents of a proc in a debugger. Generate custom procs from a template. Report default values while prompting for input.
Info commands that return information about a proc info args procname Returns a list of the names of the arguments to the procedure procname. info body procname Returns the body of the procedure procname. info default procname arg varName Returns 1 if the argument arg in procedure procName has a default, and sets varName to the default. Otherwise, returns 0.
Example
proc demo {argument1 {default "DefaultValue"} } { puts "This is a demo proc. It is being called with $argument1 and $default" } puts "The args for demo are: [info args demo]\n" puts "The body for demo is: [info body demo]\n" set arglist [info args demo] foreach arg $arglist { if {[info default demo $arg defaultval]} { puts "$arg has a default value of $defaultval" } else { puts "$arg has no default" } }
Modularization - source
The source command will load a file and execute it. This allows a program to be broken up into multiple files, with each file defining procedures and variables for a particular area of functionality. For instance, you might have a file called database.tcl that contains all the procedures for dealing with a database, or a file called gui.tcl that handles creating a graphical user interface with Tk. The main script can then simply include each file using the source command. More powerful techniques for program modularization are discussed in the on packages. This command can be used to:
separate a program into multiple files. make a library file that contains all the procs for a particular set of functions. configure programs. load data files. Reads the script in fileName and executes it. If the script executes successfully, source returns the value of the last statement in the script. If there is an error in the script, source will return that error. If there is a return (other than within a proc definition) then source will return immediately, without executing the remainder of the script. If fileName starts with a tilde (~) then $env(HOME) will substituted for the tilde, as is done in the file command.
source fileName
Example
sourcedata.tcl:
# Example data file to be sourced set scr [info script] proc testproc {} { global scr puts "testproc source file: $scr" } set abc 1 return set aaaa 1
sourcemain.tcl:
set filename "sourcedata.tcl" puts "Global variables visible before sourcing $filename:" puts "[lsort [info globals]]\n" if {[info procs testproc] eq ""} {
sourcing $filename"
puts "\nNow executing testproc" testproc puts "Global variables visible after sourcing $filename:" puts "[lsort [info globals]]\n"
Using packages
The package command provides the ability to use a package, compare package versions, and to register your own packages with an interpreter. A package is loaded by using the package require command and providing the package name and optionally a version number. The first time a script requires a package Tcl builds up a database of available packages and versions. It does this by searching for package files in all of the directories listed in the tcl_pkgPath and auto_path global variables, as well as any subdirectories of those directories. Each package provides a file called pkg.tcl that tells Tcl the names and versions of any packages in that directory, and how to load them if they are needed. It is good style to start every script you create with a set of package require statements to load any packages required. This serves two purposes: making sure that any missing requirements are identified as soon as possible; and, clearly documenting the dependencies that your code has. Tcl and Tk are both made available as packages and it is a good idea to explicitly require them in your scripts even if they are already loaded as this makes your scripts more portable and documents the version requirements of your script.
Creating a package
There are three steps involved in creating a package:
Adding a package provide statement to your script. Creating a pkg.tcl file. Installing the package where it can be found by Tcl.
The first step is to add a package provide statement to your script. It is good style to place this statement at the top of your script. The package provide command tells Tcl the name of your package and the version being provided. The next step is to create a pkg.tcl file. This file tells Tcl how to load your package. In essence the file is simply a Tcl file which is loaded into the interpreter when Tcl searches
for packages. It should use the package ifneeded command register a script which will load the package when it is required. The pkg.tcl file is evaluated globally in the interpreter when Tcl first searches for any package. For this reason it is very bad style for an script to do anything other than tell Tcl how to load a package; scripts should not define procs, require packages, or perform any other action which may affect the state of the interpreter. The simplest way to create a pkg.tcl script is to use the pkg_mk command. The pkg_mk command scans files which match a given pattern in a directory looking for package provide commands. From this information it generates an appropriate pkg.tcl file in the directory. Once a package has been created, the next step is to move the package to somewhere that Tcl can find it. The tcl_pkgPath and auto_path global variables contain a list of directories that Tcl searches for packages. The package and all the files that implement the package should be installed into a subdirectory of one of these directories. Alternatively, the auto_path variable can be extended at run-time to tell Tcl of new places to look for packages.
package require ?-exact? name ?version? Loads the package identified by name. If the -exact switch is given along with a version number then only that exact package version will be accepted. If a version number is given, without the -exact switch then any version equal to or
package provide name ?version? If a version is given this command tells Tcl that this version of the package indicated by name is loaded. If a different version of the same package has already been loaded then an error is generated. If the version argument is omitted, then
greater than that version (but with the same major version number) will be accepted. If no version is specied then any version will be loaded. If a matching package can be found then it is loaded and the command returns the actual version number; otherwise it generates an error.
pkg_mk ?-direct? ?-lazy? ?-load pkgPat? ?-verbose? dir ?pattern pattern ...? Creates a pkg.tcl file for a package or set of packages. The command works by loading the files matching the patterns in the directory, dir and seeing what new
the command returns the version number that is currently loaded, or the empty string if the package has not been loaded.
packages and commands appear. The command is able to handle both Tcl script files and binary libraries (not discussed here).
Namespaces
One problem that can occur when using packages, and particularly when using code written by others is that of name collision. This happens when two pieces of code try to define a procedure or variable with the same name. In Tcl when this occurs the old procedure or variable is simply overwritten. This is sometimes a useful feature, but more often it is the cause of bugs if the two definitions are not compatible. To solve this
problem, Tcl provides a namespace command to allow commands and variables to be partitioned into separate areas, called namespaces. Each namespace can contain commands and variables which are local to that namespace and cannot be overwritten by commands or variables in other namespaces. When a command in a namespace is invoked it can see all the other commands and variables in its namespace, as well as those in the global namespace. Namespaces can also contain other namespaces. This allows a hierachy of namespaces to be created in a similar way to a file system hierachy, or the Tk widget hierachy. Each namespace itself has a name which is visible in its parent namespace. Items in a namespace can be accessed by creating a path to the item. This is done by joining the names of the items with ::. For instance, to access the variable bar in the namespace foo, you could use the path foo::bar. This kind of path is called a relative path because Tcl will try to follow the path relative to the current namespace. If that fails, and the path represents a command, then Tcl will also look relative to the global namespace. You can make a path fully-qualified by describing its exact position in the hierachy from the global namespace, which is named ::. For instance, if our foo namespace was a child of the global namespace, then the fully-qualified name of bar would be ::foo::bar. It is usually a good idea to use fully-qualified names when referring to any item outside of the current namespace to avoid surprises. A namespace can export some or all of the command names it contains. These commands can then be imported into another namespace. This in effect creates a local command in the new namespace which when invoked calls the original command in the original namespace. This is a useful technique for creating short-cuts to frequently used commands from other namespaces. In general, a namespace should be careful about exporting commands with the same name as any built-in Tcl command or with a commonly used name. Some of the most important commands to use when dealing with namespaces are:
namespace eval path script
This command evaluates the script in the namespace specified by path. If the namespace doesn't exist then it is created. The namespace becomes the current namespace while the script is executing, and any unqualified names will be resolved relative to that namespace. Returns the result of the last command in script. Deletes each namespace specified, along with all variables, commands and child namespaces it contains. Returns the fully qualified path of the current namespace. Adds any commands matching one of the patterns to the list of commands exported by the current namespace. If the -clear switch is given then the export list is cleared before adding any new commands. If no arguments are given, returns the currently exported command names. Each pattern is a glob-style pattern such as *, [a-z]*, or *foo*.
Imports all commands matching any of the patterns into the current namespace. Each pattern is a glob-style pattern such as foo::*, or foo::bar.
Example
This example creates a package which provides a stack data structure.
# Register the package package provide tutstack 1.0 package require Tcl 8.5 # Create the namespace namespace eval ::tutstack { # Export commands namespace export push pop peek empty # Set up state variable stack variable id 0
# Create a new stack proc ::tutstack::create {} { variable stack variable id set token "stack[incr id]" set stack($token) [list] return $token
# Push an element onto a stack proc ::tutstack::push {token elem} { variable stack
# Check if stack is empty proc ::tutstack::empty {token} { variable stack set num [llength $stack($token)] return [expr {$num == 0}]
# Remove an element from the top of the stack proc ::tutstack::pop {token} { variable stack if {[empty $token]} { error "stack empty" } set ret [l $stack($token) end] set stack($token) [lrange $stack($token) 0 end-1] return $ret
# See what is on top of the stack without removing it proc ::tutstack::peek {token} { variable stack if {[empty $token]} { error "stack empty" } } return [l $stack($token) end]
Example
set cmd {puts "Evaluating a puts"} puts "CMD IS: $cmd" eval $cmd
if {[string match [info procs newProcA] ""] } { puts "\nDefining newProcA for this invocation" set num 0; set cmd "proc newProcA " set cmd [concat $cmd "{} {\n"] set cmd [concat $cmd "global num;\n"] set cmd [concat $cmd "incr num;\n"] set cmd [concat $cmd " return \"/tmp/TMP.[pid].\$num\";\n"] set cmd [concat $cmd "}"] eval $cmd } puts "\nThe body of newProcA is: \n[info body newProcA]\n" puts "newProcA returns: [newProcA]"
puts "newProcA returns: [newProcA]" # # Defind a proc using lists # if {[string match [info procs newProcB] ""] } { puts "\nDefining newProcB for this invocation" set cmd "proc newProcB " lappend cmd {} lappend cmd {global num; incr num; return $num;} eval } $cmd
puts "\nThe body of newProcB is: \n[info body newProcB]\n" puts "newProcB returns: [newProcB]"
will generate an error. The reason that the second command generates an error is that the eval uses concat to merge its arguments into a command string. This causes the two words Not OK to be treated as two arguments to puts. If there is more than one argument to puts, the first argument must be a file pointer. Correct ways to write the second command include these:
eval [list puts {Not OK}] eval [list puts "Not OK"] set cmd "puts" ; lappend cmd {Not OK}; eval $cmd
As long as you keep track of how the arguments you present to eval will be grouped, you can use many methods of creating the strings for eval, including the string commands and format. The recommended methods of constructing commands for eval is to use the list and lappend commands. These commands become difficult to use, however if you need to put braces in the command, as was done in the . The example from the is re-implemented in the example code using lappend. The completeness of a command can be checked with info complete. Info complete can also be used in an interactive program to determine if the line being typed in is a complete command, or the user just entered a newline to format the command better. info complete string If string has no unmatched brackets, braces or parentheses, then a value of 1 is returned, else 0 is returned.
Example
set cmd "OK" eval puts $cmd set cmd "puts" ; lappend cmd {Also OK}; eval $cmd
set cmd "NOT OK" eval puts $cmd eval [format {%s "%s"} puts "Even This Works"] set cmd "And even this can be made to work" eval [format {%s "%s"} puts $cmd ] set tmpFileNum 0;
set cmd {proc tempFileName } lappend cmd "" lappend cmd "global num; incr num; return \"/tmp/TMP.[pid].\$num\"" eval $cmd puts "\nThis is the body of the proc definition:" puts "[info body tempFileName]\n"
set cmd {puts "This is Cool!} if {[info complete $cmd]} { eval $cmd } else { puts "INCOMPLETE COMMAND: $cmd" }
Example
set a "alpha" set b a puts {a and b with no substitution: $a $$b} puts "a and b with one pass of substitution: $a $$b" puts "a and b with subst in braces: [subst {$a $$b}]" puts "a and b with subst in quotes: [subst "$a $$b"]\n" puts "format with no subst [format {$%s} $b]" puts "format with subst: [subst [format {$%s} $b]]" eval "puts \"eval after format: [format {$%s} $b]\"" set num 0; set cmd "proc tempFileName {} " set cmd [format "%s {global num; incr num;" $cmd] set cmd [format {%s return "/tmp/TMP.%s.$num"} $cmd [pid] ] set cmd [format "%s }" $cmd ] eval $cmd puts "[info body tempFileName]" set a arrayname set b set c newvalue eval [format "set %s(%s) %s" $a $b $c] puts ": $b of $a was set to: $arrayname()"
Example
set dirs [list TEMPDIR] puts "[format "%-15s %-20s " "FILE" "DIRECTORY"]"
foreach dir $dirs { catch {cd $dir} set c_files [glob -nocomplain c*] foreach name $c_files { puts "[format "%-15s %-20s " $name [pwd]]" } }
Generates a return exception condition. The possible arguments are: -code the next value specifies the return status. Code must be one of:
ok ........ Normal status return error ..... Proc returns error status return .... Normal return break ..... Proc returns break status continue .. Proc returns continue status
-errorinfo info will be the first string in the errorInfo variable. -errorcode The proc will set errorCode to errorcode. value The string value will be the value returned by this proc. errorInfo errorInfo is a global variable that contains the error information from commands that have failed. errorCode errorCode is a global variable that contains the error code from command that failed.
Example
proc errorproc {x} { if {$x > 0} { error "Error generated by error" "Info String for error" $x } } catch errorproc puts "after bad proc call: ErrorCode: $errorCode" puts "ERRORINFO:\n$errorInfo\n" set errorInfo ""; catch {errorproc 0} puts "after proc call with no error: ErrorCode: $errorCode" puts "ERRORINFO:\n$errorInfo\n" catch {errorproc 2} puts "after error generated in proc: ErrorCode: $errorCode" puts "ERRORINFO:\n$errorInfo\n" proc returnErr { x } { return -code error -errorinfo "Return Generates This" -errorcode "999" }
catch {returnErr 2} puts "after proc that uses return to generate an error: ErrorCode: $errorCode" puts "ERRORINFO:\n$errorInfo\n" proc withError {x} { set x $a } catch {withError 2} puts "after proc with an error: ErrorCode: $errorCode" puts "ERRORINFO:\n$errorInfo\n" catch {open "/no_such_directory/no_such_file" "r"} puts "after an error call to a nonexistent file:" puts "ErrorCode: $errorCode" puts "ERRORINFO:\n$errorInfo\n"
Example
proc traceproc {variableName arrayElement operation} {
set op(w) "Write"; set op(u) "Unset"; set op(r) "Read" set level [info level] incr level -1; if {$level > 0} { set procid [info level $level] } else {set procid "main"} if {![string match $arrayElement ""]} { puts "TRACE: $op($operation) $variableName($arrayElement) in $procid" } else { puts "TRACE: $op($operation) $variableName in $procid" } } proc testProc {input1 input2} { upvar $input1 i upvar $input2 j set i 2 set k $j; } trace variable i1 w traceproc trace variable i2 r traceproc trace variable i2 w traceproc set i2 "testvalue" puts "\ncall testProc" testProc i1 i2 puts "\nTraces on i1: [trace vinfo i1]" puts "Traces on i2: [trace vinfo i2]\n" trace vdelete i2 r traceproc puts "Traces on i2 after vdelete: [trace vinfo i2]" puts "\ncall testProc again" testProc i1 i2
Example
puts "There are $argc arguments to this script" puts "The name of this script is $argv0" if {$argc > 0} {puts "The other arguments are: $argv" } puts "You have these environment variables set:" foreach [array names env] { puts "$: $env($)" }
Example
proc timetst1 {lst} { set x [lsearch $lst "5000"] return $x } proc timetst2 {array} { upvar $array a return $a(5000); } # Make a long list and a large array.
for {set i 0} {$i < 5001} {incr i} { set array($i) $i lappend list $i } puts "Time for list search: [ time {timetst1 $list} 10]" puts "Time for array : [ time {timetst2 array} 10]" proc existence {variable} { upvar $variable testVar; if {[info exists testVar]} { puts "$variable Exists" } else { puts "$variable Does Not Exist" } } set x 1 set y 2 for {set i 0} {$i < 5} {incr i} { set a($i) $i; } puts "\ntesting unsetting a simple variable" ;# Confirm that x exists. existence x ;# Unset x puts "x has been unset" unset x ;# Confirm that x no longer exists. existence x puts "\ntesting unsetting a member of an array" existence a(0); puts "a0 has been unset" unset a(0); existence a(0); puts "\ntesting unsetting several members of an array, with an error" existence a(3); existence a(4); catch {unset a(3) a(0) a(4)} puts "\nAfter attempting to delete a(3), a(0) and a(4)" existence a(3); existence a(4); puts "\ntesting unsetting an array" existence a; puts "a has been unset" unset a; existence a;
channel . . . .The channel for the new client address . . . . The IP Address of this client port. . . . . . . . The port that is assigned to this client
socket ?options? host port The socket command without the -server option opens a client connection to the system with IP Address host and port address port. The IP Address may be given as a numeric string, or as a fully qualified domain address. To connect to the local host, use the address 127.0.0.1 (the loopback address). fileevent channelId readable ?script? fileevent channelId writeable ?script? The fileevent command defines a handler to be invoked when a condition occurs. The conditions are readable, which invokes script when a data is ready to be read on channelId, and writeable, when channelID is ready to receive data. vwait varName The vwait command pauses the execution of a script until some background action sets the value of varName. A background action can be a proc invoked by a fileevent, or a socket connection, or an event from a tk widget.
Example
proc serverOpen {channel addr port} { global connected set connected 1 fileevent $channel readable "readLine Server $channel" puts "OPENED" } proc readLine {who channel} { global didRead if {[gets $channel line]<0} { fileevent $channel readable {} after idle "close $channel;set out 1" } else { puts "READ LINE: $line" puts $channel "This is a return" flush $channel; set didRead 1 } } set connected 0; # catch {socket -server serverOpen 33000} server set server [socket -server serverOpen 33000] after 100 update; set sock [socket -async 127.0.0.1 33000] vwait connected puts $sock "A Test Line" flush $sock vwait didRead set len [gets $sock line] puts "Return line: $len -- $line" catch {close $sock} vwait out close $server
%a . . . . Abbreviated weekday name (Mon, Tue, etc.) %A . . . . Full weekday name (Monday, Tuesday, etc.) %b . . . . Abbreviated month name (Jan, Feb, etc.) %B . . . . Full month name (January, February, etc.) %d. . . . . Day of month %j . . . . . Julian day of year %m . . . . Month number (01-12) %y. . . . . Year in century %Y . . . . Year with 4 digits %H . . . . Hour (00-23) %I . . . . . Hour (00-12) %M . . . . Minutes (00-59) %S . . . . . Seconds(00-59) %p . . . . . PM or AM %D . . . . Date as %m/%d/%y %r. . . . . Time as %I:%M:%S %p %R . . . . Time as %I:%M
clock scan dateString The scan subcommand converts a human readable string to a system clock value, as would be returned by clock seconds The dateString argument contains strings in these forms: time A time of day in one of the formats shown below. Meridian may be AM, or PM, or a capitalization variant. If it is not sepcified, then the hour (hh) is interpreted as a 24 hour clock. Zone may be a three letter description of a time zone, EST, PDT, etc.
Example
set systemTime [clock seconds] puts "The time is: [clock format $systemTime -format %H:%M:%S]" puts "The date is: [clock format $systemTime -format %D]" puts [clock format $systemTime -format {Today is: %A, the %d of %B, %Y}] puts "\n the default format for the time is: [clock format $systemTime]\n" set halBirthBook "Jan 12, 1997" set halBirthMovie "Jan 12, 1992" set bookSeconds [clock scan $halBirthBook] set movieSeconds [clock scan $halBirthMovie] puts "The book and movie versions of '2001, A Space Oddysey' had a" puts "discrepency of [expr $bookSeconds - $movieSeconds] seconds in how" puts "soon we would have sentient computers like the HAL 9000"
-blocking . . . Determines whether or not the task will block when data cannot be moved on a channel. (i.e. If no data is available on a read, or the buffer is full on a write). -buffersize . . . The number of bytes that will be buffered before data is sent, or can be buffered before being read when data is received. The value must be an integer between 10 and 1000000.
-translation . . . Sets how Tcl will terminate a line when it is output. By default, the lines are teminated with the newline, carriage return, or newline/carriage return that is appropriate to the system on which the interpreter is running. This can be configured to be:
o o o o o
auto . . . Translates newline, carriage return, or newline/carriage return as an end of line marker. Outputs the correct line termination for the current platform. binary . . Treats newlines as end of line markers. Does not add any line termination to lines being output. cr . . . . Treats carriage returns as the end of line marker (and translates them to newline internally). Output lines are terminated with a carriage return. This is the Macintosh standard. crlf . . . Treats cr/lf pairs as the end of line marker, and terminates output lines with a carriage return/linefeed combination. This is the Windows standard. lf . . . . Treats linefeeds as the end of line marker, and terminates output lines with a linefeed. This is the Unix standard.
The example is similar to the lesson 40 example with a client and server socket in the same script. It shows a server channel being configured to be non-blocking, and using the default buffering style - data is not made avaialble to the script until a newline is present, or the buffer has filled. When the first write: puts -nonewline $sock "A Test Line" is done, the fileevent triggers the read, but the gets can't read characters because there is no newline. The gets returns a -1, and fblocked returns a 1. When a bare newline is sent, the data in the input buffer will become available, and the gets returns 18, and fblocked returns 0.
Example
proc serverOpen {channel addr port} { puts "channel: $channel - from Address: $addr Port: $port" puts "The default state for blocking is: [fconfigure $channel blocking]" puts "The default buffer size is: [fconfigure $channel -buffersize ]" ;# Set this channel to be non-blocking. fconfigure $channel -blocking 0 set bl [fconfigure $channel -blocking] puts "After fconfigure the state for blocking is: $bl"
;# Change the buffer size to be smaller fconfigure $channel -buffersize 12 puts "After Fconfigure buffer size is: [fconfigure $channel buffersize ]\n" ;# When input is available, read it. fileevent $channel readable "readLine Server $channel" } proc readLine {who channel} { global didRead global blocked puts "There is input for $who on $channel" set len [gets $channel line] set blocked [fblocked $channel] puts "Characters Read: $len Fblocked: $blocked" if {$len < 0} { if {$blocked} {puts "Input is blocked" } else { puts "The socket was closed - closing my end" close $channel; } } else { puts "Read $len characters: $line" puts $channel "This is a return" flush $channel; } incr didRead; } set server [socket -server serverOpen 33000] after 120 update; application # This kicks MS-Windows machines for this
set sock [socket 127.0.0.1 33000] set bl [fconfigure $sock -blocking] set bu [fconfigure $sock -buffersize] puts "Original setting for sock: Sock blocking: $bl buffersize: $bu" fconfigure $sock -blocking No fconfigure $sock -buffersize 8; set bl [fconfigure $sock -blocking] set bu [fconfigure $sock -buffersize] puts "Modified setting for sock: Sock blocking: $bl buffersize: $bu\n" # Send a line to the server -- NOTE flush set didRead 0
puts -nonewline $sock "A Test Line" flush $sock; # Loop until two reads have been done. while {$didRead < 2} { ;# Wait for didRead to be set vwait didRead if {$blocked} {puts $sock "Newline" ; flush $sock; puts "SEND NEWLINE"} } set len [gets $sock line] puts "Return line: $len -- $line" close $sock vwait didRead catch {close $server}
Child interpreters
For most applications, a single interpreter and subroutines are quite sufficient. However, if you are building a client-server system (for example) you may need to have several interpreters talking to different clients, and maintaining their state. You can do this with state variables, naming conventions, or swapping state to and from disk, but that gets messy. The interp command creates new child interpreters within an existing interpreter. The child interpreters can have their own sets of variables and files, or they can be given access to items in the parent interpreter. If the child is created with the -safe option, it will not be able to access the file system, or otherwise damage your system. This feature allows a script to evaluate code from an unknown (and untrusted) site. The names of child interpreters are a hierarchical list. If interpreter foo is a child of interpreter bar, then it can be accessed from the toplevel interpreter as {bar foo}. The primary interpreter (what you get when you type tclsh) is the empty list {}. The interp command has several subcommands and options. A critical subset is: interp create ?-safe? ?name? Creates a new interpreter and returns the name. If the -safe option is used, the new interpreter will be unable to access certain dangerous system facilities. interp delete name Deletes the named child interpreter. interp eval args This is similar to the regular eval command, except that it evaluates the script in the child interpreter instead of the primary interpreter. The interp eval command concatenates the args into a string, and ships that line to the child interpreter to evaluate. interp alias srcPath srcCmd targetPath targetCmd ?arg arg? The interp alias command allows a script to share procedures between child interpreters or between a child and the primary interpreter. Note that slave interpreters have a separate state and namespace, but do not have separate event loops. These are not threads, and they will not execute independently. If one slave interpreter gets stopped by a blocking I/O request, for instance, no other interpreters will be prcessed until it has unblocked. The example below shows two child interpreters being created under the primary interpreter {}. Each of these interpreters is given a variable name which contains the name of the interpreter.
Note that the alias command causes the procedure to be evaluated in the interpreter in which the procedure was defined, not the interpreter in which it was evaluated. If you need a procedure to exist within an interpreter, you must interp eval a proc command within that interpreter. If you want an interpreter to be able to call back to the primary interpreter (or other interpreter) you can use the interp alias command.
Example
set i1 [interp create firstChild] set i2 [interp create secondChild] puts "first child interp: $i1" puts "second child interp: $i2\n" # Set a variable "name" in each child interp, and # create a procedure within each interp # to return that value foreach int [list $i1 $i2] { interp eval $int [list set name $int] interp eval $int {proc nameis {} {global name; return "nameis: $name";} } } foreach int [list $i1 $i2] { interp eval $int "puts \"EVAL IN $int: name is \$name\"" puts "Return from 'nameis' is: [interp eval $int nameis]" } # # A short program to return the value of "name" # proc rtnName {} { global name return "rtnName is: $name" } # # Alias that procedure to a proc in $i1 interp alias $i1 rtnName {} rtnName puts "" # This is an error. The alias causes the evaluation # to happen in the {} interpreter, where name is # not defined. puts "firstChild reports [interp eval $i1 rtnName]"