| 1 |
'\" |
| 2 |
'\" Copyright (c) 1996 Sun Microsystems, Inc. |
| 3 |
'\" |
| 4 |
'\" See the file "license.terms" for information on usage and redistribution |
| 5 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 6 |
'\" |
| 7 |
.TH package n 7.5 Tcl "Tcl Built-In Commands" |
| 8 |
.so man.macros |
| 9 |
.BS |
| 10 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 11 |
.SH NAME |
| 12 |
package \- Facilities for package loading and version control |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
\fBpackage forget\fR ?\fIpackage package ...\fR? |
| 16 |
\fBpackage ifneeded \fIpackage version\fR ?\fIscript\fR? |
| 17 |
\fBpackage names\fR |
| 18 |
\fBpackage present \fIpackage \fR?\fIrequirement...\fR? |
| 19 |
\fBpackage present \-exact \fIpackage version\fR |
| 20 |
\fBpackage provide \fIpackage \fR?\fIversion\fR? |
| 21 |
\fBpackage require \fIpackage \fR?\fIrequirement...\fR? |
| 22 |
\fBpackage require \-exact \fIpackage version\fR |
| 23 |
\fBpackage unknown \fR?\fIcommand\fR? |
| 24 |
\fBpackage vcompare \fIversion1 version2\fR |
| 25 |
\fBpackage versions \fIpackage\fR |
| 26 |
\fBpackage vsatisfies \fIversion requirement...\fR |
| 27 |
\fBpackage prefer \fR?\fBlatest\fR|\fBstable\fR? |
| 28 |
.fi |
| 29 |
.BE |
| 30 |
.SH DESCRIPTION |
| 31 |
.PP |
| 32 |
This command keeps a simple database of the packages available for |
| 33 |
use by the current interpreter and how to load them into the |
| 34 |
interpreter. |
| 35 |
It supports multiple versions of each package and arranges |
| 36 |
for the correct version of a package to be loaded based on what |
| 37 |
is needed by the application. |
| 38 |
This command also detects and reports version clashes. |
| 39 |
Typically, only the \fBpackage require\fR and \fBpackage provide\fR |
| 40 |
commands are invoked in normal Tcl scripts; the other commands are used |
| 41 |
primarily by system scripts that maintain the package database. |
| 42 |
.PP |
| 43 |
The behavior of the \fBpackage\fR command is determined by its first argument. |
| 44 |
The following forms are permitted: |
| 45 |
.TP |
| 46 |
\fBpackage forget\fR ?\fIpackage package ...\fR? |
| 47 |
. |
| 48 |
Removes all information about each specified package from this interpreter, |
| 49 |
including information provided by both \fBpackage ifneeded\fR and |
| 50 |
\fBpackage provide\fR. |
| 51 |
.\" METHOD: ifneeded |
| 52 |
.TP |
| 53 |
\fBpackage ifneeded \fIpackage version\fR ?\fIscript\fR? |
| 54 |
. |
| 55 |
This command typically appears only in system configuration |
| 56 |
scripts to set up the package database. |
| 57 |
It indicates that a particular version of |
| 58 |
a particular package is available if needed, and that the package |
| 59 |
can be added to the interpreter by executing \fIscript\fR. |
| 60 |
The script is saved in a database for use by subsequent |
| 61 |
\fBpackage require\fR commands; typically, \fIscript\fR |
| 62 |
sets up auto-loading for the commands in the package (or calls |
| 63 |
\fBload\fR and/or \fBsource\fR directly), then invokes |
| 64 |
\fBpackage provide\fR to indicate that the package is present. |
| 65 |
There may be information in the database for several different |
| 66 |
versions of a single package. |
| 67 |
If the database already contains information for \fIpackage\fR |
| 68 |
and \fIversion\fR, the new \fIscript\fR replaces the existing |
| 69 |
one. |
| 70 |
If the \fIscript\fR argument is omitted, the current script for |
| 71 |
version \fIversion\fR of package \fIpackage\fR is returned, |
| 72 |
or an empty string if no \fBpackage ifneeded\fR command has |
| 73 |
been invoked for this \fIpackage\fR and \fIversion\fR. |
| 74 |
.\" METHOD: names |
| 75 |
.TP |
| 76 |
\fBpackage names\fR |
| 77 |
. |
| 78 |
Returns a list of the names of all packages in the |
| 79 |
interpreter for which a version has been provided (via |
| 80 |
\fBpackage provide\fR) or for which a \fBpackage ifneeded\fR |
| 81 |
script is available. |
| 82 |
The order of elements in the list is arbitrary. |
| 83 |
.\" METHOD: present |
| 84 |
.TP |
| 85 |
\fBpackage present\fR ?\fB\-exact\fR? \fIpackage\fR ?\fIrequirement...\fR? |
| 86 |
. |
| 87 |
This command is equivalent to \fBpackage require\fR except that it |
| 88 |
does not try and load the package if it is not already loaded. |
| 89 |
.\" METHOD: provide |
| 90 |
.TP |
| 91 |
\fBpackage provide \fIpackage \fR?\fIversion\fR? |
| 92 |
. |
| 93 |
This command is invoked to indicate that version \fIversion\fR |
| 94 |
of package \fIpackage\fR is now present in the interpreter. |
| 95 |
It is typically invoked once as part of an \fBifneeded\fR script, |
| 96 |
and again by the package itself when it is finally loaded. |
| 97 |
An error occurs if a different version of \fIpackage\fR has been |
| 98 |
provided by a previous \fBpackage provide\fR command. |
| 99 |
If the \fIversion\fR argument is omitted, then the command |
| 100 |
returns the version number that is currently provided, or an |
| 101 |
empty string if no \fBpackage provide\fR command has been |
| 102 |
invoked for \fIpackage\fR in this interpreter. |
| 103 |
.\" METHOD: require |
| 104 |
.TP |
| 105 |
\fBpackage require \fR\fIpackage \fR?\fIrequirement...\fR? |
| 106 |
. |
| 107 |
This command is typically invoked by Tcl code that wishes to use |
| 108 |
a particular version of a particular package. The arguments |
| 109 |
indicate which package is wanted, and the command ensures that |
| 110 |
a suitable version of the package is loaded into the interpreter. |
| 111 |
If the command succeeds, it returns the version number that is |
| 112 |
loaded; otherwise it generates an error. |
| 113 |
.RS |
| 114 |
.PP |
| 115 |
A suitable version of the package is any version which satisfies at |
| 116 |
least one of the requirements as defined in the section \fBREQUIREMENT\fR below. |
| 117 |
If multiple versions are suitable the implementation |
| 118 |
with the highest version is chosen. This last part is additionally |
| 119 |
influenced by the selection mode set with \fBpackage prefer\fR. |
| 120 |
.PP |
| 121 |
In the |
| 122 |
.QW stable |
| 123 |
selection mode the command will select the highest |
| 124 |
stable version satisfying the requirements, if any. If no stable |
| 125 |
version satisfies the requirements, the highest unstable version |
| 126 |
satisfying the requirements will be selected. In the |
| 127 |
.QW latest |
| 128 |
selection mode the command will accept the highest version satisfying |
| 129 |
all the requirements, regardless of its stableness. |
| 130 |
.PP |
| 131 |
If a version of \fIpackage\fR has already been provided (by invoking |
| 132 |
the \fBpackage provide\fR command), then its version number must |
| 133 |
satisfy the \fIrequirement\fRs and the command returns immediately. |
| 134 |
Otherwise, the command searches the database of information provided by |
| 135 |
previous \fBpackage ifneeded\fR commands to see if an acceptable |
| 136 |
version of the package is available. |
| 137 |
If so, the script for the highest acceptable version number is evaluated |
| 138 |
in the global namespace; |
| 139 |
it must do whatever is necessary to load the package, |
| 140 |
including calling \fBpackage provide\fR for the package. |
| 141 |
If the \fBpackage ifneeded\fR database does not contain an acceptable |
| 142 |
version of the package and a \fBpackage unknown\fR command has been |
| 143 |
specified for the interpreter then that command is evaluated in the |
| 144 |
global namespace; when |
| 145 |
it completes, Tcl checks again to see if the package is now provided |
| 146 |
or if there is a \fBpackage ifneeded\fR script for it. |
| 147 |
If all of these steps fail to provide an acceptable version of the |
| 148 |
package, then the command returns an error. |
| 149 |
.RE |
| 150 |
.TP |
| 151 |
\fBpackage require \-exact \fIpackage version\fR |
| 152 |
. |
| 153 |
This form of the command is used when only the given \fIversion\fR |
| 154 |
of \fIpackage\fR is acceptable to the caller. This command is |
| 155 |
equivalent to \fBpackage require \fIpackage version\fR-\fIversion\fR. |
| 156 |
.\" METHOD: unknown |
| 157 |
.TP |
| 158 |
\fBpackage unknown \fR?\fIcommand\fR? |
| 159 |
. |
| 160 |
This command supplies a |
| 161 |
.QW "last resort" |
| 162 |
command to invoke during |
| 163 |
\fBpackage require\fR if no suitable version of a package can be found |
| 164 |
in the \fBpackage ifneeded\fR database. |
| 165 |
If the \fIcommand\fR argument is supplied, it contains the first part |
| 166 |
of a command; when the command is invoked during a \fBpackage require\fR |
| 167 |
command, Tcl appends one or more additional arguments giving the desired |
| 168 |
package name and requirements. |
| 169 |
For example, if \fIcommand\fR is \fBfoo bar\fR and later the command |
| 170 |
\fBpackage require test 2.4\fR is invoked, then Tcl will execute |
| 171 |
the command \fBfoo bar test 2.4\fR to load the package. |
| 172 |
If no requirements are supplied to the \fBpackage require\fR command, |
| 173 |
then only the name will be added to invoked command. |
| 174 |
If the \fBpackage unknown\fR command is invoked without a \fIcommand\fR |
| 175 |
argument, then the current \fBpackage unknown\fR script is returned, |
| 176 |
or an empty string if there is none. |
| 177 |
If \fIcommand\fR is specified as an empty string, then the current |
| 178 |
\fBpackage unknown\fR script is removed, if there is one. |
| 179 |
.\" METHOD: vcompare |
| 180 |
.TP |
| 181 |
\fBpackage vcompare \fIversion1 version2\fR |
| 182 |
. |
| 183 |
Compares the two version numbers given by \fIversion1\fR and \fIversion2\fR. |
| 184 |
Returns -1 if \fIversion1\fR is an earlier version than \fIversion2\fR, |
| 185 |
0 if they are equal, and 1 if \fIversion1\fR is later than \fIversion2\fR. |
| 186 |
.\" METHOD: versions |
| 187 |
.TP |
| 188 |
\fBpackage versions \fIpackage\fR |
| 189 |
. |
| 190 |
Returns a list of all the version numbers of \fIpackage\fR |
| 191 |
for which information has been provided by \fBpackage ifneeded\fR |
| 192 |
commands. |
| 193 |
.\" METHOD: vsatisfies |
| 194 |
.TP |
| 195 |
\fBpackage vsatisfies \fIversion requirement...\fR |
| 196 |
. |
| 197 |
Returns 1 if the \fIversion\fR satisfies at least one of the given |
| 198 |
requirements, and 0 otherwise. \fIrequirements\fR are defined in the \fBREQUIREMENT\fR section below. |
| 199 |
.\" METHOD: prefer |
| 200 |
.TP |
| 201 |
\fBpackage prefer \fR?\fBlatest\fR|\fBstable\fR? |
| 202 |
. |
| 203 |
With no arguments, the commands returns either |
| 204 |
.QW latest |
| 205 |
or |
| 206 |
.QW stable , |
| 207 |
whichever describes the current mode of selection logic used by |
| 208 |
\fBpackage require\fR. |
| 209 |
.RS |
| 210 |
.PP |
| 211 |
When passed the argument |
| 212 |
.QW latest , |
| 213 |
it sets the selection logic mode to |
| 214 |
.QW latest . |
| 215 |
.PP |
| 216 |
When passed the argument |
| 217 |
.QW stable , |
| 218 |
if the mode is already |
| 219 |
.QW stable , |
| 220 |
that value is kept. If the mode is already |
| 221 |
.QW latest , |
| 222 |
then the attempt to set it back to |
| 223 |
.QW stable |
| 224 |
is ineffective and the mode value remains |
| 225 |
.QW latest . |
| 226 |
.PP |
| 227 |
When passed any other value as an argument, raise an invalid argument |
| 228 |
error. |
| 229 |
.PP |
| 230 |
When an interpreter is created, its initial selection mode value is set to |
| 231 |
.QW stable |
| 232 |
unless the environment variable \fBTCL_PKG_PREFER_LATEST\fR is set |
| 233 |
(to any value). Otherwise |
| 234 |
the initial (and permanent) selection mode value is set to |
| 235 |
.QW latest . |
| 236 |
.RE |
| 237 |
.SH "VERSION NUMBERS" |
| 238 |
.PP |
| 239 |
Version numbers consist of one or more decimal numbers separated |
| 240 |
by dots, such as 2 or 1.162 or 3.1.13.1. |
| 241 |
The first number is called the major version number. |
| 242 |
Larger numbers correspond to later versions of a package, with |
| 243 |
leftmost numbers having greater significance. |
| 244 |
For example, version 2.1 is later than 1.3 and version |
| 245 |
3.4.6 is later than 3.3.5. |
| 246 |
Missing fields are equivalent to zeroes: version 1.3 is the |
| 247 |
same as version 1.3.0 and 1.3.0.0, so it is earlier than 1.3.1 or 1.3.0.2. |
| 248 |
In addition, the letters |
| 249 |
.QW a |
| 250 |
(alpha) and/or |
| 251 |
.QW b |
| 252 |
(beta) may appear |
| 253 |
exactly once to replace a dot for separation. These letters |
| 254 |
semantically add a negative specifier into the version, where |
| 255 |
.QW a |
| 256 |
is \-2, and |
| 257 |
.QW b |
| 258 |
is \-1. Each may be specified only once, and |
| 259 |
.QW a |
| 260 |
or |
| 261 |
.QW b |
| 262 |
are mutually exclusive in a specifier. Thus 1.3a1 becomes (semantically) |
| 263 |
1.3.\-2.1, 1.3b1 is 1.3.\-1.1. Negative numbers are not directly allowed |
| 264 |
in version specifiers. |
| 265 |
A version number not containing the letters |
| 266 |
.QW a |
| 267 |
or |
| 268 |
.QW b |
| 269 |
as specified |
| 270 |
above is called a \fBstable\fR version, whereas presence of the letters |
| 271 |
causes the version to be called is \fBunstable\fR. |
| 272 |
A later version number is assumed to be upwards compatible with |
| 273 |
an earlier version number as long as both versions have the same |
| 274 |
major version number. |
| 275 |
For example, Tcl scripts written for version 2.3 of a package should |
| 276 |
work unchanged under versions 2.3.2, 2.4, and 2.5.1. |
| 277 |
Changes in the major version number signify incompatible changes: |
| 278 |
if code is written to use version 2.1 of a package, it is not guaranteed |
| 279 |
to work unmodified with either version 1.7.3 or version 3.1. |
| 280 |
.SH "PACKAGE INDICES" |
| 281 |
.PP |
| 282 |
The recommended way to use packages in Tcl is to invoke \fBpackage require\fR |
| 283 |
and \fBpackage provide\fR commands in scripts, and use the procedure |
| 284 |
\fBpkg_mkIndex\fR to create package index files. |
| 285 |
Once you have done this, packages will be loaded automatically |
| 286 |
in response to \fBpackage require\fR commands. |
| 287 |
See the documentation for \fBpkg_mkIndex\fR for details. |
| 288 |
.SH "REQUIREMENT" |
| 289 |
.PP |
| 290 |
A \fIrequirement\fR string checks, if a compatible version number of a package is present. |
| 291 |
Most commands accept a list of requirement strings where the highest suitable version is matched. |
| 292 |
.PP |
| 293 |
Each \fIrequirement\fR is allowed to have any of the forms: |
| 294 |
.RS |
| 295 |
.IP \fImin\fR |
| 296 |
This form is called |
| 297 |
.QW min-bounded . |
| 298 |
.IP \fImin\fB\-\fR |
| 299 |
This form is called |
| 300 |
.QW min-unbound . |
| 301 |
.IP \fImin\fB\-\fImax\fR |
| 302 |
This form is called |
| 303 |
.QW bounded . |
| 304 |
.PP |
| 305 |
where |
| 306 |
.QW \fImin\fR |
| 307 |
and |
| 308 |
.QW \fImax\fR |
| 309 |
are valid version numbers. The legacy syntax is |
| 310 |
a special case of the extended syntax, keeping backward |
| 311 |
compatibility. Regarding satisfaction the rules are: |
| 312 |
.IP [1] |
| 313 |
The \fIversion\fR has to pass at least one of the listed |
| 314 |
\fIrequirement\fRs to be satisfactory. |
| 315 |
.IP [2] |
| 316 |
A version satisfies a |
| 317 |
.QW bounded |
| 318 |
requirement when |
| 319 |
.RS |
| 320 |
.IP [a] |
| 321 |
For \fImin\fR equal to the \fImax\fR if, and only if the \fIversion\fR |
| 322 |
is equal to the \fImin\fR. |
| 323 |
.IP [b] |
| 324 |
Otherwise if, and only if the \fIversion\fR is greater than or equal |
| 325 |
to the \fImin\fR, and less than the \fImax\fR, where both \fImin\fR |
| 326 |
and \fImax\fR have been padded internally with |
| 327 |
.QW a0 . |
| 328 |
Note that while the comparison to \fImin\fR is inclusive, the |
| 329 |
comparison to \fImax\fR is exclusive. |
| 330 |
.RE |
| 331 |
.IP [3] |
| 332 |
A |
| 333 |
.QW min-bounded |
| 334 |
requirement is a |
| 335 |
.QW bounded |
| 336 |
requirement in disguise, |
| 337 |
with the \fImax\fR part implicitly specified as the next higher major |
| 338 |
version number of the \fImin\fR part. A version satisfies it per the |
| 339 |
rules above. |
| 340 |
.IP [4] |
| 341 |
A \fIversion\fR satisfies a |
| 342 |
.QW min-unbound |
| 343 |
requirement if, and only if it is greater than or equal to the |
| 344 |
\fImin\fR, where the \fImin\fR has been padded internally with |
| 345 |
.QW a0 . |
| 346 |
There is no constraint to a maximum. |
| 347 |
.RE |
| 348 |
.SH EXAMPLES |
| 349 |
.PP |
| 350 |
To state that a Tcl script requires the Tk and http packages, put this |
| 351 |
at the top of the script: |
| 352 |
.PP |
| 353 |
.CS |
| 354 |
\fBpackage require\fR Tk |
| 355 |
\fBpackage require\fR http |
| 356 |
.CE |
| 357 |
.PP |
| 358 |
To test to see if the Snack package is available and load if it is |
| 359 |
(often useful for optional enhancements to programs where the loss of |
| 360 |
the functionality is not critical) do this: |
| 361 |
.PP |
| 362 |
.CS |
| 363 |
if {[catch {\fBpackage require\fR Snack}]} { |
| 364 |
# Error thrown - package not found. |
| 365 |
# Set up a dummy interface to work around the absence |
| 366 |
} else { |
| 367 |
# We have the package, configure the app to use it |
| 368 |
} |
| 369 |
.CE |
| 370 |
.SH "SEE ALSO" |
| 371 |
msgcat(n), packagens(n), pkgMkIndex(n) |
| 372 |
.SH KEYWORDS |
| 373 |
package, version |
| 374 |
'\" Local Variables: |
| 375 |
'\" mode: nroff |
| 376 |
'\" End: |