1code(n)                           [incr Tcl]                           code(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       itcl::code - capture the namespace context for a code fragment
9

SYNOPSIS

11       itcl::code ?-namespace name? command ?arg arg ...?
12______________________________________________________________________________
13
14

DESCRIPTION

16       Creates a scoped value for the specified command and its associated arg
17       arguments.  A scoped value is a list with three elements:  the "@scope"
18       keyword,  a  namespace  context,  and a value string.  For example, the
19       command
20              namespace foo {
21                  code puts "Hello World!"
22              }
23       produces the scoped value:
24              @scope ::foo {puts {Hello World!}}
25       Note that the code command captures the current namespace context.   If
26       the  -namespace flag is specified, then the current context is ignored,
27       and the name string is used as the namespace context.
28
29       Extensions like Tk execute ordinary code fragments in the global names‐
30       pace.  A scoped value captures a code fragment together with its names‐
31       pace context in a way that allows it to be executed properly later.  It
32       is  needed,  for example, to wrap up code fragments when a Tk widget is
33       used within a namespace:
34              namespace foo {
35                  private proc report {mesg} {
36                      puts "click: $mesg"
37                  }
38
39                  button .b1 -text "Push Me"         -command [code report "Hello World!"]
40                  pack .b1
41              }
42       The code fragment associated with button .b1 only makes  sense  in  the
43       context  of  namespace  "foo".   Furthermore, the "report" procedure is
44       private, and can only be accessed within that namespace.  The code com‐
45       mand  wraps up the code fragment in a way that allows it to be executed
46       properly when the button is pressed.
47
48       Also, note that the code command preserves the integrity  of  arguments
49       on  the command line.  This makes it a natural replacement for the list
50       command, which is often used to format Tcl code  fragments.   In  other
51       words, instead of using the list command like this:
52              after 1000 [list puts "Hello $name!"]
53       use the code command like this:
54              after 1000 [code puts "Hello $name!"]
55       This  not  only  formats  the  command correctly, but also captures its
56       namespace context.
57
58       Scoped commands can be invoked like ordinary code  fragments,  with  or
59       without  the  eval command.  For example, the following statements work
60       properly:
61              set cmd {@scope ::foo .b1}
62              $cmd configure -background red
63
64              set opts {-bg blue -fg white}
65              eval $cmd configure $opts
66       Note that scoped commands by-pass the usual protection mechanisms;  the
67       command:
68              @scope ::foo {report {Hello World!}}
69       can  be  used  to access the "foo::report" proc from any namespace con‐
70       text, even though it is private.
71
72

KEYWORDS

74       scope, callback, namespace, public, protected, private
75
76
77
78itcl                                  3.0                              code(n)
Impressum