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