1Tcl_SetVar(3)               Tcl Library Procedures               Tcl_SetVar(3)
2
3
4
5______________________________________________________________________________
6

NAME

8       Tcl_SetVar2Ex,  Tcl_SetVar, Tcl_SetVar2, Tcl_ObjSetVar2, Tcl_GetVar2Ex,
9       Tcl_GetVar, Tcl_GetVar2, Tcl_ObjGetVar2, Tcl_UnsetVar, Tcl_UnsetVar2  -
10       manipulate Tcl variables
11

SYNOPSIS

13       #include <tcl.h>
14
15       Tcl_Obj *                                                               │
16       Tcl_SetVar2Ex(interp, name1, name2, newValuePtr, flags)                 │
17
18       CONST char *
19       Tcl_SetVar(interp, varName, newValue, flags)
20
21       CONST char *
22       Tcl_SetVar2(interp, name1, name2, newValue, flags)
23
24       Tcl_Obj *
25       Tcl_ObjSetVar2(interp, part1Ptr, part2Ptr, newValuePtr, flags)
26
27       Tcl_Obj *                                                               │
28       Tcl_GetVar2Ex(interp, name1, name2, flags)                              │
29
30       CONST char *
31       Tcl_GetVar(interp, varName, flags)
32
33       CONST char *
34       Tcl_GetVar2(interp, name1, name2, flags)
35
36       Tcl_Obj *
37       Tcl_ObjGetVar2(interp, part1Ptr, part2Ptr, flags)
38
39       int
40       Tcl_UnsetVar(interp, varName, flags)
41
42       int
43       Tcl_UnsetVar2(interp, name1, name2, flags)
44

ARGUMENTS

46       Tcl_Interp   *interp        (in)      Interpreter containing variable.
47
48       CONST char   *name1         (in)      Contains  the  name  of  an array
49                                             variable (if name2  is  non-NULL)
50                                             or  (if name2 is NULL) either the
51                                             name of a scalar  variable  or  a
52                                             complete   name   including  both
53                                             variable  name  and  index.   May
54                                             include  ::  namespace qualifiers
55                                             to specify a variable in  a  par‐
56                                             ticular namespace.
57
58       CONST char   *name2         (in)      If  non-NULL,  gives name of ele‐
59                                             ment within array; in  this  case
60                                             name1  must  refer  to  an  array
61                                             variable.
62
63       Tcl_Obj      *newValuePtr   (in)      Points to a Tcl object containing │
64                                             the new value for the variable.
65
66       int          flags          (in)      OR-ed combination of bits provid‐
67                                             ing additional  information.  See
68                                             below for valid values.
69
70       CONST char   *varName       (in)      Name of variable.  May include ::
71                                             namespace qualifiers to specify a
72                                             variable  in  a particular names‐
73                                             pace.   May  refer  to  a  scalar
74                                             variable  or  an  element  of  an
75                                             array.
76
77       CONST char   *newValue      (in)      New value for variable, specified
78                                             as  a  null-terminated string.  A
79                                             copy of this value is  stored  in
80                                             the variable.
81
82       Tcl_Obj      *part1Ptr      (in)      Points to a Tcl object containing
83                                             the variable's  name.   The  name
84                                             may include a series of :: names‐
85                                             pace  qualifiers  to  specify   a
86                                             variable  in  a particular names‐
87                                             pace.   May  refer  to  a  scalar
88                                             variable  or  an  element  of  an
89                                             array variable.
90
91       Tcl_Obj      *part2Ptr      (in)      If non-NULL, points to an  object
92                                             containing the name of an element
93                                             within an array and part1Ptr must
94                                             refer to an array variable.
95_________________________________________________________________
96
97

DESCRIPTION

