This command keeps a simple database of the packages available for use by the current interpreter and how
to load them into the interpreter. It supports multiple versions of each package and arranges for the
correct version of a package to be loaded based on what is needed by the application. This command also
detects and reports version clashes. Typically, only the packagerequire and packageprovide commands
are invoked in normal Tcl scripts; the other commands are used primarily by system scripts that maintain
the package database.
The behavior of the package command is determined by its first argument. The following forms are
permitted:
packagefilespackage
Lists all files forming part of package. Auto-loaded files are not included in this list, only
files which were directly sourced during package initialization. The list order corresponds with
the order in which the files were sourced.
packageforget ?packagepackage...?
Removes all information about each specified package from this interpreter, including information
provided by both packageifneeded and packageprovide.
packageifneededpackageversion ?script?
This command typically appears only in system configuration scripts to set up the package
database. It indicates that a particular version of a particular package is available if needed,
and that the package can be added to the interpreter by executing script. The script is saved in
a database for use by subsequent packagerequire commands; typically, script sets up auto-loading
for the commands in the package (or calls load and/or source directly), then invokes packageprovide to indicate that the package is present. There may be information in the database for
several different versions of a single package. If the database already contains information for
package and version, the new script replaces the existing one. If the script argument is omitted,
the current script for version version of package package is returned, or an empty string if no
packageifneeded command has been invoked for this package and version.
packagenames
Returns a list of the names of all packages in the interpreter for which a version has been
provided (via packageprovide) or for which a packageifneeded script is available. The order of
elements in the list is arbitrary.
packagepresent ?-exact? package ?requirement...?
This command is equivalent to packagerequire except that it does not try and load the package if
it is not already loaded.
packageprovidepackage ?version?
This command is invoked to indicate that version version of package package is now present in the
interpreter. It is typically invoked once as part of an ifneeded script, and again by the package
itself when it is finally loaded. An error occurs if a different version of package has been
provided by a previous packageprovide command. If the version argument is omitted, then the
command returns the version number that is currently provided, or an empty string if no packageprovide command has been invoked for package in this interpreter.
packagerequirepackage ?requirement...?
This command is typically invoked by Tcl code that wishes to use a particular version of a
particular package. The arguments indicate which package is wanted, and the command ensures that
a suitable version of the package is loaded into the interpreter. If the command succeeds, it
returns the version number that is loaded; otherwise it generates an error.
A suitable version of the package is any version which satisfies at least one of the requirements,
per the rules of packagevsatisfies. If multiple versions are suitable the implementation with the
highest version is chosen. This last part is additionally influenced by the selection mode set
with packageprefer.
In the “stable” selection mode the command will select the highest stable version satisfying the
requirements, if any. If no stable version satisfies the requirements, the highest unstable
version satisfying the requirements will be selected. In the “latest” selection mode the command
will accept the highest version satisfying all the requirements, regardless of its stableness.
If a version of package has already been provided (by invoking the packageprovide command), then
its version number must satisfy the requirements and the command returns immediately. Otherwise,
the command searches the database of information provided by previous packageifneeded commands to
see if an acceptable version of the package is available. If so, the script for the highest
acceptable version number is evaluated in the global namespace; it must do whatever is necessary
to load the package, including calling packageprovide for the package. If the packageifneeded
database does not contain an acceptable version of the package and a packageunknown command has
been specified for the interpreter then that command is evaluated in the global namespace; when
it completes, Tcl checks again to see if the package is now provided or if there is a packageifneeded script for it. If all of these steps fail to provide an acceptable version of the
package, then the command returns an error.
packagerequire-exactpackageversion
This form of the command is used when only the given version of package is acceptable to the
caller. This command is equivalent to packagerequirepackageversion-version.
packageunknown ?command?
This command supplies a “last resort” command to invoke during packagerequire if no suitable
version of a package can be found in the packageifneeded database. If the command argument is
supplied, it contains the first part of a command; when the command is invoked during a packagerequire command, Tcl appends one or more additional arguments giving the desired package name and
requirements. For example, if command is foobar and later the command packagerequiretest2.4
is invoked, then Tcl will execute the command foobartest2.4 to load the package. If no
requirements are supplied to the packagerequire command, then only the name will be added to
invoked command. If the packageunknown command is invoked without a command argument, then the
current packageunknown script is returned, or an empty string if there is none. If command is
specified as an empty string, then the current packageunknown script is removed, if there is one.
packagevcompareversion1version2
Compares the two version numbers given by version1 and version2. Returns -1 if version1 is an
earlier version than version2, 0 if they are equal, and 1 if version1 is later than version2.
packageversionspackage
Returns a list of all the version numbers of package for which information has been provided by
packageifneeded commands.
packagevsatisfiesversionrequirement...
Returns 1 if the version satisfies at least one of the given requirements, and 0 otherwise. Each
requirement is allowed to have any of the forms:
min This form is called “min-bounded”.
min- This form is called “min-unbound”.
min-max
This form is called “bounded”.
where “min” and “max” are valid version numbers. The legacy syntax is a special case of the
extended syntax, keeping backward compatibility. Regarding satisfaction the rules are:
[1] The version has to pass at least one of the listed requirements to be satisfactory.
[2] A version satisfies a “bounded” requirement when
[a] For min equal to the max if, and only if the version is equal to the min.
[b] Otherwise if, and only if the version is greater than or equal to the min, and less
than the max, where both min and max have been padded internally with “a0”. Note
that while the comparison to min is inclusive, the comparison to max is exclusive.
[3] A “min-bounded” requirement is a “bounded” requirement in disguise, with the max part
implicitly specified as the next higher major version number of the min part. A version
satisfies it per the rules above.
[4] A version satisfies a “min-unbound” requirement if, and only if it is greater than or equal
to the min, where the min has been padded internally with “a0”. There is no constraint to
a maximum.
packageprefer ?latest|stable?
With no arguments, the commands returns either “latest” or “stable”, whichever describes the
current mode of selection logic used by packagerequire.
When passed the argument “latest”, it sets the selection logic mode to “latest”.
When passed the argument “stable”, if the mode is already “stable”, that value is kept. If the
mode is already “latest”, then the attempt to set it back to “stable” is ineffective and the mode
value remains “latest”.
When passed any other value as an argument, raise an invalid argument error.
When an interpreter is created, its initial selection mode value is set to “stable” unless the
environment variable TCL_PKG_PREFER_LATEST is set (to any value) or the Tcl package itself is
unstable. Otherwise the initial (and permanent) selection mode value is set to “latest”.