Version 2 of Tcl Module basic template

Updated 2010-02-07 07:10:14 by JMN

Sometimes it's convenient to use an existing Tcl module file as a starting point for a new one, or to quickly change a version number for testing.

Below is a basic template for a Tcl .tm file which allows you to change the package name and associated namespace, as well as the version number, simply by renaming the .tm file itself.

As per the standard Tcl module naming convention, save as a file name with the format <modulename>-<version>.tm e.g mymodule-1.0.tm A 'package require mymodule' will now automatically provide the module with version number 1.0 with your functions under the namespace ::mymodule


  namespace eval pkgtemp {
      set modver [file root [file tail [info script]]]
      lassign [split $modver -] ::pkgtemp::ns ::pkgtemp::version
  }
  package provide $::pkgtemp::ns [namespace eval $::pkgtemp::ns {
      variable version $::pkgtemp::version
      set version
  }]
  namespace eval $::pkgtemp::ns {
      #module code  here
  }
  namespace delete pkgtemp

The temporary namespace pkgtemp seems pretty unlikely to collide with existing namespaces - and is simply there to avoid polluting/overwriting any variables an application may have in the global namespace.


See also