NAME
code - capture the namespace context for a code fragment
SYNOPSIS
ccooddee ?-nnaammeessppaaccee name? command ?arg arg ...?
DESCRIPTION
Creates a scoped value for the specified command and its associated arg arguments. A scoped value is a list with three elements: the "@scope" keyword, a namespace context, and a value string. For example, the command namespace foo {code puts "Hello World!" } produces the scoped value: @scope ::foo
{puts {Hello World!}} Note that the ccooddee command captures the currentnamespace context. If the -nnaammeessppaaccee flag is specified, then the cur-
rent context is ignored, and the name string is used as the namespace context.Extensions like Tk execute ordinary code fragments in the global names-
pace. A scoped value captures a code fragment together with its names-
pace context in a way that allows it to be executed properly later. Itis needed, for example, to wrap up code fragments when a Tk widget is
used within a namespace: namespace foo { private proc report {mesg} {puts "click: $mesg"
}button .b1 -text "Push Me" -command [code report "Hello
World!"]pack .b1 } The code fragment associated with button .b1 only makes
sense in the context of namespace "foo". Furthermore, the "report" procedure is private, and can only be accessed within that namespace.The ccooddee command wraps up the code fragment in a way that allows it to
be executed properly when the button is pressed. Also, note that the ccooddee command preserves the integrity of arguments on the command line. This makes it a natural replacement for the lliissttcommand, which is often used to format Tcl code fragments. In other
words, instead of using the lliisstt command like this: after 1000 [listputs "Hello $name!"] use the ccooddee command like this: after 1000 [code
puts "Hello $name!"] This not only formats the command correctly, but
also captures its namespace context.Scoped commands can be invoked like ordinary code fragments, with or
without the eevvaall command. For example, the following statements workproperly: set cmd {@scope ::foo .b1} $cmd configure -background red
set opts {-bg blue -fg white} eval $cmd configure $opts Note that
scoped commands by-pass the usual protection mechanisms; the command:
@scope ::foo {report {Hello World!}} can be used to access the"foo::report" proc from any namespace context, even though it is pri-
vate. KKEEYYWWOORRDDSS scope, callback, namespace, public, protected, privateitcl 3.0 code(n)