summaryrefslogtreecommitdiff
path: root/itcl/itcl/doc/local.n
blob: ebc527c5ab374ba32ea27a64b04252e6bb56a5ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'\"
'\" Copyright (c) 1993-1998  Lucent Technologies, Inc.
'\"
'\" See the file "license.terms" for information on usage and redistribution
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
'\"
'\" RCS: $Id$
'\"
.so man.macros
.TH local n "" itcl "[incr\ Tcl]"
.BS
'\" Note:  do not modify the .SH NAME line immediately below!
.SH NAME
local \- create an object local to a procedure
.SH SYNOPSIS
\fBitcl::local \fIclassName objName\fR ?\fIarg arg ...\fR?
.BE

.SH DESCRIPTION
.PP
The \fBlocal\fR command creates an \fB[incr\ Tcl]\fR object that
is local to the current call frame.  When the call frame goes away,
the object is automatically deleted.  This command is useful for
creating objects that are local to a procedure.
.PP
As a side effect, this command creates a variable named
"\fCitcl-local-\fIxxx\fR", where \fIxxx\fR is the name of
the object that is created.  This variable detects when the
call frame is destroyed and automatically deletes the
associated object.

.SH EXAMPLE
In the following example, a simple "counter" object is used
within the procedure "test".  The counter is created as a
local object, so it is automatically deleted each time the
procedure exits.  The \fBputs\fR statements included in the
constructor/destructor show the object coming and going
as the procedure is called.
.CS
itcl::class counter {
    private variable count 0
    constructor {} {
        puts "created: $this"
    }
    destructor {
        puts "deleted: $this"
    }

    method bump {{by 1}} {
        incr count $by
    }
    method get {} {
        return $count
    }
}

proc test {val} {
    local counter x
    for {set i 0} {$i < $val} {incr i} {
        x bump
    }
    return [x get]
}

set result [test 5]
puts "test: $result"

set result [test 10]
puts "test: $result"

puts "objects: [itcl::find objects *]"
.CE

.SH KEYWORDS
class, object, procedure