tclvars(3tcl)
π tclvars β the Tcl variables glow-up π
okay bestie, welcome to tclvars from the Tcl Built-In Commands manual β yes it's a weird name for a page about a whole squad of variables, but here we are, we're slaying anyway π«Ά
πΈ NAME πΈ
argc, argv, argv0, auto_path, env, errorCode, errorInfo, tcl_interactive, tcl_library, tcl_patchLevel, tcl_pkgPath, tcl_platform, tcl_precision, tcl_rcFileName, tcl_traceCompile, tcl_traceExec, tcl_version β literally all of these are Variables used by Tcl β¨
π DESCRIPTION π
so here's the tea: the Tcl library creates and manages these global variables automatically. She's got it handled, you don't need to do anything π
except where it says otherwise below, you should treat these as read-only. Like, hands off, no cap β application-specific code and users are not meant to be writing to them π
β¨ auto_path β¨
if it's set, it must contain a valid Tcl list giving directories to search during auto-load operations (and that includes for package index files when you're using the default package unknown handler π).
this variable gets initialized during startup to contain, in this exact order:
- the directories listed in the
TCLLIBPATHenvironment variable - the directory named by the
tcl_libraryglobal variable - the parent directory of
tcl_library [file dirname [file dirname [info nameofexecutable]]]/lib- the directories listed in the
tcl_pkgPathvariable
if you wanna add more places to look for files and package indices, the correct move is normally to lappend onto this variable. that's the vibe. π
there are also additional variables relating to package management hanging around β more details are listed in the VARIABLES section of the library manual page π
π¦ env π¦
this one is an array, maintained by Tcl, and its elements are the environment variables for the process. iconic.
- reading an element β returns the value of the corresponding environment variable β
- setting an element β modifies the corresponding environment variable, or creates a new one if it doesn't already exist π
- unsetting an element of
envβ removes the corresponding environment variable ποΈ
changes to the env array will affect the environment passed to children by commands like exec πΆ
β οΈ if the entireenvarray is unset, then Tcl stops monitoringenvaccesses and will not update environment variables anymore. so likeβ¦ don't do that unless you mean it π
Windows girlies, listen up πͺ: the environment variables PATH and COMSPEC in any capitalization get converted automatically to upper case. for instance, the PATH variable could be exported by the operating system as βpathβ, βPathβ, βPaThβ, etc., which would otherwise force simple Tcl code to support a ridiculous number of special cases. the audacity fr fr.
all other environment variables inherited by Tcl are left unmodified. setting an env array variable to blank is the same as unsetting it, because that's just how the underlying Windows OS behaves. and heads up: relying on an existing and empty environment variable will not work on Windows and is discouraged for cross-platform usage π«
the following elements of env are special to Tcl:
env(HOME)β if set, this gives the location of the directory considered to be the current user's home directory, and it's where a call ofcdwith no arguments, or with just~as an argument, will change into. most platforms set this correctly by default; it does not normally need to be set by user code πenv(TCL_LIBRARY)β if set, it specifies the location of the directory containing library scripts (the value of this variable will be assigned to thetcl_libraryvariable and therefore returned by the commandinfo library). if this variable is not set then a default value is used.
note that this environment variable should not normally be set π
env(TCLLIBPATH)β if set, it must contain a valid Tcl list giving directories to search during auto-load operations. directories must be specified in Tcl format, using/as the path separator, regardless of platform. this variable is only used when initializing theauto_pathvariable πenv(TCL_TZ),env(TZ)β these specify the default timezone used for parsing and formatting times and dates in theclockcommand. on many platforms, theTZenvironment variable is set up by the operating system β°env(LC_ALL),env(LC_MESSAGES),env(LANG)β these are used by themsgcatpackage to determine what locale to format messages using πenv(TCL_INTERP_DEBUG_FRAME)β if it exists, it has the same effect as runninginterp debug {} -frame 1as the very first command of each new Tcl interpreter π
π errorCode π
this variable holds the value of the -errorcode return option set by the most recent error that occurred in this interpreter. it's a list value, and it represents additional information about the error in a form that's easy to process with programs π§
the first element of the list identifies a general class of errors, and determines the format of the rest of the list. the following formats for -errorcode return options are used by the Tcl core; individual applications may define additional formats π―
ARITHcode msg β this format is used when an arithmetic error occurs (e.g. an attempt to divide zero by zero in theexprcommand). code identifies the precise error and msg provides a human-readable description of the error. code will be eitherDIVZERO(for an attempt to divide by zero),DOMAIN(if an argument is outside the domain of a function, such asacos(-3)),IOVERFLOW(for integer overflow),OVERFLOW(for a floating-point overflow), orUNKNOWN(if the cause of the error cannot be determined).
detection of these errors depends in part on the underlying hardware and system libraries, so it's a little bit ymmv π€·
CHILDKILLEDpid sigName msg β this format is used when a child process has been killed because of a signal. the pid element is the process's identifier (in decimal). the sigName element is the symbolic name of the signal that caused the process to terminate; it will be one of the names from the include filesignal.h, such asSIGPIPE. the msg element is a short human-readable message describing the signal, such as βwrite on pipe with no readersβ forSIGPIPEπCHILDSTATUSpid code β this format is used when a child process has exited with a non-zero exit status. the pid element is the process's identifier (in decimal) and the code element is the exit code returned by the process (also in decimal) πͺCHILDSUSPpid sigName msg β this format is used when a child process has been suspended because of a signal. the pid element is the process's identifier, in decimal. the sigName element is the symbolic name of the signal that caused the process to suspend; this will be one of the names from the include filesignal.h, such asSIGTTIN. the msg element is a short human-readable message describing the signal, such as βbackground tty readβ forSIGTTINβΈοΈNONEβ this format is used for errors where no additional information is available for an error besides the message returned with the error. in these cases the-errorcodereturn option will consist of a list containing a single element whose contents areNONEπ³POSIXerrName msg β if the first element isPOSIX, then the error occurred during a POSIX kernel call. the errName element contains the symbolic name of the error that occurred, such asENOENT; this will be one of the values defined in the include fileerrno.h. the msg element is a human-readable message corresponding to errName, such as βno such file or directoryβ for theENOENTcase π§TCL... β indicates some sort of problem generated in relation to Tcl itself, e.g. a failure to look up a channel or variable πͺ
to set the -errorcode return option, applications should use library procedures such as Tcl_SetObjErrorCode, Tcl_SetReturnOptions, and Tcl_PosixError, or they may invoke the -errorcode option of the return command. and here's the fine print: if none of these methods for setting the error code has been used, the Tcl interpreter will reset the variable to NONE after the next error π
π
errorInfo π
this variable holds the value of the -errorinfo return option set by the most recent error that occurred in this interpreter. this string value will contain one or more lines identifying the Tcl commands and procedures that were being executed when the most recent error occurred β its contents take the form of a stack trace, showing the various nested Tcl commands that had been invoked at the time of the error π§΅
more parts coming, don't go anywhere bestie π
tcl_libraryβ bestie, this one holds the name of a directory, and that directory is where the system library of Tcl scripts lives π β you know, all the auto-loading stuff. Want the value? Runinfo libraryand it hands it right back to you. The library manual entry has the full tea on what the Tcl script library actually gives you.
So here's the vibe: normally every application or package gets its own application-specific script library on top of the Tcl script library, and each app is supposed to set a global variable named like $app_library (where app is the app's name) to hold the network file name for that app's library directory. Iconic, lowkey.
When an interpreter gets created, the initial value of tcl_library is set by searching a bunch of different directories until one is found that has an appropriate Tcl startup script. If the TCL_LIBRARY environment variable exists, then the directory it names gets checked first π
. If TCL_LIBRARY isn't set, or it doesn't point at an appropriate directory, then Tcl checks several other directories based on a compiled-in default location, the location of the binary containing the application, and the current working directory. No cap.
tcl_patchLevelβ when an interpreter is created, Tcl sets this variable to hold a string with the current patch level for Tcl β¨ β stuff like8.4.16for Tcl 8.4 with the first sixteen official patches, or8.5b3for the third beta release of Tcl 8.5. The value of this variable is returned by theinfo patchlevelcommand. Simple as that.tcl_pkgPathβ this variable holds a list of directories showing where packages are normally installed π¦. It is not used on Windows, just so you know. Typically it has either one or two entries; if there are two, the first is normally a directory for platform-dependent packages (e.g., shared library binaries) and the second is normally a directory for platform-independent packages (e.g., script files). Typically a package is installed as a subdirectory of one of the entries in thetcl_pkgPathvariable. The directories intcl_pkgPathare included by default in theauto_pathvariable, so they and their immediate subdirectories get automatically searched for packages duringpackage requirecommands.
Note:tcl_pkgPathis not intended to be modified by the application π«. Its value is added toauto_pathat startup; changes totcl_pkgPathare not reflected inauto_path. If you want Tcl to search additional directories for packages, you should add the names of those directories toauto_path, nottcl_pkgPath. Periodt.
tcl_platformβ this is an associative array whose elements contain information about the platform the application is running on π β like the name of the operating system, its current release number, and the machine's instruction set. The elements listed below will always be defined, but ngl they may have empty strings as values if Tcl couldn't retrieve any relevant information π. Also, extensions and applications may add additional values to the array. The predefined elements are:byteOrderβ the native byte order of this machine: eitherlittleEndianorbigEndian.debugβ if this variable exists, then the interpreter was compiled with and linked to a debug-enabled C run-time. This variable will only exist on Windows, so extension writers can specify which package to load depending on the C run-time library that is in use. Heads up: this is not an indication that this core contains symbols.engineβ the name of the Tcl language implementation. When the interpreter is first created, this is always set to the stringTcl.machineβ the instruction set executed by this machine, likeintel,PPC,68k, orsun4m. On UNIX machines, this is the value returned byuname -m.osβ the name of the operating system running on this machine, likeWindows NTorSunOS. On UNIX machines, this is the value returned byuname -s.osVersionβ the version number for the operating system running on this machine. On UNIX machines, this is the value returned byuname -r.pathSeparatorβ the character that should be used to split PATH-like environment variables into their corresponding list of directory names π§.platformβ eitherwindowsorunix. This identifies the general operating environment of the machine.pointerSizeβ this gives the size of the native-machine pointer in bytes (strictly, it is same as the result of evaluatingsizeof(void*)in C.).threadedβ if this variable exists, then the interpreter was compiled with threads enabled π§΅.userβ this identifies the current user based on the login information available on the platform. This value comes from thegetuid()andgetpwuid()system calls on Unix, and the value from theGetUserName()system call on Windows.wordSizeβ this gives the size of the native-machine word in bytes (strictly, it is same as the result of evaluatingsizeof(long)in C.).
tcl_precisionβ okay so this variable controls the number of digits to generate when converting floating-point values to strings π’. It defaults to0. Applications should not change this value; it's provided for compatibility with legacy code.
The default value of 0 is special β it means Tcl should convert numbers using as few digits as possible while still distinguishing any floating point number from its nearest neighbours. The way it differs from using an arbitrarily high value for tcl_precision is that an inexact number like 1.4 will convert as 1.4 rather than 1.3999999999999999, even though the latter is nearer to the exact value of the binary number π.
If tcl_precision is not zero, then when Tcl converts a floating point number, it creates a decimal representation of at most tcl_precision significant digits; the result may be shorter if the shorter result represents the original number exactly. If no result of at most tcl_precision digits is an exact representation of the original number, the one that is closest to the original number is chosen. And if the original number lies precisely between two equally accurate decimal representations, then the one with an even value for the least significant digit is chosen; for instance, if tcl_precision is 3, then 0.3125 will convert to 0.312, not 0.313, while 0.6875 will convert to 0.688, not 0.687. Any string of trailing zeroes that remains gets trimmed βοΈ.
A tcl_precision value of 17 digits is βperfectβ for IEEE floating-point in that it allows double-precision values to be converted to strings and back to binary with no loss of information. For this reason, you'll often see it as a value in legacy code that must run on Tcl versions before 8.5. It is no longer recommended; as noted above, a zero value is the preferred method π.
All interpreters in a thread share a single tcl_precision value: changing it in one interpreter will affect all other interpreters as well π³. Safe interpreters are not allowed to modify the variable.
Valid values for tcl_precision range from 0 to 17.
tcl_rcFileNameβ this variable is used during initialization to indicate the name of a user-specific startup file πΈ. If it's set by application-specific initialization, then the Tcl startup code will check whether this file exists and source it if it does. For example, forwishthe variable is set to~/.wishrcfor Unix and~/wishrc.tclfor Windows.tcl_traceCompileβ the value of this variable can be set to control how much tracing information gets displayed during bytecode compilation π. By default,tcl_traceCompileis zero and no information is displayed. Settingtcl_traceCompileto1generates a one-line summary in stdout whenever a procedure or top-level command is compiled. Setting it to2generates a detailed listing in stdout of the bytecode instructions emitted during every compilation. This variable is useful in tracking down suspected problems with the Tcl compiler π.
This variable and functionality only exist if TCL_COMPILE_DEBUG was defined during Tcl's compilation.
tcl_traceExecβ the value of this variable can be set to control how much tracing information gets displayed during bytecode execution β¨. By default,tcl_traceExecis zero and no information is displayed. Settingtcl_traceExecto1generates a one-line trace in stdout on each call to a Tcl procedure. Setting it to2generates a line of output whenever any Tcl command is invoked that contains the name of the command and its arguments. Setting it to3produces a detailed trace showing the result of executing each bytecode instruction. Mentioning that whentcl_traceExecis2or3, commands such assetandincrthat have been entirely replaced by a sequence of bytecode instructions are not shown π. Setting this variable is useful in tracking down suspected problems with the bytecode compiler and interpreter.
This variable and functionality only exist if TCL_COMPILE_DEBUG was defined during Tcl's compilation.
tcl_versionβ when an interpreter is created, Tcl initializes this variable to hold the version number for this version of Tcl in the form x.y π. Changes to x represent major changes with probable incompatibilities, and changes to y represent small enhancements and bug fixes that retain backward compatibility. The value of this variable is returned by theinfo tclversioncommand.
π¦ OTHER GLOBAL VARIABLES π¦
okay so these next ones are the extra girlies, bestie. they're only guaranteed to exist in tclsh and wish executables π
the Tcl library itself does not define them, but like, a lot of Tcl environments do, so don't be shocked when you see them chilling there β¨
argcβ the number of arguments handed totclshorwish. that's it, that's the count πargvβ a Tcl list of the arguments handed totclshorwish. the whole squad, in order πΈargv0β the script thattclshorwishstarted executing (if one was actually specified), otherwise it's just the name thattclshorwishwas invoked by πͺ like, "who am i really" energytcl_interactiveβ holds1iftclshorwishis running interactively (meaning no script was specified and standard input is a terminal-like device) and0otherwise π₯ so it's basically the "are we yapping in the REPL rn" flag
π EXAMPLES π
so say you wanna add a directory to the collection of locations that package require searches β like, because your app has some app-specific packages it's using β then you gotta update the auto_path variable:
lappend ::auto_path [file join [pwd] "theLibDir"]
periodt π
now here's a simple β though ngl, not very robust β way to handle command line arguments of the form "-foo 1 -bar 2": just load them into an array, but first load in the default settings β¨ like so:
array set arguments {-foo 0 -bar 0 -grill 0}
array set arguments $::argv
puts "foo is $arguments(-foo)"
puts "bar is $arguments(-bar)"
puts "grill is $arguments(-grill)"
it's giving quick-and-dirty and honestly? valid for a lil script π«Ά but bestie, she said not very robust, she meant it π
also!! the argv0 global variable can be used (in conjunction with the info script command, iconic duo π) to figure out whether the current script is being executed as the main script or loaded as a library. this is genuinely so useful because it lets a single script serve double duty as both a library and a demonstration of that library π obsessed:
if {$::argv0 eq [info script]} {
# running as: tclsh example.tcl
} else {
package provide Example 1.0
}
one script, two jobs, main character energy fr fr π β¨
π SEE ALSO π
eval(3tcl), library(3tcl), tclsh(1), tkvars(3tcl), wish(1)
π KEYWORDS π
arithmetic, bytecode, compiler, error, environment, POSIX, precision, subprocess, user, variables
and that's the whole page, bestie!! this was tclvars(3tcl) from Tcl 8.0 β go forth and read your global variables with confidence, you absolute scholar πππ¦ slay, no cap, and remember: the vibe changes but the facts never do πΈππ
π¦ where this page comes from
- ubuntu stonking: tcl8.6-doc 8.6.18+dfsg-1
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1997 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms