I have two question about namespace in Tcl.
namespace eval ::dai {
set a 5
set b 10
namespace export *
}
My questions are:
-
export *– the export will make some variable inside this namespace can be used in other namespace, but what does thisexport *mean? -
Set a 5, should not we use
variable a 5? are they the same? some tutorials say inside namespace, we should usevariable, what is the difference betweenvariableandsetin namespace?
1) As is (supposed to be) logical for Unix users, “*” means “everything available at the moment”. It’s like when you do
rm -f *in a shell, the shell expands “*” and replaces it with a list of all the files present in the current directory. Actually, asnamespacemanual states you can specify more elaborate patters than simple “*”. To learn what that “glob-style” thing mentioned there means read aboutstring match.2) The question “should not we use…” is incorrect because it depends on what you want to do. If you want to declare a variable located in the namespace, use
variable. If you want to set a variable, useset, but bevare that if that variablexis not exist in the namespace yet, Tcl will attempt to find a global variablte with this name, see:This is explained in the “NAME RESOLUTION” section of the
namespaceman page.Note that this behaviour may appear illogical, but it actually matches that of the procedure scope: if you do
set foo barin a procedure body, this means setting the local variable unless you stated otherwise using eitherglobalorvariableor by using a fully-qualified name (like::ns::foo).