\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#189: Tcl Modules}
\date{March 24, 2004}
\author{Andreas Kupries, Jean-Claude Wippler, Jeff Hobbs, Don Porter, Larry W. Virden, Daniel A. Steffen, Don Porter}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#189}&\textbf{Tcl Modules}\\\hline
Author&

Andreas Kupries \tipmail{akupries}{shaw.ca}
\par
Jean-Claude Wippler \tipmail{jcw}{equi4.com}
\par
Jeff Hobbs \tipmail{jeffh}{activestate.com}
\par
Don Porter \tipmail{dgp}{users.sourceforge.net}
\par
Larry W. Virden \tipmail{lvirden}{yahoo.com}
\par
Daniel A. Steffen \tipmail{das}{users.sourceforge.net}
\par
Don Porter \tipmail{dgp}{users.sf.net}
\\
Created&Wednesday, \(\text{24}^{\text{th}}\) March 2004\\
Type&Project\\
Tcl Version&\(8.5\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.13 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This document describes a new mechanism for the handling of packages by the Tcl Core which differs from the existing system in important details and makes different trade-offs with regard to flexibility of package declarations and to access to the filesystem. This mechanism is called ``Tcl Modules''.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Background and Motivation}

The current mechanism for locating and loading packages employed by the Tcl core is very flexible, but suffers from a number of drawbacks as well. These are at least partially the result of the flexibility, and thus not easily solved without giving up something.

One problem with the current mechanism is that it extensively searches the filesystem for packages, and that it has to actually read a file (\textit{pkgIndex.tcl}) to get the full information for a prospective package. All of these operations take time. The fact that ``index scripts'' are able to extend the list of paths searched tends to heighten this cost as it forces rescans of the filesystem. Installations where directories in the \textit{auto\_path} are large or mounted from remote hosts are hit especially hard by this (network delays). All of this together causes a slow startup of tclsh and Tcl-based applications.

``\textbf{Tcl Modules}'' on the other hand is designed with less flexibility in mind and to allow implementations to glean as much information as possible without having to perform lots of accesses to the filesystem.

Additional benefits of the proposed design are a simplified deployment of packages, akin to the way starkits made application deployment simple, and from that an easier implementation and management of repositories.

It does not come without penalties however.
\begin{itemize}
\item{}
The simplified design has no ``index scripts''. While this does away with extending the list of paths for searching, it also does away with the ability of packages to check preconditions, like the version of the currently executing Tcl interpreter. Dependencies of packages (in module form) on particular versions of Tcl have to be managed differently and outside of them.
\item{}
``Tcl Modules'' is defined to be an extension of the existing package mechanism and does \textit{not} replace it. This means that any failure to find a package as a module \textit{has to} cause a fall back to the regular package mechanism. It also sets a limit on how much of our goals we can reach: searching for packages which are not installed will stay relatively slow, and dominated by the filesystem scan of the regular search. This implies that ``Tcl Modules'' will be best suited in installations where the number of regular packages is low, and contained in a small part of the overall filesystem.

On the gripping hand, the only regular packages required will be packages supporting the virtual filesystems employed by modules (more on that later), so a transformation of a installation based on a set of regular packages to the form above is quite feasible.
\end{itemize}

\section{Specification}

\subsection{Introduction}

Modules are regular Tcl Packages, in a different guise. To ease explanations, first a summary of the existing mechanism:
\begin{itemize}
\item{}
Packages are identified through ``\textit{pkgIndex.tcl}'' files and the ``index script'' they contain. These files are read and define the ``provide script'', which tells Tcl how to actually load the package. In other words, the provide script tells whether to use the ``\textbf{source}'' or ``\textbf{load}'' command, which file to specify as an argument to that command, etc. However as ``\textit{pkgIndex.tcl}'' contains a regular tcl script, it can do more than that and actually influence the environment, i.e., the package search itself, in several ways:
\begin{itemize}
\item{}
It may choose to not register the package if conditions for the package are not met, like being run by a too old version of Tcl.
\item{}
It may extend the list of paths used to search for packages. This implies that a package is able to modify the behaviour of the package search (usually extending the search) even before it is loaded, and even if it will not be loaded at all.
\end{itemize}
\end{itemize}

