At Sat, 10 Jun 23 15:33:08 GMT [email protected] wrote:
I want to use a variable in a namespace (containing configuration information) in
several other namespaces. While the man page says namespace upvar
works within proceedures, it appears to work outside them as well:
namespace eval n1 {set v(a) aaa}
namespace eval n2 {namespace upvar ::n1 v v}
set n2::v(a)
returns aaa
Is there a simpler way to do this that I'm missing, or should the man page >> for upvar mention it works outside proc definitions as well as inside them?
You don't reallyto use namespace upvar:
namespace eval n1 {}
set n1::v(a) aaa
set n1::v(a)
returns aaa
I want to use a variable in a namespace (containing configuration information) in
several other namespaces. While the man page says namespace upvar
works within proceedures, it appears to work outside them as well:
namespace eval n1 {set v(a) aaa}
namespace eval n2 {namespace upvar ::n1 v v}
set n2::v(a)
returns aaa
Is there a simpler way to do this that I'm missing, or should the man page for upvar mention it works outside proc definitions as well as inside them?
Dave B
On 6/10/2023 9:15 AM, Andreas Leitgeb wrote:
While not the official documentation, in Ashok'sAt Sat, 10 Jun 23 15:33:08 GMT [email protected] wrote:
I want to use a variable in a namespace (containing configuration information) in
several other namespaces. While the man page says namespace upvar
works within proceedures, it appears to work outside them as well:
namespace eval n1 {set v(a) aaa}
namespace eval n2 {namespace upvar ::n1 v v}
set n2::v(a)
returns aaa
Is there a simpler way to do this that I'm missing, or should the man page >>>> for upvar mention it works outside proc definitions as well as inside them?
Maybe it was designed merely for procedures, and the effect seen
here is just an "accident" ...
If that is *not* the case, the wording in the docs ought to be changed.
Robert Heller <[email protected]> wrote:
You don't reallyto use namespace upvar:
namespace eval n1 {}
set n1::v(a) aaa
set n1::v(a)
returns aaa
Robert, I think you mis-read the question.
Dave requested the value of n2::v(a), not n1::v(a)
Tcl book, section 12.5.1.3 on namespace upvar, he says,
"When the command is invoked outside a procedure,
LOCALVAR names a namespace variable in the namespace
in which the command is invoked."
And then he provides the examples to back that up.
Also, he uses
namespace upvar NAMESPACE ?NSVAR LOCALVAR …?
as his template.
One statement in the manual that seems to indicate this is,
The command *namespace upvar $ns a b* has the same behaviour as *upvar 0 ${ns}::a b*
But that is confusing to me, since I don't know what 0 is for a level, while #0 is
used to mean global level. Did they leave off the # also?
At Sat, 10 Jun 23 15:33:08 GMT [email protected] wrote:
I want to use a variable in a namespace (containing configuration information) in
several other namespaces. While the man page says namespace upvar
works within proceedures, it appears to work outside them as well:
namespace eval n1 {set v(a) aaa}
namespace eval n2 {namespace upvar ::n1 v v}
set n2::v(a)
returns aaa
Is there a simpler way to do this that I'm missing, or should the man page >>> for upvar mention it works outside proc definitions as well as inside them?
Maybe it was designed merely for procedures, and the effect seen
here is just an "accident" ...
If that is *not* the case, the wording in the docs ought to be changed.
Robert Heller <[email protected]> wrote:
You don't reallyto use namespace upvar:
namespace eval n1 {}
set n1::v(a) aaa
set n1::v(a)
returns aaa
Robert, I think you mis-read the question.
Dave requested the value of n2::v(a), not n1::v(a)
Wait, I think a level of 0 means the current level, since:
% proc foo {} {set b bbb; upvar 0 b b2; puts $b; puts $b2}
% foo
bbb
bbb
seems to allow for an alias in the same proc, so maybe that
manual is correct, though I couldn't find anywhere that they
explicitly talked about level 0.
seems to allow for an alias in the same proc, so maybe that
manual is correct, though I couldn't find anywhere that they
explicitly talked about level 0.
Is there a simpler way to do this that I'm missing, or should the man page for upvar mention it works outside proc definitions as well as inside them?
Dave B
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 144:02:57 |
| Calls: | 12,089 |
| Calls today: | 2 |
| Files: | 15,000 |
| Messages: | 6,517,483 |