📄 namespace.n
字号:
'\"'\" Copyright (c) 1993-1997 Bell Labs Innovations for Lucent Technologies'\" Copyright (c) 1997 Sun Microsystems, Inc.'\"'\" See the file "license.terms" for information on usage and redistribution'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.'\" '\" SCCS: @(#) namespace.n 1.9 97/08/13 17:08:25'\" .so man.macros.TH namespace n 8.0 Tcl "Tcl Built-In Commands".BS'\" Note: do not modify the .SH NAME line immediately below!.SH NAMEnamespace \- create and manipulate contexts for commands and variables.SH SYNOPSIS\fBnamespace \fR?\fIoption\fR? ?\fIarg ...\fR?.BE.SH DESCRIPTION.PPThe \fBnamespace\fR command lets you create, access, and destroyseparate contexts for commands and variables.See the section \fBWHAT IS A NAMESPACE?\fR belowfor a brief overview of namespaces.The legal \fIoption\fR's are listed below.Note that you can abbreviate the \fIoption\fR's..TP\fBnamespace children \fR?\fInamespace\fR? ?\fIpattern\fR?Returns a list of all child namespaces that belong to thenamespace \fInamespace\fR.If \fInamespace\fR is not specified,then the children are returned for the current namespace.This command returns fully-qualified names,which start with \fB::\fR.If the optional \fIpattern\fR is given,then this command returns only the names that match the glob-style pattern.The actual pattern used is determined as follows:a pattern that starts with \fB::\fR is used directly,otherwise the namespace \fInamespace\fR(or the fully-qualified name of the current namespace)is prepended onto the the pattern..TP\fBnamespace code \fIscript\fRCaptures the current namespace context for later executionof the script \fIscript\fR.It returns a new script in which \fIscript\fR has been wrappedin a \fBnamespace code\fR command.The new script has two important properties.First, it can be evaluated in any namespace and will cause\fIscript\fR to be evaluated in the current namespace(the one where the \fBnamespace code\fR command was invoked).Second, additional arguments can be appended to the resulting scriptand they will be passed to \fIscript\fR as additional arguments.For example, suppose the command\fBset script [namespace code {foo bar}]\fRis invoked in namespace \fB::a::b\fR.Then \fBeval "$script x y"\fRcan be executed in any namespace (assuming the value of\fBscript\fR has been passed in properly)and will have the same effect as the command\fBnamespace eval ::a::b {foo bar x y}\fR.This command is needed becauseextensions like Tk normally execute callback scriptsin the global namespace.A scoped command captures a command together with its namespace contextin a way that allows it to be executed properly later.See the section \fBSCOPED VALUES\fR for some examplesof how this is used to create callback scripts..TP\fBnamespace current\fRReturns the fully-qualified name for the current namespace.The actual name of the global namespace is ``''(i.e., an empty string),but this command returns \fB::\fR for the global namespaceas a convenience to programmers..TP\fBnamespace delete \fR?\fInamespace namespace ...\fR?Each namespace \fInamespace\fR is deletedand all variables, procedures, and child namespacescontained in the namespace are deleted.If a procedure is currently executing inside the namespace,the namespace will be kept alive until the procedure returns;however, the namespace is marked to prevent other code fromlooking it up by name.If a namespace doesn't exist, this command returns an error.If no namespace names are given, this command does nothing..TP\fBnamespace eval\fR \fInamespace arg\fR ?\fIarg ...\fR?Activates a namespace called \fInamespace\fR and evaluates some codein that context.If the namespace does not already exist, it is created.If more than one \fIarg\fR argument is specified,the arguments are concatenated together with a space between each onein the same fashion as the \fBeval\fR command,and the result is evaluated..br.spIf \fInamespace\fR has leading namespace qualifiersand any leading namespaces do not exist,they are automatically created..TP\fBnamespace export \fR?\-\fBclear\fR? ?\fIpattern pattern ...\fR?Specifies which commands are exported from a namespace.The exported commands are those that can be later importedinto another namespace using a \fBnamespace import\fR command.Both commands defined in a namespace andcommands the namespace has previously importedcan be exported by a namespace.The commands do not have to be definedat the time the \fBnamespace export\fR command is executed.Each \fIpattern\fR may contain glob-style special characters,but it may not include any namespace qualifiers.That is, the pattern can only specify commandsin the current (exporting) namespace.Each \fIpattern\fR is appended onto the namespace's list of export patterns.If the \-\fBclear\fR flag is given,the namespace's export pattern list is reset to empty before any\fIpattern\fR arguments are appended.If no \fIpattern\fRs are given and the \-\fBclear\fR flag isn't given,this command returns the namespace's current export list..TP\fBnamespace forget \fR?\fIpattern pattern ...\fR?Removes previously imported commands from a namespace.Each \fIpattern\fR is a qualified name such as\fBfoo::x\fR or \fBa::b::p*\fR.Qualified names contain \fB::\fRs and qualify a namewith the name of one or more namespaces.Each \fIpattern\fR is qualified with the name of an exporting namespaceand may have glob-style special characters in the command nameat the end of the qualified name.Glob characters may not appear in a namespace name.This command first finds the matching exported commands.It then checks whether any of those those commandswere previously imported by the current namespace.If so, this command deletes the corresponding imported commands. In effect, this un-does the action of a \fBnamespace import\fR command..TP\fBnamespace import \fR?\fB\-force\fR? ?\fIpattern\fR \fIpattern ...\fR?Imports commands into a namespace.Each \fIpattern\fR is a qualified name like\fBfoo::x\fR or \fBa::p*\fR.That is, it includes the name of an exporting namespaceand may have glob-style special characters in the command nameat the end of the qualified name.Glob characters may not appear in a namespace name.All the commands that match a \fIpattern\fR stringand which are currently exported from their namespaceare added to the current namespace.This is done by creating a new command in the current namespacethat points to the exported command in its original namespace;when the new imported command is called, it invokes the exported command.This command normally returns an errorif an imported command conflicts with an existing command.However, if the \-\fBforce\fR option is given,imported commands will silently replace existing commands.The \fBnamespace import\fR command has snapshot semantics:that is, only requested commands that are currently definedin the exporting namespace are imported.In other words, you can import only the commands that are in a namespaceat the time when the \fBnamespace import\fR command is executed.If another command is defined and exported in this namespace later on,it will not be imported..TP\fBnamespace inscope\fR \fInamespace arg\fR ?\fIarg ...\fR?Executes a script in the context of a particular namespace.This command is not expected to be used directly by programmers;calls to it are generated implicitly when applicationsuse \fBnamespace code\fR commands to create callback scriptsthat the applications then register with, e.g., Tk widgets.The \fBnamespace inscope\fR command is much like the \fBnamespace eval\fRcommand except that it has \fBlappend\fR semanticsand the namespace must already exist.It treats the first argument as a list,and appends any arguments after the firstonto the end as proper list elements.\fBnamespace inscope ::foo a x y z\fRis equivalent to\fBnamespace eval ::foo [concat a [list x y z]]\fRThis \fBlappend\fR semantics is important because many callback scriptsare actually prefixes..TP\fBnamespace origin \fIcommand\fRReturns the fully-qualified name of the original commandto which the imported command \fIcommand\fR refers.When a command is imported into a namespace,a new command is created in that namespacethat points to the actual command in the exporting namespace.If a command is imported into a sequence of namespaces\fIa, b,...,n\fR where each successive namespacejust imports the command from the previous namespace,this command returns the fully-qualified name of the original commandin the first namespace, \fIa\fR.If \fIcommand\fR does not refer to an imported command,the command's own fully-qualified name is returned..TP\fBnamespace parent\fR ?\fInamespace\fR?Returns the fully-qualified name of the parent namespacefor namespace \fInamespace\fR.If \fInamespace\fR is not specified,the fully-qualified name of the current namespace's parent is returned..TP\fBnamespace qualifiers\fR \fIstring\fRReturns any leading namespace qualifiers for \fIstring\fR.Qualifiers are namespace names separated by \fB::\fRs.For the \fIstring\fR \fB::foo::bar::x\fR,this command returns \fB::foo::bar\fR,and for \fB::\fR it returns \fB``''\fR (an empty string).This command is the complement of the \fBnamespace tail\fR command.Note that it does not check whether thenamespace names are, in fact,the names of currently defined namespaces..TP\fBnamespace tail\fR \fIstring\fRReturns the simple name at the end of a qualified string.Qualifiers are namespace names separated by \fB::\fRs.For the \fIstring\fR \fB::foo::bar::x\fR,this command returns \fBx\fR,and for \fB::\fR it returns \fB``''\fR (an empty string).This command is the complement of the \fBnamespace qualifiers\fR command.It does not check whether the namespace names are, in fact,the names of currently defined namespaces..TP\fBnamespace which\fR ?\-\fBcommand\fR? ?\-\fBvariable\fR? \fIname\fRLooks up \fIname\fR as either a command or variableand returns its fully-qualified name.For example, if \fIname\fR does not exist in the current namespacebut does exist in the global namespace,this command returns a fully-qualified name in the global namespace.If the command or variable does not exist,this command returns an empty string.If no flag is given, \fIname\fR is treated as a command name.See the section \fBNAME RESOLUTION\fR below for an explanation ofthe rules regarding name resolution..SH "WHAT IS A NAMESPACE?".PPA namespace is a collection of commands and variables.It encapsulates the commands and variables to ensure that theywon't interfere with the commands and variables of other namespaces.Tcl has always had one such collection,which we refer to as the \fIglobal namespace\fR.The global namespace holds all global variables and commands.The \fBnamespace eval\fR command lets you create new namespaces.For example,.CS\fBnamespace eval Counter { namespace export Bump variable num 0 proc Bump {} { variable num incr num }}\fR.CEcreates a new namespace containing the variable \fBnum\fR andthe procedure \fBBump\fR.The commands and variables in this namespace are separate fromother commands and variables in the same program.If there is a command named \fBBump\fR in the global namespace,for example, it will be different from the command \fBBump\fRin the \fBCounter\fR namespace..PPNamespace variables resemble global variables in Tcl.They exist outside of the procedures in a namespacebut can be accessed in a procedure via the \fBvariable\fR command,as shown in the example above..PPNamespaces are dynamic.You can add and delete commands and variables at any time,so you can build up the contents of anamespace over time using a series of \fBnamespace eval\fR commands.For example, the following series of commands has the same effectas the namespace definition shown above:.CS\fBnamespace eval Counter { variable num 0 proc Bump {} { variable num return [incr num] }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -