variable.n
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:3k
- '"
- '" Copyright (c) 1993-1997 Bell Labs Innovations for Lucent Technologies
- '" Copyright (c) 1997 Sun Microsystems, Inc.
- '"
- '" See the file "license.terms" for information on usage and redistribution
- '" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
- '"
- '" RCS: @(#) $Id: variable.n,v 1.4.18.2 2005/02/16 18:53:02 msofer Exp $
- '"
- .so man.macros
- .TH variable n 8.0 Tcl "Tcl Built-In Commands"
- .BS
- '" Note: do not modify the .SH NAME line immediately below!
- .SH NAME
- variable - create and initialize a namespace variable
- .SH SYNOPSIS
- fBvariable fR?fIname value...fR? fIname fR?fIvaluefR?
- .BE
- .SH DESCRIPTION
- .PP
- This command is normally used within a
- fBnamespace evalfR command to create one or more variables
- within a namespace.
- Each variable fInamefR is initialized with fIvaluefR.
- The fIvaluefR for the last variable is optional.
- .PP
- If a variable fInamefR does not exist, it is created.
- In this case, if fIvaluefR is specified,
- it is assigned to the newly created variable.
- If no fIvaluefR is specified, the new variable is left undefined.
- If the variable already exists,
- it is set to fIvaluefR if fIvaluefR is specified
- or left unchanged if no fIvaluefR is given.
- Normally, fInamefR is unqualified
- (does not include the names of any containing namespaces),
- and the variable is created in the current namespace.
- If fInamefR includes any namespace qualifiers,
- the variable is created in the specified namespace. If the variable
- is not defined, it will be visible to the fBnamespace whichfR
- command, but not to the fBinfo existsfR command.
- .PP
- If the fBvariablefR command is executed inside a Tcl procedure,
- it creates local variables
- linked to the corresponding namespace variables (and therefore these
- variables are listed by fBinfo varsfR.)
- In this way the fBvariablefR command resembles the fBglobalfR command,
- although the fBglobalfR command
- only links to variables in the global namespace.
- If any fIvaluefRs are given,
- they are used to modify the values of the associated namespace variables.
- If a namespace variable does not exist,
- it is created and optionally initialized.
- .PP
- A fInamefR argument cannot reference an element within an array.
- Instead, fInamefR should reference the entire array,
- and the initialization fIvaluefR should be left off.
- After the variable has been declared,
- elements within the array can be set using ordinary
- fBsetfR or fBarrayfR commands.
- .SH EXAMPLES
- Create a variable in a namespace:
- .CS
- namespace eval foo {
- fBvariablefR bar 12345
- }
- .CE
- .PP
- Create an array in a namespace:
- .CS
- namespace eval someNS {
- fBvariablefR someAry
- array set someAry {
- someName someValue
- otherName otherValue
- }
- }
- .CE
- .PP
- Access variables in namespaces from a procedure:
- .CS
- namespace eval foo {
- proc spong {} {
- # Variable in this namespace
- fBvariablefR bar
- puts "bar is $bar"
- # Variable in another namespace
- fBvariablefR ::someNS::someAry
- parray someAry
- }
- }
- .CE
- .SH "SEE ALSO"
- global(n), namespace(n), upvar(n)
- .SH KEYWORDS
- global, namespace, procedure, variable