Tcl Source Code

View Ticket
Login
Ticket UUID: 1274918
Title: doc: upvar and qualified varNames
Type: Bug Created on: 2005-08-28 01:36:47
Submitter: msofer Assigned to: msofer
Subsystem: 07. Variables Severity:
Priority: 7 High Last modified: 2009-05-01 08:21:25
Status: Open Closed by:
Resolution: None Closed on:
Version: None
Description:
This supersedes Bug #959831.

There are two (related, important) issues that the
[upvar] docs do not describe: what it does when called
outside a proc body, and how it handles qualified
variable names.

The behaviour outside of proc bodies just needs
documenting: 
   upvar ?level? otherVar myVar ?otherVar myVar ...?
will create a variable named myVar in the current
namespace (assuming for now that myVar is not
qualified) linked to the variable otherVar as resolved
in the corresponding stack frame. This is the same
behaviour as documented in upvar.c, just replacing
"current procedure" by "current context". The fact that
this variable can never be unset until the namespace is
deleted deserves mention - and maybe a fix too?

The behaviour when otherVar is qualified is completely
unspecified in the manual page. This may not be a
problem, the behaviour is without surprises: the
variable that is linked to is otherVar as resolved in
the corresponding stack frame.

The behaviour when myVar is qualified is also
unspecified in the manual page, and is currently buggy
(Bug #1274916).
User Comments:
msofer added on 2006-08-26 02:45:32:
Logged In: YES 
user_id=148712

Note that the issue for qualified otherVar is not quite trivial:
% namespace eval a {}; namespace eval a::b {}; namespace
eval b {}
% set b::x ::b
::b
% namespace eval a {upvar 0 b::x w; set w}
::b
% set a::b::x ::a::b
::a::b
% namespace eval a {upvar 0 b::x w; set w}
::a::b

In the example above
  namespace eval a {upvar 0 b::x w}
the new variable should(?) be linked (in order) to:

  (1) ::a::b::x if the namespace ::a::b exists
  (2) ::b::x     if the namespace ::b     exists

Note that this refers to the existence of namespaces; the
usual resolution rules (and the actual upvar behaviour)
refer to the existence of the variables themselves, the
reference is to:

  (1) ::a::b::x if the variable ::a::b::x exists
  (2) ::b::x     if the variable ::b::x     exists
  (3) ::a::b::x otherwise (even if ::b exists but ::a::b
does not)

msofer added on 2006-08-26 09:26:37:
Logged In: YES 
user_id=148712

There is madness in here:
% namespace eval a {namespace eval b {}}
% namespace eval b {}
% set b::x 1
1
% namespace eval a {
   upvar 0 b::x s b::y t
   set s 2
   set t 3
}
3
% set b::x
2
% info vars b::*
::b::x
% info vars a::b::*
::a::b::y

Ie: [upvar 0 b::x s b::y t] creates vars linked to different
namespaces. This is definitely not the path of least surprise.