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