Tcl_Namespace(3tcl)
π Tcl_Namespace(3tcl) π
okay so bestie, welcome to the Tcl Library Procedures page for namespace manipulation. This is where all the C-level namespace functions live: creating, deleting, searching, importing, exporting, and generally making namespaces behave. π
β¨ NAME β¨
Tcl_AppendExportList, Tcl_CreateNamespace, Tcl_DeleteNamespace, Tcl_Export, Tcl_FindCommand, Tcl_FindNamespace, Tcl_ForgetImport, Tcl_GetCurrentNamespace, Tcl_GetGlobalNamespace, Tcl_GetNamespaceUnknownHandler, Tcl_Import, Tcl_SetNamespaceUnknownHandler β manipulate namespaces.
π SYNOPSIS π
#include <tcl.h>
Tcl_Namespace *
Tcl_CreateNamespace(interp, name, clientData, deleteProc)
Tcl_DeleteNamespace(nsPtr)
int
Tcl_AppendExportList(interp, nsPtr, objPtr)
int
Tcl_Export(interp, nsPtr, pattern, resetListFirst)
int
Tcl_Import(interp, nsPtr, pattern, allowOverwrite)
int
Tcl_ForgetImport(interp, nsPtr, pattern)
Tcl_Namespace *
Tcl_GetCurrentNamespace(interp)
Tcl_Namespace *
Tcl_GetGlobalNamespace(interp)
Tcl_Namespace *
Tcl_FindNamespace(interp, name, contextNsPtr, flags)
Tcl_Command
Tcl_FindCommand(interp, name, contextNsPtr, flags)
Tcl_Obj *
Tcl_GetNamespaceUnknownHandler(interp, nsPtr)
int
Tcl_SetNamespaceUnknownHandler(interp, nsPtr, handlerPtr)
π«Ά ARGUMENTS π«Ά
Tcl_Interp *interp(in/out) β the interpreter in which the namespace exists and where name lookups are performed. Also where error result messages are written. πconst char *name(in) β the name of the namespace or command to be created or accessed. β¨void *clientData(in) β a context pointer by the creator of the namespace. Tcl does not interpret it at all. πTcl_NamespaceDeleteProc *deleteProc(in) β a pointer to function to call when the namespace is deleted, orNULLif no such callback is to be performed. πTcl_Namespace *nsPtr(in) β the namespace to be manipulated, orNULL(for other thanTcl_DeleteNamespace) to manipulate the current namespace. πTcl_Obj *objPtr(out) β a reference to an unshared value to which the function output will be written. πconst char *pattern(in) β the glob-style pattern (seeTcl_StringMatch) that describes the commands to be imported or exported. π¦int resetListFirst(in) β whether the list of export patterns should be reset before adding the current pattern to it. πΈint allowOverwrite(in) β whether new commands created by this import action can overwrite existing commands. π₯Tcl_Namespace *contextNsPtr(in) β the location in the namespace hierarchy where the search for a namespace or command should be conducted relative to when the search term is not rooted at the global namespace.NULLindicates the current namespace. πint flags(in) β OR-ed combination of bits controlling how the search is to be performed. The following flags are supported:TCL_GLOBAL_ONLYβ indicates that the search is always to be conducted relative to the global namespace. πTCL_NAMESPACE_ONLYβ just forTcl_FindCommand; indicates that the search is always to be conducted relative to the context namespace. πTCL_LEAVE_ERR_MSGβ indicates that an error message should be left in the interpreter if the search fails. π
Tcl_Obj *handlerPtr(in) β a script fragment to be installed as the unknown command handler for the namespace, orNULLto reset the handler to its default. β
π DESCRIPTION π
okay so namespaces are hierarchic naming contexts that can contain commands and variables. They also maintain a list of patterns that describes what commands are exported, and can import commands that have been exported by other namespaces. Namespaces can also be manipulated through the Tcl command namespace. π«Ά
The Tcl_Namespace structure encapsulates a namespace, and is guaranteed to have the following fields in it: name (the local name of the namespace, with no namespace separator characters in it, with empty denoting the global namespace), fullName (the fully specified name of the namespace), clientData, deleteProc (the values specified in the call to Tcl_CreateNamespace), and parentPtr (a pointer to the containing namespace, or NULL for the global namespace.) π
Tcl_CreateNamespace creates a new namespace. The deleteProc will have the following type signature:
typedef void Tcl_NamespaceDeleteProc(
void *clientData);
Tcl_DeleteNamespace deletes a namespace, calling the deleteProc defined for the namespace (if any). π
Tcl_AppendExportList retrieves the export patterns for a namespace given namespace and appends them (as list items) to objPtr. π
Tcl_Export sets and appends to the export patterns for a namespace. Patterns are appended unless the resetListFirst flag is true. β¨
Tcl_Import imports commands matching a pattern into a namespace. Note that the pattern must include the name of the namespace to import from. This function returns TCL_ERROR if an attempt to import a command over an existing command is made, unless the allowOverwrite flag has been set. π¨
Tcl_ForgetImport removes imports matching a pattern. π§Ή
Tcl_GetCurrentNamespace returns the current namespace for an interpreter. π
Tcl_GetGlobalNamespace returns the global namespace for an interpreter. π
Tcl_FindNamespace searches for a namespace named name within the context of the namespace contextNsPtr. If the namespace cannot be found, NULL is returned. π
Tcl_FindCommand searches for a command named name within the context of the namespace contextNsPtr. If the command cannot be found, NULL is returned. π
Tcl_GetNamespaceUnknownHandler returns the unknown command handler for the namespace, or NULL if none is set. π»
Tcl_SetNamespaceUnknownHandler sets the unknown command handler for the namespace. If handlerPtr is NULL, then the handler is reset to its default. π
π₯ REFERENCE COUNT MANAGEMENT π₯
okay bestie this part is IMPORTANT, like, pay attention because refcounts are the vibe check of C extension life π
- the
objPtrargument you hand toTcl_AppendExportListshould be an unshared object, because that function is gonna modify it. and the reference count ofobjPtr? not altered. nope. she stays exactly as she was. π Tcl_GetNamespaceUnknownHandlergives you back a value that might be shared. so if you wanna keep that value around, you gotta increment its reference count yourself. no one's doing it for you, bestie π- the
handlerPtrargument toTcl_SetNamespaceUnknownHandler? if it's a non-empty list</>, its reference count will get incremented. that one's on the function, not you π«Ά
fr fr, do not sleep on this section. refcount mistakes are the lowkey villain of every Tcl extension, and they will not hesitate to eat your memory alive π
π SEE ALSO π
go read those too, they're literally the squad π
β KEYWORDS β
namespace, command
and that's the whole page, bestie!! π you now know namespaces in Tcl better than most people who've been writing C extensions since before you were born, no cap π
this lil masterpiece lives in Tcl 8.5, and its whole government name is Tcl_Namespace(3tcl) β go forth and slay those namespaces, mwah π
β¨π¦
π¦ where this page comes from
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 2003 Donal K. Fellows
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms