Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 23 |
Nodes: | 6 (0 / 6) |
Uptime: | 50:02:40 |
Calls: | 583 |
Files: | 1,138 |
Messages: | 111,305 |
Hi,
In the example code below, I try to access a proc and a variable in a namespace,
when using a variable for the namespace name itself.
Calling the namespace proc, or *updating* the namespace variable works ok. What bites me is how to *read* the namespace variable.
As shown in the code below, I can read the namespace variable by combining
it with the 'set' command.
My question is if there is another way to read the namespace variable.
# Read namespace variable using only string variable '$NS_NAME' - FAILS
puts "NS_NAME::myvar = ${NS_NAME}::myvar"
puts "::NS_NAME::myvar = ::${NS_NAME}::myvar"
Hi,
In the example code below, I try to access a proc and a variable in a namespace,
when using a variable for the namespace name itself.
Calling the namespace proc, or*updating* the namespace variable works ok. What bites me is how to*read* the namespace variable.
As shown in the code below, I can read the namespace variable by combining
it with the 'set' command.
My question is if there is another way to read the namespace variable.
Sample code:
# --------------------------------------------------------------------------------
# The example namespace
namespace eval myns {
variable myvar 42
proc myproc {} {
return "Hello world!"
}
}
# Call namespace proc using string literal 'myns'
puts "myns::myproc = [myns::myproc]"
puts "::myns::myproc = [::myns::myproc]"
# Read namespace variable using string literal 'myns'
puts "myns::myvar = $myns::myvar"
puts "::myns::myvar = $::myns::myvar"
set NS_NAME myns
Am 25.08.2025 um 20:56 schrieb johan.kuuse@gmail.com:
Hi,
In the example code below, I try to access a proc and a variable in a namespace,
when using a variable for the namespace name itself.
Calling the namespace proc, or*updating* the namespace variable works ok. What bites me is how to*read* the namespace variable.
As shown in the code below, I can read the namespace variable by combining it with the 'set' command.
My question is if there is another way to read the namespace variable.
Sample code:
# --------------------------------------------------------------------------------
# The example namespace
namespace eval myns {
variable myvar 42
proc myproc {} {
return "Hello world!"
}
}
# Call namespace proc using string literal 'myns'
puts "myns::myproc = [myns::myproc]"
puts "::myns::myproc = [::myns::myproc]"
# Read namespace variable using string literal 'myns'
puts "myns::myvar = $myns::myvar"
puts "::myns::myvar = $::myns::myvar"
set NS_NAME myns
What about this:
% set ::${NS_NAME}::myvar
42
% ::${NS_NAME}::myproc
Hello world!
To get access, you may also enjoy "namespace which":
% set varName [namespace eval $NS_NAME {namespace which -variable myvar}] ::myns::myvar
% set $varName
42
% set cmdName [namespace eval $NS_NAME {namespace which -command myproc}] ::myns::myproc
% $cmdName
Hello world!
Sorry, I did not understand all your examples, but this is IMHO
conformant to your question.
Take care,
Harald
# this works
% puts "::NS_NAME::myvar-a = [namespace eval ::$NS_NAME {set ::${NS_NAME}::myvar}]"
# this worksHere, there is no need for the eval:
% puts "::NS_NAME::myvar-a = [eval {set ::${NS_NAME}::myvar}]"
puts "myns::myvar = [set [namespace eval ::$NS_NAME {set ::${NS_NAME}::myvar}]"
puts "myns::myvar = [set [namespace eval $NS_NAME {namespace which -variable myvar}]]
My "real" question is if there is a "shorter" form for reading the variable.
It works, but it is very convoluted. Within the namespace there is no
need to use a fully qualified variable name. So you could just do:
Which in fact is one of the options the OP already mentioned.
Harald Oehlmann <wortkarg3@yahoo.com> posted:Well, you need two rounds of variable resolution.
Am 25.08.2025 um 20:56 schrieb johan.kuuse@gmail.com:
Hi,
In the example code below, I try to access a proc and a variable in a namespace,
when using a variable for the namespace name itself.
Calling the namespace proc, or*updating* the namespace variable works ok. >>> What bites me is how to*read* the namespace variable.
As shown in the code below, I can read the namespace variable by combining >>> it with the 'set' command.
My question is if there is another way to read the namespace variable.
Sample code:
# --------------------------------------------------------------------------------
# The example namespace
namespace eval myns {
variable myvar 42
proc myproc {} {
return "Hello world!"
}
}
# Call namespace proc using string literal 'myns'
puts "myns::myproc = [myns::myproc]"
puts "::myns::myproc = [::myns::myproc]"
# Read namespace variable using string literal 'myns'
puts "myns::myvar = $myns::myvar"
puts "::myns::myvar = $::myns::myvar"
set NS_NAME myns
What about this:
% set ::${NS_NAME}::myvar
42
% ::${NS_NAME}::myproc
Hello world!
To get access, you may also enjoy "namespace which":
% set varName [namespace eval $NS_NAME {namespace which -variable myvar}] >> ::myns::myvar
% set $varName
42
% set cmdName [namespace eval $NS_NAME {namespace which -command myproc}]
::myns::myproc
% $cmdName
Hello world!
Sorry, I did not understand all your examples, but this is IMHO
conformant to your question.
Take care,
Harald
Thank you both for your answers.
With my examples, I tried to show that *setting* a namespace variable can be done
using basically the same syntax with either using a string literal ('myns') or a variable ($NS_NAME):
set myns::myvar 42
set ${NS_NAME}::myvar 42
And *getting* a namespace variable when using a string literal ('myns') is also straight-forward:
puts "myns::myvar = $myns::myvar"
Anyhow, *getting* a namespace variable when using a variable ($NS_NAME)
seems to require either 'set', 'namespace eval' or 'namespace which' (or a combination):
puts "myns::myvar = [set ${NS_NAME}::myvar]"
puts "myns::myvar = [eval {set ::${NS_NAME}::myvar}]"
puts "myns::myvar = [set [namespace eval ::$NS_NAME {set ::${NS_NAME}::myvar}]"
puts "myns::myvar = [set [namespace eval $NS_NAME {namespace which -variable myvar}]]
My "real" question is if there is a "shorter" form for reading the variable.
BR,
Johan
Am 25.08.2025 um 22:34 schrieb johan.kuuse@gmail.com:
Harald Oehlmann <wortkarg3@yahoo.com> posted:
Am 25.08.2025 um 20:56 schrieb johan.kuuse@gmail.com:
Hi,
In the example code below, I try to access a proc and a variable in a namespace,
when using a variable for the namespace name itself.
Calling the namespace proc, or*updating* the namespace variable works ok. >>> What bites me is how to*read* the namespace variable.
As shown in the code below, I can read the namespace variable by combining
it with the 'set' command.
My question is if there is another way to read the namespace variable. >>>
Sample code:
# --------------------------------------------------------------------------------
# The example namespace
namespace eval myns {
variable myvar 42
proc myproc {} {
return "Hello world!"
}
}
# Call namespace proc using string literal 'myns'
puts "myns::myproc = [myns::myproc]"
puts "::myns::myproc = [::myns::myproc]"
# Read namespace variable using string literal 'myns'
puts "myns::myvar = $myns::myvar"
puts "::myns::myvar = $::myns::myvar"
set NS_NAME myns
What about this:
% set ::${NS_NAME}::myvar
42
% ::${NS_NAME}::myproc
Hello world!
To get access, you may also enjoy "namespace which":
% set varName [namespace eval $NS_NAME {namespace which -variable myvar}] >> ::myns::myvar
% set $varName
42
% set cmdName [namespace eval $NS_NAME {namespace which -command myproc}] >> ::myns::myproc
% $cmdName
Hello world!
Sorry, I did not understand all your examples, but this is IMHO
conformant to your question.
Take care,
Harald
Thank you both for your answers.
With my examples, I tried to show that *setting* a namespace variable can be done
using basically the same syntax with either using a string literal ('myns') or a variable ($NS_NAME):
set myns::myvar 42
set ${NS_NAME}::myvar 42
And *getting* a namespace variable when using a string literal ('myns') is also straight-forward:
puts "myns::myvar = $myns::myvar"
Anyhow, *getting* a namespace variable when using a variable ($NS_NAME) seems to require either 'set', 'namespace eval' or 'namespace which' (or a combination):
puts "myns::myvar = [set ${NS_NAME}::myvar]"
puts "myns::myvar = [eval {set ::${NS_NAME}::myvar}]"
puts "myns::myvar = [set [namespace eval ::$NS_NAME {set ::${NS_NAME}::myvar}]"
puts "myns::myvar = [set [namespace eval $NS_NAME {namespace which -variable myvar}]]
My "real" question is if there is a "shorter" form for reading the variable.
BR,Well, you need two rounds of variable resolution.
Johan
For me, the following two are my favorites:
If the variable should be accessed from the outside as value:
[set ${NS_NAME}::myvar]
If something should be done with the variable, this might be cleaner: namespace eval $NS_NAME {do something with $myvar}
You may also consider using an object with tcloo. THen, you have more options here.
Also, an access function is common.
Normally, you don't want to expose the inside of a namespace to the outside.If you "just" want a variable store, dicts and arrays may be helpful.
Take care,
Harald
Yes, you are right, I probably don't want to expose the inside of a namespace to the outside.
I have also learned that using a variable for namespace names may not
be such a brillant idea that I first thought of. Sticking to
hardcoded names makes the code cleaner, IMHO.
Harald Oehlmann <wortkarg3@yahoo.com> posted:
Am 25.08.2025 um 22:34 schrieb johan.kuuse@gmail.com:
Well, you need two rounds of variable resolution.
Harald Oehlmann <wortkarg3@yahoo.com> posted:
Am 25.08.2025 um 20:56 schrieb johan.kuuse@gmail.com:
Hi,
In the example code below, I try to access a proc and a variable in a namespace,
when using a variable for the namespace name itself.
Calling the namespace proc, or*updating* the namespace variable works ok. >>>>> What bites me is how to*read* the namespace variable.
As shown in the code below, I can read the namespace variable by combining
it with the 'set' command.
My question is if there is another way to read the namespace variable. >>>>>
Sample code:
# --------------------------------------------------------------------------------
# The example namespace
namespace eval myns {
variable myvar 42
proc myproc {} {
return "Hello world!"
}
}
# Call namespace proc using string literal 'myns'
puts "myns::myproc = [myns::myproc]"
puts "::myns::myproc = [::myns::myproc]"
# Read namespace variable using string literal 'myns'
puts "myns::myvar = $myns::myvar"
puts "::myns::myvar = $::myns::myvar"
set NS_NAME myns
What about this:
% set ::${NS_NAME}::myvar
42
% ::${NS_NAME}::myproc
Hello world!
To get access, you may also enjoy "namespace which":
% set varName [namespace eval $NS_NAME {namespace which -variable myvar}] >>>> ::myns::myvar
% set $varName
42
% set cmdName [namespace eval $NS_NAME {namespace which -command myproc}] >>>> ::myns::myproc
% $cmdName
Hello world!
Sorry, I did not understand all your examples, but this is IMHO
conformant to your question.
Take care,
Harald
Thank you both for your answers.
With my examples, I tried to show that *setting* a namespace variable can be done
using basically the same syntax with either using a string literal ('myns') >>> or a variable ($NS_NAME):
set myns::myvar 42
set ${NS_NAME}::myvar 42
And *getting* a namespace variable when using a string literal ('myns') is >>> also straight-forward:
puts "myns::myvar = $myns::myvar"
Anyhow, *getting* a namespace variable when using a variable ($NS_NAME)
seems to require either 'set', 'namespace eval' or 'namespace which' (or a combination):
puts "myns::myvar = [set ${NS_NAME}::myvar]"
puts "myns::myvar = [eval {set ::${NS_NAME}::myvar}]"
puts "myns::myvar = [set [namespace eval ::$NS_NAME {set ::${NS_NAME}::myvar}]"
puts "myns::myvar = [set [namespace eval $NS_NAME {namespace which -variable myvar}]]
My "real" question is if there is a "shorter" form for reading the variable.
BR,
Johan
For me, the following two are my favorites:
If the variable should be accessed from the outside as value:
[set ${NS_NAME}::myvar]
If something should be done with the variable, this might be cleaner:
namespace eval $NS_NAME {do something with $myvar}
You may also consider using an object with tcloo. THen, you have more
options here.
Also, an access function is common.
Normally, you don't want to expose the inside of a namespace to the
outside.If you "just" want a variable store, dicts and arrays may be
helpful.
Take care,
Harald
Yes, you are right, I probably don't want to expose the inside of a namespace to the outside.
I have also learned that using a variable for namespace names may not be such a brillant idea that I first thought of.
Sticking to hardcoded names makes the code cleaner, IMHO.
Thanks again for all input.