1package(n) Tcl Built-In Commands package(n)
2
3
4
5______________________________________________________________________________
6
8 package - Facilities for package loading and version control
9
11 package forget ?package package ...?
12 package ifneeded package version ?script?
13 package names
14 package present package ?requirement...?
15 package present -exact package version
16 package provide package ?version?
17 package require package ?requirement...?
18 package require -exact package version
19 package unknown ?command?
20 package vcompare version1 version2
21 package versions package
22 package vsatisfies version requirement...
23 package prefer ?latest|stable?
24______________________________________________________________________________
25
27 This command keeps a simple database of the packages available for use
28 by the current interpreter and how to load them into the interpreter.
29 It supports multiple versions of each package and arranges for the cor‐
30 rect version of a package to be loaded based on what is needed by the
31 application. This command also detects and reports version clashes.
32 Typically, only the package require and package provide commands are
33 invoked in normal Tcl scripts; the other commands are used primarily
34 by system scripts that maintain the package database.
35
36 The behavior of the package command is determined by its first argu‐
37 ment. The following forms are permitted:
38
39 package forget ?package package ...?
40 Removes all information about each specified package from this
41 interpreter, including information provided by both package
42 ifneeded and package provide.
43
44 package ifneeded package version ?script?
45 This command typically appears only in system configuration
46 scripts to set up the package database. It indicates that a
47 particular version of a particular package is available if
48 needed, and that the package can be added to the interpreter by
49 executing script. The script is saved in a database for use by
50 subsequent package require commands; typically, script sets up
51 auto-loading for the commands in the package (or calls load
52 and/or source directly), then invokes package provide to indi‐
53 cate that the package is present. There may be information in
54 the database for several different versions of a single package.
55 If the database already contains information for package and
56 version, the new script replaces the existing one. If the
57 script argument is omitted, the current script for version ver‐
58 sion of package package is returned, or an empty string if no
59 package ifneeded command has been invoked for this package and
60 version.
61
62 package names
63 Returns a list of the names of all packages in the interpreter
64 for which a version has been provided (via package provide) or
65 for which a package ifneeded script is available. The order of
66 elements in the list is arbitrary.
67
68 package present ?-exact? package ?requirement...?
69 This command is equivalent to package require except that it
70 does not try and load the package if it is not already loaded.
71
72 package provide package ?version?
73 This command is invoked to indicate that version version of
74 package package is now present in the interpreter. It is typi‐
75 cally invoked once as part of an ifneeded script, and again by
76 the package itself when it is finally loaded. An error occurs
77 if a different version of package has been provided by a previ‐
78 ous package provide command. If the version argument is omit‐
79 ted, then the command returns the version number that is cur‐
80 rently provided, or an empty string if no package provide com‐
81 mand has been invoked for package in this interpreter.
82
83 package require package ?requirement...?
84 This command is typically invoked by Tcl code that wishes to use
85 a particular version of a particular package. The arguments
86 indicate which package is wanted, and the command ensures that a
87 suitable version of the package is loaded into the interpreter.
88 If the command succeeds, it returns the version number that is
89 loaded; otherwise it generates an error.
90
91 A suitable version of the package is any version which satisfies
92 at least one of the requirements, per the rules of package vsat‐
93 isfies. If multiple versions are suitable the implementation
94 with the highest version is chosen. This last part is addition‐
95 ally influenced by the selection mode set with package prefer.
96
97 In the “stable” selection mode the command will select the high‐
98 est stable version satisfying the requirements, if any. If no
99 stable version satisfies the requirements, the highest unstable
100 version satisfying the requirements will be selected. In the
101 “latest” selection mode the command will accept the highest ver‐
102 sion satisfying all the requirements, regardless of its stable‐
103 ness.
104
105 If a version of package has already been provided (by invoking
106 the package provide command), then its version number must sat‐
107 isfy the requirements and the command returns immediately. Oth‐
108 erwise, the command searches the database of information pro‐
109 vided by previous package ifneeded commands to see if an accept‐
110 able version of the package is available. If so, the script for
111 the highest acceptable version number is evaluated in the global
112 namespace; it must do whatever is necessary to load the package,
113 including calling package provide for the package. If the pack‐
114 age ifneeded database does not contain an acceptable version of
115 the package and a package unknown command has been specified for
116 the interpreter then that command is evaluated in the global
117 namespace; when it completes, Tcl checks again to see if the
118 package is now provided or if there is a package ifneeded script
119 for it. If all of these steps fail to provide an acceptable
120 version of the package, then the command returns an error.
121
122 package require -exact package version
123 This form of the command is used when only the given version of
124 package is acceptable to the caller. This command is equivalent
125 to package require package version-version.
126
127 package unknown ?command?
128 This command supplies a “last resort” command to invoke during
129 package require if no suitable version of a package can be found
130 in the package ifneeded database. If the command argument is
131 supplied, it contains the first part of a command; when the
132 command is invoked during a package require command, Tcl appends
133 one or more additional arguments giving the desired package name
134 and requirements. For example, if command is foo bar and later
135 the command package require test 2.4 is invoked, then Tcl will
136 execute the command foo bar test 2.4 to load the package. If no
137 requirements are supplied to the package require command, then
138 only the name will be added to invoked command. If the package
139 unknown command is invoked without a command argument, then the
140 current package unknown script is returned, or an empty string
141 if there is none. If command is specified as an empty string,
142 then the current package unknown script is removed, if there is
143 one.
144
145 package vcompare version1 version2
146 Compares the two version numbers given by version1 and version2.
147 Returns -1 if version1 is an earlier version than version2, 0 if
148 they are equal, and 1 if version1 is later than version2.
149
150 package versions package
151 Returns a list of all the version numbers of package for which
152 information has been provided by package ifneeded commands.
153
154 package vsatisfies version requirement...
155 Returns 1 if the version satisfies at least one of the given
156 requirements, and 0 otherwise. Each requirement is allowed to
157 have any of the forms:
158
159 min This form is called “min-bounded”.
160
161 min- This form is called “min-unbound”.
162
163 min-max
164 This form is called “bounded”.
165
166 where “min” and “max” are valid version numbers. The legacy syn‐
167 tax is a special case of the extended syntax, keeping backward
168 compatibility. Regarding satisfaction the rules are:
169
170 [1] The version has to pass at least one of the listed
171 requirements to be satisfactory.
172
173 [2] A version satisfies a “bounded” requirement when
174
175 [a] For min equal to the max if, and only if the ver‐
176 sion is equal to the min.
177
178 [b] Otherwise if, and only if the version is greater
179 than or equal to the min, and less than the max,
180 where both min and max have been padded internally
181 with “a0”. Note that while the comparison to min
182 is inclusive, the comparison to max is exclusive.
183
184 [3] A “min-bounded” requirement is a “bounded” requirement in
185 disguise, with the max part implicitly specified as the
186 next higher major version number of the min part. A ver‐
187 sion satisfies it per the rules above.
188
189 [4] A version satisfies a “min-unbound” requirement if, and
190 only if it is greater than or equal to the min, where the
191 min has been padded internally with “a0”. There is no
192 constraint to a maximum.
193
194 package prefer ?latest|stable?
195 With no arguments, the commands returns either “latest” or “sta‐
196 ble”, whichever describes the current mode of selection logic
197 used by package require.
198
199 When passed the argument “latest”, it sets the selection logic
200 mode to “latest”.
201
202 When passed the argument “stable”, if the mode is already “sta‐
203 ble”, that value is kept. If the mode is already “latest”, then
204 the attempt to set it back to “stable” is ineffective and the
205 mode value remains “latest”.
206
207 When passed any other value as an argument, raise an invalid
208 argument error.
209
210 When an interpreter is created, its initial selection mode value
211 is set to “stable” unless the environment variable TCL_PKG_PRE‐
212 FER_LATEST is set. If that environment variable is defined
213 (with any value) then the initial (and permanent) selection mode
214 value is set to “latest”.
215
217 Version numbers consist of one or more decimal numbers separated by
218 dots, such as 2 or 1.162 or 3.1.13.1. The first number is called the
219 major version number. Larger numbers correspond to later versions of a
220 package, with leftmost numbers having greater significance. For exam‐
221 ple, version 2.1 is later than 1.3 and version 3.4.6 is later than
222 3.3.5. Missing fields are equivalent to zeroes: version 1.3 is the
223 same as version 1.3.0 and 1.3.0.0, so it is earlier than 1.3.1 or
224 1.3.0.2. In addition, the letters “a” (alpha) and/or “b” (beta) may
225 appear exactly once to replace a dot for separation. These letters
226 semantically add a negative specifier into the version, where “a” is
227 -2, and “b” is -1. Each may be specified only once, and “a” or “b” are
228 mutually exclusive in a specifier. Thus 1.3a1 becomes (semantically)
229 1.3.-2.1, 1.3b1 is 1.3.-1.1. Negative numbers are not directly allowed
230 in version specifiers. A version number not containing the letters “a”
231 or “b” as specified above is called a stable version, whereas presence
232 of the letters causes the version to be called is unstable. A later
233 version number is assumed to be upwards compatible with an earlier ver‐
234 sion number as long as both versions have the same major version num‐
235 ber. For example, Tcl scripts written for version 2.3 of a package
236 should work unchanged under versions 2.3.2, 2.4, and 2.5.1. Changes in
237 the major version number signify incompatible changes: if code is writ‐
238 ten to use version 2.1 of a package, it is not guaranteed to work
239 unmodified with either version 1.7.3 or version 3.1.
240
242 The recommended way to use packages in Tcl is to invoke package require
243 and package provide commands in scripts, and use the procedure
244 pkg_mkIndex to create package index files. Once you have done this,
245 packages will be loaded automatically in response to package require
246 commands. See the documentation for pkg_mkIndex for details.
247
249 To state that a Tcl script requires the Tk and http packages, put this
250 at the top of the script:
251
252 package require Tk
253 package require http
254
255 To test to see if the Snack package is available and load if it is
256 (often useful for optional enhancements to programs where the loss of
257 the functionality is not critical) do this:
258
259 if {[catch {package require Snack}]} {
260 # Error thrown - package not found.
261 # Set up a dummy interface to work around the absence
262 } else {
263 # We have the package, configure the app to use it
264 }
265
267 msgcat(n), packagens(n), pkgMkIndex(n)
268
270 package, version
271
272
273
274Tcl 7.5 package(n)