The above is very flexible, but comes at a price. The filesystem is not only searched, but files have to be read as well to build up the in-memory index of packages. And this is iterated if index files change/extend the list of paths to search.

Tcl Modules simplifies the above considerably, by cutting down on the number of indirections involved. It only searches for module files and records their location, but does not read them. The search is only performed when required, on a limited part of the filesystem. This makes locating and importing packages in module form easier and faster. The price is that packages in module form cannot prevent registration in an interpreter not of their choice, nor can they influence the package search itself before they are actually used.

The remainder of this document will cover the following topics
\begin{itemize}
\item{}
What constitutes a Tcl Module ?
\item{}
How are they found ?
\item{}
How are they indexed, i.e. entered into the package database ?
\end{itemize}

\subsection{Module Definition}

A Tcl Module is a Tcl Package contained in a \textit{single} file, and no other files required by it. This file has to be \textbf{source}able. In other words, a Tcl Module is always imported via:
\begingroup\small\begin{verbatim}
 source module_file
\end{verbatim}
\endgroup

The ``load'' command is not directly used. This restriction is not an actual limitation, as we may believe. Ever since 8.4 the Tcl \textbf{source} command reads only until the first \textasciicircum{}Z character. This allows us to combine an arbitrary Tcl script with arbitrary binary data into one file, where the script processes the attached data in any it chooses to fully import and activate the package. Please read \cite{tip190} ``Implementation Choices for Tcl Modules'' for more explanations of the various choices which are possible.

The name of a module file has to match the regular expression
\begingroup\small\begin{verbatim}
 ([[:alpha:]_][:[:alnum:]_]*)-([[:digit:]].*)\.tm
\end{verbatim}
\endgroup

The first capturing parentheses provides the name of the package, the second clause its version. In addition to matching the pattern, the extracted version number must not raise an error when used in the command
\begingroup\small\begin{verbatim}
 package vcompare $version 0
\end{verbatim}
\endgroup

This additional check has several benefits. The regular expression pattern is a bit simpler, and the full version check is based on the official definition of version numbers used by the Tcl core itself.

\subsection{Finding Modules}

Remember the check for a valid module in last section, and notice that any filename matching this name pattern is going to be treated by the TM system as if it's a Tcl module, whether it really is or not. This means it's a bad idea for any non-Tcl module files that might match that pattern to end up in a directory where TM will be scanning. This suggests that the directory tree for storing Tcl modules ought to be something separate from other parts of the filesystem. This further implies that a new search path over just these separate storage areas would be better than Yet Another Use of \textit{\$::auto\_path}.

Therefore: Modules are searched for in all directories listed in the result of the command ``\textbf{::tcl::tm::path list}'' (See also section 'API to ``Tcl Modules'''). This is called the ``Module path''. Neither ``\textit{auto\_path}'' nor ``\textit{tcl\_pkgPath}'' are used.

All directories on the module path have to obey one restriction:
\begin{itemize}
\item{}
For any two directories, neither is an ancestor directory of the other.
\end{itemize}

This is required to avoid ambiguities in package naming. If for example the two directories
\begingroup\small\begin{verbatim}
 foo/
 foo/cool
\end{verbatim}
\endgroup

were on the path a package named 'cool::ice' could be found via the names 'cool::ice' or 'ice', the latter potentially obscuring a package named 'ice', unqualified.

Before the search is started, the name of the requested package is translated into a partial path, using the following algorithm:
\begin{itemize}
\item{}
All occurrences of '::' in the package name are replaced by the appropriate directory separator character for the platform we are on. On Unix, for example, this is '/'.
\end{itemize}

Example:
\begin{itemize}
\item{}
The requested package is \textit{encoding::base64}. The generated partial path is
\begingroup\small\begin{verbatim}
 encoding/base64
\end{verbatim}
\endgroup
\end{itemize}

After this translation the package is looked for in all module paths, by combining them one-by-one, first to last with the partial path to form a complete search pattern. The exact pattern and mechanism is left unspecified, giving the implementation freedom of choice as to what glob searches to perform, how much of them, and when.

Independent of that, the implemented algorithm has to reject all files where the filename does not match the regular expression given in the previous section. For the remaining files ``provide scripts'' are generated and added to the \textbf{package ifneeded} database.

