1local(n) [incr Tcl] local(n)
2
3
4
5______________________________________________________________________________
6
8 itcl::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
34 itcl::class counter {
35 private variable count 0
36 constructor {} {
37 puts "created: $this"
38 }
39 destructor {
40 puts "deleted: $this"
41 }
42
43 method bump {{by 1}} {
44 incr count $by
45 }
46 method get {} {
47 return $count
48 }
49 }
50
51 proc test {val} {
52 local counter x
53 for {set i 0} {$i < $val} {incr i} {
54 x bump
55 }
56 return [x get]
57 }
58
59 set result [test 5]
60 puts "test: $result"
61
62 set result [test 10]
63 puts "test: $result"
64
65 puts "objects: [itcl::find objects *]"
66
67
69 class, object, procedure
70
71
72
73itcl local(n)