99       These procedures are used to create, modify, read, and delete Tcl vari‐
100       ables from C code.
101
102       Tcl_SetVar2Ex, Tcl_SetVar, Tcl_SetVar2, and Tcl_ObjSetVar2 will  create │
103       a  new  variable  or  modify an existing one.  These procedures set the │
104       given variable to the value given by newValuePtr or newValue and return │
105       a  pointer  to the variable's new value, which is stored in Tcl's vari‐ │
106       able structure.  Tcl_SetVar2Ex and Tcl_ObjSetVar2 take the new value as │
107       a  Tcl_Obj  and return a pointer to a Tcl_Obj.  Tcl_SetVar and Tcl_Set‐ 
108       Var2 take the new value as a string and return a string; they are  usu‐ │
109       ally  less  efficient  than Tcl_ObjSetVar2.  Note that the return value │
110       may be different than the newValuePtr or newValue argument, due to mod‐
111       ifications  made  by  write  traces.  If an error occurs in setting the
112       variable (e.g. an array variable is referenced without giving an  index
113       into  the  array)  NULL  is  returned  and  an error message is left in
114       interp's result if the TCL_LEAVE_ERR_MSG flag bit is set.
115
116       Tcl_GetVar2Ex, Tcl_GetVar, Tcl_GetVar2, and Tcl_ObjGetVar2  return  the │
117       current  value  of  a  variable.  The arguments to these procedures are │
118       treated in the same way as the arguments to  the  procedures  described │
119       above.   Under  normal  circumstances, the return value is a pointer to │
120       the variable's value.  For Tcl_GetVar2Ex and Tcl_ObjGetVar2  the  value │
121       is  returned as a pointer to a Tcl_Obj.  For Tcl_GetVar and Tcl_GetVar2 
122       the value is returned as a string; this is usually less  efficient,  so │
123       Tcl_GetVar2Ex  or  Tcl_ObjGetVar2  are  preferred.   If an error occurs
124       while reading the variable (e.g. the variable doesn't exist or an array
125       element  is specified for a scalar variable), then NULL is returned and
126       an error message is left in interp's result  if  the  TCL_LEAVE_ERR_MSG
127       flag bit is set.
128
129       Tcl_UnsetVar  and  Tcl_UnsetVar2  may  be used to remove a variable, so
130       that future attempts to read the variable will return  an  error.   The
131       arguments  to these procedures are treated in the same way as the argu‐
132       ments to the procedures above.  If the variable is successfully removed
133       then  TCL_OK is returned.  If the variable cannot be removed because it
134       doesn't exist then TCL_ERROR is returned and an error message  is  left
135       in  interp's  result  if  the TCL_LEAVE_ERR_MSG flag bit is set.  If an
136       array element is specified, the given element is removed but the  array
137       remains.   If  an  array  name  is specified without an index, then the
138       entire array is removed.
139
140       The name of a variable may be specified to  these  procedures  in  four
141       ways:
142
143       [1]    If Tcl_SetVar, Tcl_GetVar, or Tcl_UnsetVar is invoked, the vari‐
144              able name is given as a single string, varName.  If varName con‐
145              tains  an  open  parenthesis  and ends with a close parenthesis,
146              then the value between the parentheses is treated  as  an  index
147              (which  can have any string value) and the characters before the
148              first open parenthesis are treated as the name of an array vari‐
149              able.   If  varName doesn't have parentheses as described above,
150              then the entire string is treated as the name of a scalar  vari‐
151              able.
152
153       [2]    If  the name1 and name2 arguments are provided and name2 is non-
154              NULL, then an array element is specified and the array name  and
155              index  have already been separated by the caller: name1 contains
156              the name and name2 contains the index.  An error is generated if │
157              name1  contains an open parenthesis and ends with a close paren‐ │
158              thesis (array element) and name2 is non-NULL.                    │
159
160       [3]                                                                     │
161              If name2 is NULL, name1 is treated just like varName in case [1] │
162              above  (it  can  be either a scalar or an array element variable │
163              name).
164
165       The flags argument may be used to specify any of several options to the
166       procedures.  It consists of an OR-ed combination of the following bits.
167
168       TCL_GLOBAL_ONLY
169              Under  normal  circumstances the procedures look up variables as
170              follows.  If a procedure call is active in interp, the  variable
171              is looked up at the current level of procedure call.  Otherwise,
172              the variable is looked up first in the current  namespace,  then
173              in  the  global namespace.  However, if this bit is set in flags
174              then the variable is looked up only in the global namespace even
175              if  there  is  a procedure call active.  If both TCL_GLOBAL_ONLY
176              and TCL_NAMESPACE_ONLY are given, TCL_GLOBAL_ONLY is ignored.
177
178       TCL_NAMESPACE_ONLY
179              If this bit is set in flags then the variable is looked up  only
180              in the current namespace; if a procedure is active its variables
181              are ignored, and the global namespace is also ignored unless  it
182              is the current namespace.
183
184       TCL_LEAVE_ERR_MSG
185              If  an  error  is returned and this bit is set in flags, then an
186              error message will be left in the interpreter's result, where it
187              can  be  retrieved with Tcl_GetObjResult or Tcl_GetStringResult.
188              If this flag bit isn't set then no error message is left and the
189              interpreter's result will not be modified.
190
191       TCL_APPEND_VALUE
192              If  this  bit is set then newValuePtr or newValue is appended to
193              the current value instead of replacing it.  If the  variable  is
194              currently  undefined, then the bit is ignored.  This bit is only
195              used by the Tcl_Set* procedures.
196
197       TCL_LIST_ELEMENT
198              If this bit is set, then newValue is converted to  a  valid  Tcl
199              list  element  before setting (or appending to) the variable.  A
200              separator space is appended before the new list  element  unless
201              the  list  element is going to be the first element in a list or
202              sublist (i.e. the variable's current value is empty, or contains
203              the single character ``{'', or ends in `` }'').  When appending,
204              the original value of the variable must also be a valid list, so
205              that the operation is the appending of a new list element onto a
206              list.
207
208       Tcl_GetVar and Tcl_GetVar2 return the current value of a variable.  The
209       arguments  to these procedures are treated in the same way as the argu‐
210       ments to Tcl_SetVar and Tcl_SetVar2.  Under normal  circumstances,  the
211       return  value  is a pointer to the variable's value (which is stored in
212       Tcl's variable structure and will not change before the  next  call  to
213       Tcl_SetVar  or  Tcl_SetVar2).   Tcl_GetVar and Tcl_GetVar2 use the flag
214       bits TCL_GLOBAL_ONLY and TCL_LEAVE_ERR_MSG, both of which have the same
215       meaning  as for Tcl_SetVar.  If an error occurs in reading the variable
216       (e.g. the variable doesn't exist or an array element is specified for a
217       scalar variable), then NULL is returned.
218
219       Tcl_UnsetVar  and  Tcl_UnsetVar2  may  be used to remove a variable, so
220       that future calls to Tcl_GetVar or Tcl_GetVar2 for  the  variable  will
221       return  an error.  The arguments to these procedures are treated in the
222       same way as the arguments to Tcl_GetVar and Tcl_GetVar2.  If the  vari‐
223       able  is successfully removed then TCL_OK is returned.  If the variable
224       cannot be removed because it doesn't exist then TCL_ERROR is  returned.
225       If  an array element is specified, the given element is removed but the
226       array remains.  If an array name is specified without  an  index,  then
227       the entire array is removed.
228
229

SEE ALSO

231       Tcl_GetObjResult, Tcl_GetStringResult, Tcl_TraceVar
232
233

KEYWORDS

235       array, get variable, interpreter, object, scalar, set, unset, variable
236
237
238
239Tcl                                   8.1                        Tcl_SetVar(3)
Impressum