1local(n) [incr Tcl] local(n)
2
3
4
5______________________________________________________________________________
6
8 local - create an object local to a procedure
9
11 itcl::local className objName ?arg arg ...?
12_________________________________________________________________
13
14
16 The local command creates an [incr Tcl] object that is local to the
17 current call frame. When the call frame goes away, the object is auto‐
18 matically deleted. This command is useful for creating objects that
19 are local to a procedure.
20
21 As a side effect, this command creates a variable named "itcl-local-
22 xxx", where xxx is the name of the object that is created. This vari‐
23 able detects when the call frame is destroyed and automatically deletes
24 the associated object.
25
26
28 In the following example, a simple "counter" object is used within the
29 procedure "test". The counter is created as a local object, so it is
30 automatically deleted each time the procedure exits. The puts state‐
31 ments included in the constructor/destructor show the object coming and
32 going as the procedure is called.
33 itcl::class counter {
34 private variable count 0
35 constructor {} {
36 puts "created: $this"
37 }
38 destructor {
39 puts "deleted: $this"
40 }
41
42 method bump {{by 1}} {
43 incr count $by
44 }
45 method get {} {
46 return $count
47 }
48 }
49
50 proc test {val} {
51 local counter x
52 for {set i 0} {$i < $val} {incr i} {
53 x bump
54 }
55 return [x get]
56 }
57
58 set result [test 5]
59 puts "test: $result"
60
61 set result [test 10]
62 puts "test: $result"
63
64 puts "objects: [itcl::find objects *]"
65
66
68 class, object, procedure
69
70
71
72itcl local(n)