Version 57 of package

Updated 2012-12-14 05:12:53 by pooryorick

Summary

This Tcl command is used for working with versioned libraries that provide functionality to Tcl and Tk. It is structured into subcommands.

Synopsis

package forget ?package package ...?
package ifneeded package version ?script?
package names
package present ?-exact? package ?version?
package provide package ?version?
package require ?-exact? package ?version?
package unknown ?command?
package vcompare version1 version2
package versions package
package vsatisfies version1 version2

http://www.purl.org/tcl/home/man/tcl8.5/TclCmd/package.htm

See Also

User Guide
Developer Guide
extension
pkg_mkIndex
(deprecated)
Using Tk as a loadable package
Where does the package command find the packages it seeks?
package management
Tcl syntax

MGS 2004/02/10 - In my packages I create a namespace array variable called pkginfo which contains values for package, version, script, directory, name, description, author, email, date, copyright, etc. Perhaps a package source command could be useful here (to return the directory in which the requested package's pkgIndex.tcl file is/was found) ?

LV 2007 Nov 16 - it would seem to me that having some code (in my opinion as part of the core) to which best practices could refer would be ideal. Then it becomes a matter of going from package to package, suggesting that maintainers add a line to their package to register the appropriate information.


[Is there no succinct reference that explains the crucial role of pkgIndex.tcl?!? CL will return to this, if no one else does.]

DGP --- The man page for pkg_mkIndex, http://www.purl.org/tcl/home/man/tcl8.5/TclCmd/pkgMkIndex.htm covers the basics in the HOW IT WORKS section.

[Good point.] It's crucial that package authors understand pkgIndex.tcl as a keyword in package preparation.

DGP --- For now, yes, but stay tuned for better ideas. We don't want to be chained to pkgIndex.tcl files forever.

See also pkg_mkIndex pitfalls.


[One summary statement: package use currently requires comprehension of pkgIndex.tcl (see Magic names).]


Don Porter advises, "Anyone writing packages should read William Duquette's tutorial on 'Namespaces and Packages' [L1 ] first."

Also, note that Don made a presentation on how package unknown works and you can find the slides at [L2 ]. A current version of the work appears at http://math.nist.gov/~DPorter/tcltk/package/ .


Don writes, "The main weakness is that the [package] system does not keep track of what the interdependencies are. We get "can't find package foo" instead of 'could not load package foo 1.3 because it depends on bar 2.1, and that is not installed'." CL agrees with this, and complains that package authorship is clumsy and error-prone. Perhaps more on this subject will appear in October 2001.

LV Another common encounter is getting "can't find" and the cause is that the directory in which it was installed isn't on $auto_path , or perhaps the pkgIndex.tcl contains code that just doesn't work. In fact, I've seen cases where syntax code in one package's pkgIndex.tcl caused tcl problems when some other package was required....



JCW was once heard to conclude, Package require is a contract to make a certain API available, not a procedural-call-with-side-effects in disguise - IMNSHO.


See also On Namespaces And Packages [L3 ] and Programmation modulaire en Tcl/Tk [L4 ].


List of currently loaded packages:

proc packages {} {
    # returns the list of presently loaded packages
    set res {}
    foreach i [package names] {
        if {[string length [package provide $i]]} {
            lappend res $i
        }
    }
    set res
} ;# RS

LV Is there a way to modify this packages proc so that it also reports the version of the package that was loaded?

Lars H: Yes, simply change the

lappend res $i

line to

lappend res $i [package provide $i]

Started a new page on package equivalences to sort out issues regarding packages which need to co-exist, such as binaries with a fallback to a pure Tcl implementation. -jcw


See also Tcl modules for possibly new alternatives to packages. Then there is kitten, which is an example of storing packages in a single starkit which is easier to ship around.


RS 2006-01-29: Here's a package version retriever that returns a list, which can be put into a dict or array:

proc packages {{pattern *}} {
        catch {package require ""}
        set res {}
        foreach p [lsort [package names]] {
                if [string match $pattern $p] {
                        lappend res $p [package versions $p]
                }
        }
        set res
}

Incidentally, packages Tcl and ActiveTcl report {} at package versions, seen on 8.4.5 and 8.4.12. At least the former could do the same as what info patchlevel does... Tk says 8.4 at least, but there are many precedents of packaging reporting 3-part version numbers, Mk4tcl even does four: 2.4.9.2. Test:

% foreach {p v} [packages T*] {puts "$p : $v"}
Tcl :
TclScript : 1.0
Tclx : 8.4
Tk : 8.4
Tkhtml : 2.0
Tktable : 2.8
Tkx : 8.3
Trf : 2.1

LV 2007 August 30 Is it possible at this time to distribute a Tcl package as a single starkit, and have that single file be recognized by a developer/user's code? If so, what steps would the package developer need to follow, and what steps would the receiver of the starkit have to follow, for this to work?


hat0 If you're looking for a useful guide to making packages for your own system or distribution, the Tcl tutorial has an excellent step-by-step guide to the process here: http://www.tcl.tk/man/tcl8.5/tutorial/Tcl31.html