⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 namespace.n

📁 linux系统下的音频通信
💻 N
📖 第 1 页 / 共 2 页
字号:
}namespace eval Counter {    proc test {args} {        return $args    }}namespace eval Counter {    rename test ""}\fR.CENote that the \fBtest\fR procedure is added to the \fBCounter\fR namespace,and later removed via the \fBrename\fR command..PPNamespaces can have other namespaces within them,so they nest hierarchically.A nested namespace is encapsulated inside its parent namespaceand can not interfere with other namespaces..SH "QUALIFIED NAMES".PPEach namespace has a textual name such as\fBhistory\fR or \fB::safe::interp\fR.Since namespaces may nest,qualified names are used to refer tocommands, variables, and child namespaces contained inside namespaces.Qualified names are similar to the hierarchical path names forUnix files or Tk widgets,except that \fB::\fR is used as the separatorinstead of \fB/\fR or \fB.\fR.The topmost or global namespace has the name ``'' (i.e., an empty string),although \fB::\fR is a synonym.As an example, the name \fB::safe::interp::create\fRrefers to the command \fBcreate\fR in the namespace \fBinterp\fRthat is a child of of namespace \fB::safe\fR,which in turn is a child of the global namespace \fB::\fR..PPIf you want to access commands and variables from another namespace,you must use some extra syntax.Names must be qualified by the namespace that contains them.From the global namespace,we might access the \fBCounter\fR procedures like this:.CS\fBCounter::Bump 5Counter::Reset\fR.CEWe could access the current count like this:.CS\fBputs "count = $Counter::num"\fR.CEWhen one namespace contains another, you may need more than onequalifier to reach its elements.If we had a namespace \fBFoo\fR that contained the namespace \fBCounter\fR,you could invoke its \fBBump\fR procedurefrom the global namespace like this:.CS\fBFoo::Counter::Bump 3\fR.CE.PPYou can also use qualified names when you create and rename commands.For example, you could add a procedure to the \fBFoo\fRnamespace like this:.CS\fBproc Foo::Test {args} {return $args}\fR.CEAnd you could move the same procedure to another namespace like this:.CS\fBrename Foo::Test Bar::Test\fR.CE.PPThere are a few remaining points about qualified namesthat we should cover.Namespaces have nonempty names except for the global namespace.\fB::\fR is disallowed in simple command, variable, and namespace namesexcept as a namespace separator.Extra \fB:\fRs in a qualified name are ignored;that is, two or more \fB:\fRs are treated as a namespace separator.A trailing \fB::\fR in a qualified variable or command namerefers to the variable or command named {}.However, a trailing \fB::\fR in a qualified namespace name is ignored..SH "NAME RESOLUTION".PPIn general, all Tcl commands that take variable and command namessupport qualified names.This means you can give qualified names to such commands as\fBset\fR, \fBproc\fR, \fBrename\fR, and \fBinterp alias\fR.If you provide a fully-qualified name that starts with a \fB::\fR,there is no question about what command, variable, or namespaceyou mean.However, if the name does not start with a \fB::\fR(i.e., is \fIrelative\fR), Tcl follows a fixed rule for looking it up:Command and variable names are always resolvedby looking first in the current namespace,and then in the global namespace.Namespace names, on the other hand, are always resolvedby looking in only the current namespace..PPIn the following example,.CS\fBset traceLevel 0namespace eval Debug {    printTrace $traceLevel}\fR.CETcl looks for \fBtraceLevel\fR in the namespace \fBDebug\fRand then in the global namespace.It looks up the command \fBprintTrace\fR in the same way.If a variable or command name is not found in either context,the name is undefined.To make this point absolutely clear, consider the following example:.CS\fBset traceLevel 0namespace eval Foo {    variable traceLevel 3    namespace eval Debug {        printTrace $traceLevel    }}\fR.CEHere Tcl looks for \fBtraceLevel\fR first in the namespace \fBFoo::Debug\fR.Since it is not found there, Tcl then looks for it in the global namespace.The variable \fBFoo::traceLevel\fR is completely ignoredduring the name resolution process..PPYou can use the \fBnamespace which\fR command to clear up any questionabout name resolution.For example, the command:.CS\fBnamespace eval Foo::Debug {namespace which \-variable traceLevel}\fR.CEreturns \fB::traceLevel\fR.On the other hand, the command,.CS\fBnamespace eval Foo {namespace which \-variable traceLevel}\fR.CEreturns \fB::Foo::traceLevel\fR..PPAs mentioned above,namespace names are looked up differentlythan the names of variables and commands.Namespace names are always resolved in the current namespace.This means, for example,that a \fBnamespace eval\fR command that creates a new namespacealways creates a child of the current namespaceunless the new namespace name begins with a \fB::\fR..PPTcl has no access control to limit what variables, commands,or namespaces you can reference.If you provide a qualified name that resolves to an elementby the name resolution rule above,you can access the element..PPYou can access a namespace variablefrom a procedure in the same namespaceby using the \fBvariable\fR command.Much like the \fBglobal\fR command,this creates a local link to the namespace variable.If necessary, it also creates the variable in the current namespaceand initializes it.Note that the \fBglobal\fR command only creates linksto variables in the global namespace.It is not necessary to use a \fBvariable\fR commandif you always refer to the namespace variable using anappropriate qualified name..SH "IMPORTING COMMANDS".PPNamespaces are often used to represent libraries.Some library commands are used so frequentlythat it is a nuisance to type their qualified names.For example, suppose that all of the commands in a packagelike BLT are contained in a namespace called \fBBlt\fR.Then you might access these commands like this:.CS\fBBlt::graph .g \-background redBlt::table . .g 0,0\fR.CEIf you use the \fBgraph\fR and \fBtable\fR commands frequently,you may want to access them without the \fBBlt::\fR prefix.You can do this by importing the commands into the current namespace,like this:.CS\fBnamespace import Blt::*\fR.CEThis adds all exported commands from the \fBBlt\fR namespaceinto the current namespace context, so you can write code like this:.CS\fBgraph .g \-background redtable . .g 0,0\fR.CEThe \fBnamespace import\fR command only imports commandsfrom a namespace that that namespace exportedwith a \fBnamespace export\fR command..PPImporting \fIevery\fR command from a namespace is generallya bad idea since you don't know what you will get.It is better to import just the specific commands you need.For example, the command.CS\fBnamespace import Blt::graph Blt::table\fR.CEimports only the \fBgraph\fR and \fBtable\fR commands into thecurrent context..PPIf you try to import a command that already exists, you will get anerror.  This prevents you from importing the same command from twodifferent packages.  But from time to time (perhaps when debugging),you may want to get around this restriction.  You may want toreissue the \fBnamespace import\fR command to pick up new commandsthat have appeared in a namespace.  In that case, you can use the\fB\-force\fR option, and existing commands will be silently overwritten:.CS\fBnamespace import \-force Blt::graph Blt::table\fR.CEIf for some reason, you want to stop using the imported commands,you can remove them with an \fBnamespace forget\fR command, like this:.CS\fBnamespace forget Blt::*\fR.CEThis searches the current namespace for any commands imported from \fBBlt\fR.If it finds any, it removes them.  Otherwise, it does nothing.After this, the \fBBlt\fR commands must be accessed with the \fBBlt::\fRprefix..PPWhen you delete a command from the exporting namespace like this:.CS\fBrename Blt::graph ""\fR.CEthe command is automatically removed from all namespaces that import it..SH "EXPORTING COMMANDS"You can export commands from a namespace like this:.CS\fBnamespace eval Counter {    namespace export Bump Reset    variable num 0    variable max 100    proc Bump {{by 1}} {        variable num        incr num $by        check        return $num    }    proc Reset {} {        variable num        set num 0    }    proc check {} {        variable num        variable max        if {$num > $max} {            error "too high!"        }    }}\fR.CEThe procedures \fBBump\fR and \fBReset\fR are exported,so they are included when you import from the \fBCounter\fR namespace,like this:.CS\fBnamespace import Counter::*\fR.CEHowever, the \fBcheck\fR procedure is not exported,so it is ignored by the import operation..PPThe \fBnamespace import\fR command only imports commandsthat were declared as exported by their namespace.The \fBnamespace export\fR command specifies what commandsmay be imported by other namespaces.If a \fBnamespace import\fR command specifies a commandthat is not exported, the command is not imported..SH "SEE ALSO"variable(n).SH KEYWORDSexported, internal, variable

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -