Importing proc variable into namespace
proc foo {param} {
namespace eval foo_ns {
uplevel {set foo_ns::x $param }
}
}
This just looks ugly.
[upvar] will not work, because it can't link to 'param'.
Thanks.
Code from answers does not work (tclsh8.4)
-------------------------------------------
% proc bar {param} {
namespace eval foo_ns {
uplevel [list set foo_ns::x $param]开发者_C百科
}
}
% bar 123
can't read "param": no such variable
-------------------------------------------
% proc foo {param} {
set ::foo_ns::x $param
}
% foo 123
can't set "::foo_ns::x": parent namespace doesn't exist
What is wrong with:
proc foo {param} {
set ::foo_ns::x $param
}
In my test, it seems to accomplish the same objective.
Update: Thanks To K0re for pointing this out. Before calling foo, you need to define the name space:
namespace eval ::foo_ns {}
Namespaces and levels are two different things. You don't need uplevel for this problem.
Here's a simple solution that creates the namespace and sets the variable in one line:
proc foo {param} {
namespace eval ::foo_ns [list set x $param]
}
Ok, you have two different problems. The first is that the namespace doesn't already exist; the second is that you need to write the code so that the variable is created/written in that namespace. Overall, this require only a tiny modification of Hai's code:
proc foo {param} {
# Create the namespace if it doesn't already exist
namespace eval ::foo_ns {}
# Set the variable in the namespace
set ::foo_ns::x $param
}
As commentary on some of the problems you were having:
proc foo {param} {
namespace eval foo_ns {
uplevel {set foo_ns::x $param }
}
}
This doesn't work because you are, effectively, saying the following: in the namespace "foo_ns", run the following code: at the top level of the stack, rung the following code: "set foo::x $param"
However, at the top level of the stack, the variable "param" has no value (its only defined within the procedure. You'll need to make sure it gets substitured beforehand. I'd include code that would work but, honestly, I'm afraid it will cause confusion with the actual answer to the question... so I'll leave it out.
精彩评论