Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/package.n @ 25

Last change on this file since 25 was 25, checked in by landauf, 17 years ago

added tcl to libs

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