1tm(n) Tcl Built-In Commands tm(n)
2
3
4
5______________________________________________________________________________
6
8 tm - Facilities for locating and loading of Tcl Modules
9
11 ::tcl::tm::path add ?path...?
12 ::tcl::tm::path remove ?path...?
13 ::tcl::tm::path list
14 ::tcl::tm::roots ?path...?
15_________________________________________________________________
16
18 This document describes the facilities for locating and loading Tcl
19 Modules (see MODULE DEFINITION for the definition of a Tcl Module).
20 The following commands are supported:
21
22 ::tcl::tm::path add ?path...?
23 The paths are added at the head to the list of module paths, in
24 order of appearance. This means that the last argument ends up
25 as the new head of the list.
26
27 The command enforces the restriction that no path may be an
28 ancestor directory of any other path on the list. If any of the
29 new paths violates this restriction an error will be raised,
30 before any of the paths have been added. In other words, if only
31 one path argument violates the restriction then none will be
32 added.
33
34 If a path is already present as is, no error will be raised and
35 no action will be taken.
36
37 Paths are searched later in the order of their appearance in the
38 list. As they are added to the front of the list they are
39 searched in reverse order of addition. In other words, the paths
40 added last are looked at first.
41
42 ::tcl::tm::path remove ?path...?
43 Removes the paths from the list of module paths. The command
44 silently ignores all paths which are not on the list.
45
46 ::tcl::tm::path list
47 Returns a list containing all registered module paths, in the
48 order that they are searched for modules.
49
50 ::tcl::tm::roots ?path...?
51 Similar to path add, and layered on top of it. This command
52 takes a list of paths, extends each with “tclX/site-tcl”, and
53 “tclX/X.y”, for major version X of the Tcl interpreter and minor
54 version y less than or equal to the minor version of the inter‐
55 preter, and adds the resulting set of paths to the list of paths
56 to search.
57
58 This command is used internally by the system to set up the sys‐
59 tem-specific default paths.
60
61 The command has been exposed to allow a build system to define
62 additional root paths beyond those described by this document.
63
65 A Tcl Module is a Tcl Package contained in a single file, and no other
66 files required by it. This file has to be sourceable. In other words, a
67 Tcl Module is always imported via:
68 source module_file
69
70 The load command is not directly used. This restriction is not an
71 actual limitation, as some may believe. Ever since 8.4 the Tcl source
72 command reads only until the first ^Z character. This allows us to com‐
73 bine an arbitrary Tcl script with arbitrary binary data into one file,
74 where the script processes the attached data in any it chooses to fully
75 import and activate the package.
76
77 The name of a module file has to match the regular expression:
78 ([_[:alpha:]][:_[:alnum:]]*)-([[:digit:]].*)\.tm
79
80 The first capturing parentheses provides the name of the package, the
81 second clause its version. In addition to matching the pattern, the
82 extracted version number must not raise an error when used in the com‐
83 mand:
84 package vcompare $version 0
85
87 The directory tree for storing Tcl modules is separate from other parts
88 of the filesystem and independent of auto_path.
89
90 Tcl Modules are searched for in all directories listed in the result of
91 the command ::tcl::tm::path list. This is called the Module path. Nei‐
92 ther the auto_path nor the tcl_pkgPath variables are used. All direc‐
93 tories on the module path have to obey one restriction:
94
95 For any two directories, neither is an ancestor directory of the
96 other.
97
98 This is required to avoid ambiguities in package naming. If for example
99 the two directories “foo/” and “foo/cool” were on the path a package
100 named cool::ice could be found via the names cool::ice or ice, the lat‐
101 ter potentially obscuring a package named ice, unqualified.
102
103 Before the search is started, the name of the requested package is
104 translated into a partial path, using the following algorithm:
105
106 All occurrences of “::” in the package name are replaced by the
107 appropriate directory separator character for the platform we
108 are on. On Unix, for example, this is “/”.
109
110 Example:
111
112 The requested package is encoding::base64. The generated partial
113 path is “encoding/base64”.
114
115 After this translation the package is looked for in all module paths,
116 by combining them one-by-one, first to last with the partial path to
117 form a complete search pattern. Note that the search algorithm rejects
118 all files where the filename does not match the regular expression
119 given in the section MODULE DEFINITION. For the remaining files provide
120 scripts are generated and added to the package ifneeded database.
121
122 The algorithm falls back to the previous unknown handler when none of
123 the found module files satisfy the request. If the request was satis‐
124 fied the fall-back is ignored.
125
126 Note that packages in module form have no control over the index and
127 provide scripts entered into the package database for them. For a mod‐
128 ule file MF the index script is always:
129 package ifneeded PNAME PVERSION [list source MF]
130 and the provide script embedded in the above is:
131 source MF
132
133 Both package name PNAME and package version PVERSION are extracted from
134 the filename MF according to the definition below:
135 MF = /module_path/PNAME′-PVERSION.tm
136
137 Where PNAME′ is the partial path of the module as defined in section
138 FINDING MODULES, and translated into PNAME by changing all directory
139 separators to “::”, and module_path is the path (from the list of paths
140 to search) that we found the module file under.
141
142 Note also that we are here creating a connection between package names
143 and paths. Tcl is case-sensitive when it comes to comparing package
144 names, but there are filesystems which are not, like NTFS. Luckily
145 these filesystems do store the case of the name, despite not using the
146 information when comparing.
147
148 Given the above we allow the names for packages in Tcl modules to have
149 mixed-case, but also require that there are no collisions when compar‐
150 ing names in a case-insensitive manner. In other words, if a package
151 Foo is deployed in the form of a Tcl Module, packages like foo, fOo,
152 etc. are not allowed anymore.
153
155 The default list of paths on the module path is computed by a tclsh as
156 follows, where X is the major version of the Tcl interpreter and y is
157 less than or equal to the minor version of the Tcl interpreter.
158
159 All the default paths are added to the module path, even those paths
160 which do not exist. Non-existent paths are filtered out during actual
161 searches. This enables a user to create one of the paths searched when
162 needed and all running applications will automatically pick up any mod‐
163 ules placed in them.
164
165 The paths are added in the order as they are listed below, and for
166 lists of paths defined by an environment variable in the order they are
167 found in the variable.
168
169 SYSTEM SPECIFIC PATHS
170 file normalize [info library]/../tclX/X.y
171 In other words, the interpreter will look into a directory spec‐
172 ified by its major version and whose minor versions are less
173 than or equal to the minor version of the interpreter.
174
175 For example for Tcl 8.4 the paths searched are:
176 [info library]/../tcl8/8.4
177 [info library]/../tcl8/8.3
178 [info library]/../tcl8/8.2
179 [info library]/../tcl8/8.1
180 [info library]/../tcl8/8.0
181
182 This definition assumes that a package defined for Tcl X.y can
183 also be used by all interpreters which have the same major num‐
184 ber X and a minor number greater than y.
185
186 file normalize EXEC/tclX/X.y
187 Where EXEC is file normalize [info nameofexecutable]/../lib or
188 file normalize [::tcl::pkgconfig get libdir,runtime]
189
190 This sets of paths is handled equivalently to the set coming
191 before, except that it is anchored in EXEC_PREFIX. For a build
192 with PREFIX = EXEC_PREFIX the two sets are identical.
193
194 SITE SPECIFIC PATHS
195 file normalize [info library]/../tclX/site-tcl
196 Note that this is always a single entry because X is always a
197 specific value (the current major version of Tcl).
198
199 USER SPECIFIC PATHS
200 $::env(TCLX_y_TM_PATH)
201 A list of paths, separated by either : (Unix) or ; (Windows).
202 This is user and site specific as this environment variable can
203 be set not only by the user's profile, but by system configura‐
204 tion scripts as well.
205
206 $::env(TCLX.y_TM_PATH)
207 Same meaning and content as the previous variable. However the
208 use of dot '.' to separate major and minor version number makes
209 this name less to non-portable and its use is discouraged. Sup‐
210 port of this variable has been kept only for backward compati‐
211 bility with the original specification, i.e. TIP 189.
212
213 These paths are seen and therefore shared by all Tcl shells in the
214 $::env(PATH) of the user.
215
216 Note that X and y follow the general rules set out above. In other
217 words, Tcl 8.4, for example, will look at these 5 environment vari‐
218 ables:
219 $::env(TCL8.4_TM_PATH) $::env(TCL8_4_TM_PATH)
220 $::env(TCL8.3_TM_PATH) $::env(TCL8_3_TM_PATH)
221 $::env(TCL8.2_TM_PATH) $::env(TCL8_2_TM_PATH)
222 $::env(TCL8.1_TM_PATH) $::env(TCL8_1_TM_PATH)
223 $::env(TCL8.0_TM_PATH) $::env(TCL8_0_TM_PATH)
224
226 package(n), Tcl Improvement Proposal #189 “Tcl Modules” (online at
227 http://tip.tcl.tk/189.html), Tcl Improvement Proposal #190 “Implementa‐
228 tion Choices for Tcl Modules” (online at http://tip.tcl.tk/190.html)
229
231 modules, package
232
233
234
235Tcl 8.5 tm(n)