Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

TCL: using a namespace name variable

I create a namespace where the name is contained in one variable:

set fg_path fg_$path
namespace eval $fg_path {
    variable Elem 17
    puts "--> [namespace current]"
}

But I am not able to access the Elem value or to set it, for example this doesn't works: set ::$fg_path::Elem [dict create]

Thanks for any suggestion. John


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to be careful when assembling the qualified variable name:

set ::${fg_path}::Elem [dict create]

Note the curly braces { and } which protect the variable fg_path for variable substitution. Without them, you would ask Tcl to run substitution for a variable named fg_path::Elem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...