Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k 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 Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.5k users

...