Version 7 of handle

Updated 2008-12-17 12:52:32 by LV

Purpose: to discuss the concept of a handle.

Within Tcl, since everything is a string, sometimes things work a bit differently than expected.

For instance, Tcl variables can have various values.

  set a 123
 set b "xyz abc"
 set c [format "Nuts! There are %3.3d minutes until class" 3.1415]
 set d [open "/etc/motd" "r"]

It is this fourth example that is relevant in this page's discussion. The value from the open command is a string. That string is called a handle. It serves as a sort of indirect reference to a system resource which has been allocated. The puts command, as well as other Tcl commands, expect to deal with handles as one of their arguments. They use them as a black box value - something that you as a developer should not attempt to calculate, dynamically generate directly, etc. Instead, you let the software return these handles to you. Inside Tcl, there are functions which will make use of these handles in the appropriate manner.

[hopefully technical readers will add more details at this point]

NEM Handles are often used in Tcl, where a pointer or reference would be used in other languages - to "point" to a shared complex data structure. Handles themselves obey everything is a string and Tcl's normal value semantics, but the things they are handles for need not, because they are hidden from normal view. Object Oriented extensions are big users of the handle model, with the handle usually being a command which can be used to send messages to the underlying object. The Tcl (and Tk) "core" is a big user of handles - commands, variables, arrays and namespaces are all handle "types".

The other type of value frequently used in Tcl is what is sometimes referred to as transparent. This includes things like lists, dicts (in 8.5), and indeed strings. Transparent values directly represent the value, without the level of indirection that handles provide. See also Abstract Data Types for some discussion on the difference and pros and cons of the different representations.


See also tcl handles.


[ Category tutorial | Category Data Structure ]