ZetCode

Tcl package Command

last modified April 3, 2025

The Tcl package command provides a mechanism for loading and managing extensions and libraries. It's essential for modular Tcl programming. The command helps organize code into reusable components.

Basic Definition

The package command manages Tcl packages, which are collections of code that can be loaded on demand. Packages help organize large applications.

Key subcommands include require, provide, ifneeded, and versions. Packages are identified by name and version numbers.

Basic Package Require

This shows the simplest usage of package require to load a package.

basic_require.tcl
package require Tk
puts "Tk version: [package present Tk]"

This loads the Tk package and prints its version. The package present command checks if a package is loaded and returns its version.

Package Provide

This demonstrates how to create and provide your own package in Tcl.

mypackage.tcl
package provide mypackage 1.0

proc greet {name} {
    return "Hello, $name!"
}
use_mypackage.tcl
package require mypackage 1.0
puts [greet "World"]

The first file defines a package with version 1.0 and provides a simple proc. The second file requires and uses the package. The package must be in Tcl's path.

Package Version Requirements

You can specify version requirements when requiring packages.

version_require.tcl
package require Tcl 8.6
package require Tk 8.6
puts "Running Tcl [info patchlevel] with Tk [package present Tk]"

This ensures specific minimum versions of Tcl and Tk are loaded. The script will fail if these versions aren't available. Version numbers use major.minor format.

Package ifneeded

The package ifneeded command registers how to load a package.

register_package.tcl
package ifneeded mylib 2.0 {
    source [file join $dir mylib.tcl]
}
use_registered.tcl
package require mylib 2.0

The first script registers how to load mylib version 2.0 when requested. The second script requires the package, triggering the loading mechanism. This is typically used in pkgIndex.tcl files.

Package Namespace

Packages often use namespaces to avoid naming conflicts.

namespaced_package.tcl
package provide myns 1.0

namespace eval ::myns {
    variable counter 0
    
    proc increment {} {
        variable counter
        return [incr counter]
    }
}
use_namespaced.tcl
package require myns 1.0
puts [::myns::increment]
puts [::myns::increment]

This creates a package with its own namespace. The counter variable is protected within the namespace. The package provides the increment procedure.

Package Versions

You can query available package versions before loading.

package_versions.tcl
puts "Available Tk versions: [package versions Tk]"
set latest [lindex [lsort -decreasing [package versions Tk]] 0]
package require Tk $latest
puts "Loaded Tk version: [package present Tk]"

This lists all available Tk versions, then loads the latest one. The package versions command returns all known versions of a package.

Best Practices

This tutorial covered the Tcl package command with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Tcl Tutorials.