Tool Command Language: 5.2 Commands: Built-in Commands

Up: GEOS SDK TechDocs | Up | Prev: 5.1 Notation | Next: 6 Coding

The built-in Tcl commands are as follows:

bc

bc list <proc>
bc disasm <proc>
bc compile <proc>
bc fcompile <file> [<nohelp>]
bc fload <file>
bc fdisasm <file>
bc debug [1|0]

Examples:

"bc compile poof"
Compiles the body of the procedure "poof" and replaces the existing procedure with its compiled form.
"bc fcomp bptutils.tcl"
Creates the file "bptutils.tlc" that contains a stream of compiled Tcl that will do exactly what sourcing bptutils.tcl does, except the resulting procedures will be compiled Tcl, not interpreted Tcl.
"bc fload bptutils.tlc"
Loads a file containing a stream of compiled Tcl code.

The "bc" command allows you to create and examine compiled Tcl code. Compiled Tcl is not nearly as readable or changeable as interpreted Tcl code, but it's 30-50% faster.

The "list" subcommand doesn't work.

See Also: source.

break

break

Examples:

"break"
Break out of the current loop.

Breaks out of the current loop or the current nested interpreter.

See Also: continue, for.

case

case <string> [in] [<pat> <body>]+

Examples:

"[case $c in
       {[0-9]} {
       # do something with digit
   }
       default {
        # do something with non-digit
   }
  ]"
Do one of two things depending on whether the character in $c is a digit.

Perform one of a set of actions based on whether a string matches one or more patterns.

See Also: string, if.

catch

catch <command> [<varName>]

Executes a command, retaining control even if the command generates an error (which would otherwise cause execution to unwind completely).

See Also: protect.

concat

concat <arg1>+

Examples:

"concat $list1 $list2"
Merges the lists in $list1 and $list2 into a single list whose elements are the elements of the two lists.

Concatenates multiple list arguments into a single list.

See Also: list.

continue

continue

Examples:

"continue"
Return to the top of the enclosing loop.

Skips the rest of the commands in the current loop iteration, continuing at the top of the loop.

See Also: break, for.

defsubr

defsubr <name> <args> <body>

Examples:

"defsubr poof {arg1 args} {return [list $arg1 $args]}"
Defines a procedure poof that takes 1 or more arguments and merges them into a list of two elements.

This is the same as the "proc" command, except the new procedure's name may not be abbreviated when it is invoked.

error

error <message>

Examples:

"error {invalid argument}"
Generates an error, giving the not-so-helpful message "invalid argument" to the caller's caller.

See Also: return, catch.

eval

eval <body>

Examples:

"eval $mangled_command"
Evaluate the command contained in $mangled_command and return its result.

Evaluates the passed string as a command and returns the result of that evaluation.

See Also: concat, list.

expr

expr <expression> [float]

Examples:

"expr 36*25"
Multiplies 36 by 25 and returns the result.
"expr $i/6 float"
Divides the number in $i by 6 using floating- point arithmetic; the result is a real number.
"expr 7.2*10 float"
Multiplies 7.2 by 10. Note that though the answer (72) is an integer, we need to pass the "float" keyword to make sure that the expression is interpreted correctly.

Evaluates an arithmetic expression and returns its value.

file

file dirname <name>
file exists <name>
file extension <name>
file isdirectory <name>
file isfile <name>
file readable <name>
file rootname <name>
file tail <name>
file writable <name>
file match <pattern>
file newer <name1> <name2>

Examples:

"file match /pcgeos/tcl/*.tcl"
Looks for all files/directories in /pcgeos/tcl whose name ends with ".tcl".
"file isdir $path"
See if the path stored in $path refers to a directory.
"file tail $path"
Return the final component of the path stored in $path

Performs various checks and manipulations of file and directory names.

See Also: string.

for

for <start> <test> <next> <body>

Examples:

"for {var i 0} {$i < 10} {var i [expr $i+1]} {echo $i}"
Prints the numbers from 0 to 9.

This is Tcl's main looping construct. It functions similarly to the "for" in C.

See Also: foreach, break, continue.

foreach

foreach <varname> <list> <body>

Examples:

"foreach el $list {echo poof = $el}"
Prints each element of the list $list preceded by the profound words "poof = "

This is a looping construct to easily iterate over all the elements of a list.

format

format <formatString> [<arg> ]*

This command generates a formatted string in the same way as the C sprintf procedure (it uses sprintf in its implementation). <formatString> indicates how to format the result, using % fields as in sprintf , and the additional arguments, if any, provide values to be substituted into the result. All of the sprintf options are valid; see the sprintf procedure in a C manual for more details. Each <arg> must match the expected type from the % field in <formatString>; the format command converts each argument to the correct type (floating, integer, etc.) before passing it to sprintf for formatting. The only unusual conversion is for %c; in this case the argument must be a decimal string, which will then be converted to the corresponding ASCII character value. format does backslash substitution on its <formatString> argument, so backslash sequences in <formatString> will be handled correctly even if the argument is in braces. The return value from format is the formatted string.

global

global <varname>+

Examples:

"global attached"
When next the "attached" variable is fetched or set, get it from the global scope, not the local one.

Declares the given variables to be from the global scope.