The algorithm has to fall back to the previous unknown handler when none of the found module files satisfy the request. If the request was satisfied no fall-back is required.

\subsection{Provide and Index Scripts}

Packages in module form have no control over the ``index'' and ``provide script''s entered into the package database for them. For a module file \textit{MF} the ``index script'' is
\begingroup\small\begin{verbatim}
 package ifneeded PNAME PVERSION [list source MF]
\end{verbatim}
\endgroup

and the ``provide script'' embedded in the above is
\begingroup\small\begin{verbatim}
 source MF
\end{verbatim}
\endgroup

Both package name \textbf{PNAME} and package version \textbf{PVERSION} are extracted from the filename \textbf{MF} according to the definition below:
\begingroup\small\begin{verbatim}
 MF = /module_path/PNAME'-PVERSION.tm
\end{verbatim}
\endgroup

Where \textbf{PNAME' }is the partial path of the module as defined in section 'Finding Modules' before, and translated into \textbf{PNAME} by changing all directory separators to '::', and \textbf{module\_path} is the path (from the list of paths to search) that we found the module file under.

\textit{Note} that we are here creating a connection between package names and paths. Tcl is case-sensitive when it comes to comparing package names, but there are filesystems which are not, like NTFS. Luckily these filesystems do store the case of the name, despite not using the information when comparing.

Given the above we allow the names for packages in Tcl modules to have mixed-case, but also require that there are no collisions when comparing names in a case-insensitive manner. In other words, if a package 'Foo' is deployed in the form of a Tcl Module, packages like 'foo', 'fOo', etc. are not allowed anymore.

Regular packages have no problem with the names of their files, as their entry point has a standard name (``\textit{pkgIndex.tcl}'') and its contents can be adjusted according to the filesystem they are stored in.

\subsection{API to "Tcl Modules"}

``Tcl Modules'' is implemented in Tcl, as a new handler command for \textbf{package unknown}. This command calls the previously installed handler when its own search fails, thereby ensuring proper fall-back to the regular package search.

All code and data structures implementing ``Tcl Modules'' reside in the namespace ``\textit{::tcl::tm}''.

A namespace variable holds the list of paths to search for modules, but is not officially exported. All access to this variable is done through the following public commands:
\begin{itemize}
\item{}
\textbf{::tcl::tm::path add} \textit{PATH}

The path is added at the head to the list of module paths.

The command enforces the restriction that no path may be an ancestor directory of any other path on the list. If the new path violates this restriction an error will be raised.

If the path is already present as is, no error will be raised and no action will be taken.

Paths are searched in the order of their appearance in the list. As they are added to the front of the list they are searched in reverse order of addition. In other words, the paths added last are looked at first.
\item{}
\textbf{::tcl::tm::path remove} \textit{PATH}

Removes the path from the list of module paths. The command is silently ignored if the path is not on the list.
\item{}
\textbf{::tcl::tm::path list}

Returns a list containing all registered module paths, in the order that they are searched for modules.
\item{}
\textbf{::tcl::tm::roots} \textit{PATH\_LIST}

Similar to \textit{path add}, and layered on top of it. This command takes a list of paths, extends each with \textit{tclX/site-tcl}, and \textit{tclX/X.y}, for major version X of the tcl interpreter and minor version y less than or equal to the minor version of the interpreter, and adds the resulting set of paths to the list of paths to search.

This command is used internally by the system to set up the system-specific default paths. See section \textit{Defaults} for their definition, and that their structure matches what this command does.

The command has been exposed to allow a buildsystem to define additional root paths beyond those defined by this document.
\end{itemize}

We do \textit{not} provide APIs for rescanning directories, clearing internal state and such. The official interface to this functionality is ``package forget'' and special interfaces are neither required nor desirable.

\section{Discussion}

\subsection{Restriction to "source"}

This has already been discussed in the specification above.

For more discussion I again refer to \cite{tip190} ``Implementation Choices for Tcl Modules'' which explains the various implementation choices in much more detail.

\subsection{Preconditions}

It has already been mentioned in section 'Background and Motivation' that preconditions in ``index scripts'' are lost, one of the penalties of the simplified scheme specified here.

Their existence was most important to installations with multiple versions of Tcl coexisting with each other as they could share the directory hierarchy containing packages between the various Tcl cores. This is not possible anymore, at least not in a simple manner.

For the majority of installations however, i.e. those without only one version of Tcl installed, or controlled environments like the inside of starkits and starpacks, this loss is irrelevant and of no consequence.

For more discussion please see \cite{tip191} ``Managing Tcl Package and Modules in a Multi-Version Environment'' which explains the various choices a sysadmin has in much more detail.

\subsection{Package Metadata}

An area possibly made harder by Tcl Modules is the storage and query of package metadata. \cite{tip59} was one way of handling such information, by storing them in the binary library of packages which have such. Another approach was to store them in the package index script, using a hypothetical \textbf{package about} command.

The latter approach has the definite advantage that it was possible to query the database of metadata for a particular package without having to actually load said package, as a load may fail if the Tcl shell used to query the database does not fulfil the preconditions for that package.

Both approaches listed above assume that it makes sense to query the database of metadata for all installed packages from a plain Tcl shell. In other words, to use the standard Tcl shell also as the tool to directly manage an installation.

It is possible to extend the proposal made in this document to handle metadata as well. We already reserved the namespace \textbf{::tcl::tm} for use by us, so it is no problem to extend the public API with commands to locate all installed packages, their metadata, and to perform queries based on this. This will require an additional specification as to how metadata is stored in/by Tcl Modules, and it will have to be understood that these extended management operations can take considerably more time than a \textbf{package require}, as they will have to scan all defined search paths and all their sub directories for Tcl Modules, and have to extract the metadata itself as well.

\subsection{Deployment}

The fact that a Tcl Module consists only of a single file makes its deployment quite easy. We only have to ensure correct placement in one of the searched directories when installing it locally, but nothing more.

Regarding the usage of Tcl Modules in a wrapped application, please see \cite{tip190} ``Implementation Choices for Tcl Modules''. This is highly dependent on the implementation chosen for a specific Tcl Module and thus not discussed here, but in the referred document.

\subsection{Package Repositories}

At a very basic level, the physical storage, any directory tree containing properly placed files for a number of modules can serve as a package repository for the modules in it. In other words, from that point of view an installation is virtually indistinguishable from a repository, and their creation and maintenance is very easy

Note however that the higher levels of a repository, like indexing package metadata in general, or dependence tracking in particular, licensing, documentation, etc. are not addressed here and by this.

This requires standards for package metadata, format and content, topics with which this document will not deal.

\subsection{Defaults}

The default list of paths on the module path is computed by a tclsh as follows, where \textit{X} is the major version of the Tcl interpreter and \textit{y} is less than or equal to the minor version of the Tcl interpreter.
\begin{itemize}
\item{}
System specific paths
\begin{itemize}
\item{}
\textbf{file normalize} [\textbf{info library}]/../tcl\textit{X}/\textit{X}.\textit{y}

In other words, the interpreter will look into a directory specified by its major version and whose minor versions are less than or equal to the minor version of the interpreter.
\begin{description}
\item[Example]
For Tcl 8.4 the paths searched are
\end{description}
\begin{itemize}
\item{}
[\textbf{info library}]/../tcl8/8.4
\item{}
[\textbf{info library}]/../tcl8/8.3
\item{}
[\textbf{info library}]/../tcl8/8.2
\item{}
[\textbf{info library}]/../tcl8/8.1
\item{}
[\textbf{info library}]/../tcl8/8.0
\end{itemize}

This definition assumes that a package defined for Tcl \textit{X.y} can also be used by all interpreters which have the same major number \textit{X} and a minor number greater than \textit{y}.
\item{}
\textbf{file normalize} \textit{EXEC}/tcl\textit{X}/\textit{X}.\textit{y}

Where \textit{EXEC} is [\textbf{file normalize} [\textbf{info nameofexecutable}]/../lib] or [\textbf{file normalize} [\textbf{::tcl::pkgconfig get} libdir,runtime]]

This sets of paths is handled equivalently to the set coming before, except that it is anchored in \textit{EXEC\_PREFIX}. For a build with \textit{PREFIX} = \textit{EXEC\_PREFIX} the two sets are identical.
\end{itemize}
\item{}
Site specific paths.
\begin{itemize}
\item{}
\textbf{file normalize} [\textbf{info library}]/../tcl\textit{X}/site-tcl
\end{itemize}
\item{}
User specific paths.
\begin{itemize}
\item{}
\textbf{\$::env}(TCL\textit{X}.\textit{y}\_TM\_PATH)

A list of paths, separated by either \textbf{:} (Unix) or \textbf{;} (Windows). This is user and site specific as this environment variable can be set not only by the user's profile, but by system configuration scripts as well.

These paths are seen and therefore shared by all Tcl shells in the \textbf{\$::env}(PATH) of the user.

Note that \textit{X} and \textit{y} follow the general rules set out above. In other words, Tcl 8.4, for example, will look at these 5 environment variables
\begin{itemize}
\item{}
\textbf{\$::env}(TCL8.4\_TM\_PATH)
\item{}
\textbf{\$::env}(TCL8.3\_TM\_PATH)
\item{}
\textbf{\$::env}(TCL8.2\_TM\_PATH)
\item{}
\textbf{\$::env}(TCL8.1\_TM\_PATH)
\item{}
\textbf{\$::env}(TCL8.0\_TM\_PATH)
\end{itemize}
\end{itemize}
\end{itemize}

\textit{All} the default paths are added to the module path, even those paths which do not exist. Non-existent paths are filtered out during actual searches. This enables a user to create one of the paths searched when needed and all running applications will automatically pick up any modules placed in them.

The paths are added in the order as they are listed above, and for lists of paths defined by an environment variable in the order they are found in the variable.

\subsection{Installation}

The installation of a Tcl module for a particular interpreter is basically done like this:
\begingroup\small\begin{verbatim}
 #! /path/to/chosen/tclsh
 # First argument is the name of the module.
 # Second argument is the base filename
 set mpaths [::tcl::tm::path list]
 ... remove all paths the user has no write permissions for.
 ... throw an error if there are no paths left.
 ... provide the user with some UI if more than one path is left
 ... so that she can select the path to use.
 set selmpath [ui_select $mpaths]
 file copy [lindex $argv 1] \
     [file join $selmpath \
     [file dirname [string map {:: /} \
     [lindex $argv 0]]]]
\end{verbatim}
\endgroup

\section{Glossary}

The following terms and definitions are used throughout the document
\begin{itemize}
\item{}
\textit{index script}

A script used to index a package, or not. Usually contained in a file named ``\textit{pkgIndex.tcl}''. Can check preconditions for a package and contains package specific code for setting up the package specific \textit{provide script}.
\item{}
\textit{provide script}

This is a package specific script and tells Tcl exactly how to import it. In the existing package system it is generated and registered by the \textit{index script}. Tcl Modules on the other hand generates it based on information gleaned from filenames.
\end{itemize}

\section{Reference Implementation}

A reference implementation is available in Patch 942881 [\url{http://sf.net/tracker/?func=detail&aid=942881&group_id=10894&atid=310894}]

\section{Questions}

\section{Comments}

[ Add comments on the document here ]

A feature asked for during discussion is to allow a directory as a Tcl Module. I am opposed to this, because behind Tcl Modules is the same idea/vision as for starkit and starpacks, namely that of deploying something in the simplest possible manner, without any overhead. Sometimes I call Tcl Modules package kits, short pakits (and then twist that then spoken into 'packet' :). [\url{http://groups.google.ca/groups?hl=en&lr=&ie=UTF-8&frame=right&th=78764d499cc4e4a&seekm=c6tshf030c6\%40news4.newsguy.com\#link19}]

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\begin{thebibliography}{TIP \#3}
\addcontentsline{toc}{section}{References}
\bibitem[TIP \#59]{tip59}
Andreas Kupries,
\emph{Embed Build Information in Tcl Binary Library},
on-line at \url{http://purl.org/tcl/tip/59.html}
\bibitem[TIP \#190]{tip190}
Andreas Kupries,
Jean-Claude Wippler,
Jeff Hobbs,
\emph{Implementation Choices for Tcl Modules},
on-line at \url{http://purl.org/tcl/tip/190.html}
\bibitem[TIP \#191]{tip191}
Andreas Kupries,
Joe English,
Larry Virden,
\emph{Managing Tcl Packages and Modules in a Multi-Version Environment},
on-line at \url{http://purl.org/tcl/tip/191.html}
\end{thebibliography}
\end{document}
