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 ?-exact? package ?version?
15 package provide package ?version?
16 package require ?-exact? package ?version?
17 package unknown ?command?
18 package vcompare version1 version2
19 package versions package
20 package vsatisfies version1 version2
21_________________________________________________________________
22
23
25 This command keeps a simple database of the packages available for use
26 by the current interpreter and how to load them into the interpreter.
27 It supports multiple versions of each package and arranges for the cor‐
28 rect version of a package to be loaded based on what is needed by the
29 application. This command also detects and reports version clashes.
30 Typically, only the package require and package provide commands are
31 invoked in normal Tcl scripts; the other commands are used primarily
32 by system scripts that maintain the package database.
33
34 The behavior of the package command is determined by its first argu‐
35 ment. The following forms are permitted:
36
37 package forget ?package package ...?
38 Removes all information about each specified package from this
39 interpreter, including information provided by both package
40 ifneeded and package provide.
41
42 package ifneeded package version ?script?
43 This command typically appears only in system configuration
44 scripts to set up the package database. It indicates that a
45 particular version of a particular package is available if
46 needed, and that the package can be added to the interpreter by
47 executing script. The script is saved in a database for use by
48 subsequent package require commands; typically, script sets up
49 auto-loading for the commands in the package (or calls load
50 and/or source directly), then invokes package provide to indi‐
51 cate that the package is present. There may be information in
52 the database for several different versions of a single package.
53 If the database already contains information for package and
54 version, the new script replaces the existing one. If the
55 script argument is omitted, the current script for version ver‐
56 sion of package package is returned, or an empty string if no
57 package ifneeded command has been invoked for this package and
58 version.
59
60 package names
61 Returns a list of the names of all packages in the interpreter
62 for which a version has been provided (via package provide) or
63 for which a package ifneeded script is available. The order of
64 elements in the list is arbitrary.
65
66 package present ?-exact? package ?version?
67 This command is equivalent to package require except that it
68 does not try and load the package if it is not already loaded.
69
70 package provide package ?version?
71 This command is invoked to indicate that version version of
72 package package is now present in the interpreter. It is typi‐
73 cally invoked once as part of an ifneeded script, and again by
74 the package itself when it is finally loaded. An error occurs
75 if a different version of package has been provided by a previ‐
76 ous package provide command. If the version argument is omit‐
77 ted, then the command returns the version number that is cur‐
78 rently provided, or an empty string if no package provide com‐
79 mand has been invoked for package in this interpreter.
80
81 package require ?-exact? package ?version?
82 This command is typically invoked by Tcl code that wishes to use
83 a particular version of a particular package. The arguments
84 indicate which package is wanted, and the command ensures that a
85 suitable version of the package is loaded into the interpreter.
86 If the command succeeds, it returns the version number that is
87 loaded; otherwise it generates an error. If both the -exact
88 switch and the version argument are specified then only the
89 given version is acceptable. If -exact is omitted but version
90 is specified, then versions later than version are also accept‐
91 able as long as they have the same major version number as ver‐
92 sion. If both -exact and version are omitted then any version
93 whatsoever is acceptable. If a version of package has already
94 been provided (by invoking the package provide command), then
95 its version number must satisfy the criteria given by -exact and
96 version and the command returns immediately. Otherwise, the
97 command searches the database of information provided by previ‐
98 ous package ifneeded commands to see if an acceptable version of
99 the package is available. If so, the script for the highest
100 acceptable version number is evaluated in the global namespace;
101 it must do whatever is necessary to load the package, including
102 calling package provide for the package. If the package
103 ifneeded database does not contain an acceptable version of the
104 package and a package unknown command has been specified for the
105 interpreter then that command is evaluated in the global names‐
106 pace; when it completes, Tcl checks again to see if the package
107 is now provided or if there is a package ifneeded script for it.
108 If all of these steps fail to provide an acceptable version of
109 the package, then the command returns an error.
110
111 package unknown ?command?
112 This command supplies a ``last resort'' command to invoke during
113 package require if no suitable version of a package can be found
114 in the package ifneeded database. If the command argument is
115 supplied, it contains the first part of a command; when the
116 command is invoked during a package require command, Tcl appends
117 two additional arguments giving the desired package name and
118 version. For example, if command is foo bar and later the com‐
119 mand package require test 2.4 is invoked, then Tcl will execute
120 the command foo bar test 2.4 to load the package. If no version
121 number is supplied to the package require command, then the ver‐
122 sion argument for the invoked command will be an empty string.
123 If the package unknown command is invoked without a command
124 argument, then the current package unknown script is returned,
125 or an empty string if there is none. If command is specified as
126 an empty string, then the current package unknown script is
127 removed, if there is one.
128
129 package vcompare version1 version2
130 Compares the two version numbers given by version1 and version2.
131 Returns -1 if version1 is an earlier version than version2, 0 if
132 they are equal, and 1 if version1 is later than version2.
133
134 package versions package
135 Returns a list of all the version numbers of package for which
136 information has been provided by package ifneeded commands.
137
138 package vsatisfies version1 version2
139 Returns 1 if scripts written for version2 will work unchanged
140 with version1 (i.e. version1 is equal to or greater than ver‐
141 sion2 and they both have the same major version number), 0 oth‐
142 erwise.
143
145 Version numbers consist of one or more decimal numbers separated by
146 dots, such as 2 or 1.162 or 3.1.13.1. The first number is called the
147 major version number. Larger numbers correspond to later versions of a
148 package, with leftmost numbers having greater significance. For exam‐
149 ple, version 2.1 is later than 1.3 and version 3.4.6 is later than
150 3.3.5. Missing fields are equivalent to zeroes: version 1.3 is the
151 same as version 1.3.0 and 1.3.0.0, so it is earlier than 1.3.1 or
152 1.3.0.2. A later version number is assumed to be upwards compatible
153 with an earlier version number as long as both versions have the same
154 major version number. For example, Tcl scripts written for version 2.3
155 of a package should work unchanged under versions 2.3.2, 2.4, and
156 2.5.1. Changes in the major version number signify incompatible
157 changes: if code is written to use version 2.1 of a package, it is not
158 guaranteed to work unmodified with either version 1.7.3 or version 3.1.
159
161 The recommended way to use packages in Tcl is to invoke package require
162 and package provide commands in scripts, and use the procedure
163 pkg_mkIndex to create package index files. Once you've done this,
164 packages will be loaded automatically in response to package require
165 commands. See the documentation for pkg_mkIndex for details.
166
168 To state that a Tcl script requires the Tk and http packages, put this
169 at the top of the script:
170 package require Tk
171 package require http
172
173 To test to see if the Snack package is available and load if it is
174 (often useful for optional enhancements to programs where the loss of
175 the functionality is not critical) do this:
176 if {[catch {package require Snack}]} {
177 # We have the package, configure the app to use it
178 } else {
179 # Set up a dummy interface to work around the absence
180 }
181
182
184 msgcat(n), packagens(n), pkgMkIndex(n)
185
186
188 package, version
189
190
191
192Tcl 7.5 package(n)