See Also: var.

if

if <test> [then] <trueBody> 
(elif <test> [(then)] <trueBody>)*
[[else] <falseBody>]

Examples:

"if {$v > 3} {echo yes} {echo no}"
Prints "yes" if $v is greater than 3, else it prints "no".
"if {$v > 3} then {echo yes} else {echo no}"
Ditto.
"if {$v > 3} then {echo yes} elif {$v == 3} {echo maybe} else {echo no}"

This is Tcl's conditional, as you'd expect from its name.

See Also: expr.

index

index <value> <index> [chars]

Examples:

"index {a b c} 1"
Extracts "b" from the list.
"index {hi mom} 3 char"
Extracts "m" from the string.

"index" is used to retrieve a single element or character from a list or string.

info

info args <procname> [<pattern>]
info arglist <procname>
info body <procname>
info cmdcount
info commands [<pattern>]
info default <procname> <arg> <varname>
info globals [<pattern>]
info locals [<pattern>]
info procs [<pattern>]
info vars [<pattern>]

Examples:

"info args fmtval"
Retrieves the names of the arguments for the "fmtval" command so you know in what order to pass things.
"info body print-frame"
Retrieves the string that is the body of the "print-frame" Tcl procedure.
"info commands reg "
Retrieves a list of commands whose names contain the string "reg".

This command provides information about a number of data structures maintained by the Tcl interpreter.

See Also: proc, defcmd, defcommand, defsubr.

length

length <value> [<chars>]

Examples:

"length $args"
Returns the number of elements in the list $args
"length $str char"
Returns the number of characters in the string $str

Determines the number of characters in a string, or elements in a list.

See Also: index, range.

list

list <arg>+

Examples:

"list a b {c d e} {f {g h}}"
Returns the list "a b {c d e} {f {g h}}"

Joins any number of arguments into a single list, applying quoting braces and backslashes as necessary to form a valid Tcl list.

See Also: concat, index, range.

proc

proc <name> <args> <body>

Examples:

"proc poof {{arg1 one} args} {return [list $arg1 $args]}"
Defines a procedure poof that takes 0 or more arguments and merges them into a list of two elements. If no argument is given, the result will be the list {one {}}

Defines a new Tcl procedure that can be invoked by typing a unique abbreviation of the procedure name.

See Also: defsubr, return.

protect

protect <body> <cleanup>

Examples:

"protect {
     var s [stream open $file w]
     # do stuff with the stream
 } {

     catch {stream close $s}

 }"
Perform some random operations on a file making sure the stream gets closed, even if the user types control-C.

Allows one to ensure that clean-up for a sequence of commands will always happen, even if the user types control-C to interrupt the command.

See Also: catch.

range

range <value> <first> <last> [chars] 

Examples:

"range {a b c} 1 end"
Returns {b c} (element 1 to the end)
"range {hi mom} 3 end chars"
Returns "mom"

Extracts a range of characters from a string, or elements from a list.

See Also: index.

return

return [<value>]

Examples:

"return $val"
Returns the string in $val as the value for the current Tcl procedure.

Causes an immediate return from the current Tcl procedure, with or without a value.

See Also: error, proc, defsubr, defcommand, defcmd.

scan

scan <string> <format> [<varname1> ]*

Examples:

"scan $input {my name is %s} name"
Trims the leading string "my name is " from the string in $input and stores the rest of the string within the variable $name

"scan" parses fields from an input string, given the string and a format string that defines the various types of fields. The fields are assigned to variables within the caller's scope.

See Also: format.

source

source <fileName>

Examples:

"source coolness"
Evaluates all commands within the file "coolness.tcl" in the current directory.

Reads and evaluates commands from a file.

string

string compare<string1> <string2> [no_case]
string first<substring> <string> [no_case]
string last<substring> <string> [no_case]
string match<string> <pattern>
string subst <string> <search> <replace> [global] 

Examples:

"if {[string c [index $args 1] all] == 0}"
Do something if the 2nd element of the list in $args is the string "all".
"while {[string m [index $args 0] -*]}"
Loop while the first element of the list in $args begins with a hyphen.

Examine strings in various ways.

See Also: case.

uplevel

uplevel <level> <body>
uplevel <function> <body>

Examples:

"uplevel print-frame {var found1}"
Sets $found to 1 within the variables belonging to the nearest invocation of print-frame on the call stack.
"uplevel 0 {var foo-table}"
Retrieves the value of the global variable foo-table.
"uplevel 1 {var found 1}"
Sets $found to 1 within the scope of the procedure that called the one executing the "uplevel" command.

Provides access to the variables of another procedure for fairly specialized purposes.

See Also: global.

var

var <varname>
var (<name> <value>)+

Examples:

"echo [var poof]"
Prints the value stored in the variable "poof"
"var a b c d"
Assigns the string "b" to the variable "a", and the string "d" to the variable "c".
"var yes $no no $yes"
Exchanges the values of the "yes" and "no" variables

This is the means by which variables are defined in Tcl. Less often, it is also used to retrieve the value of a variable (usually that's done via variable substitution).

See Also: global.


Up: GEOS SDK TechDocs | Up | Prev: 5.1 Notation | Next: 6 Coding