Debian Codegraph @

doc/library.n

raw
1 '\"
2 '\" Copyright (c) 1991-1993 The Regents of the University of California.
3 '\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
4 '\"
5 '\" See the file "license.terms" for information on usage and redistribution
6 '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7 '\"
8 .TH library n "8.0" Tcl "Tcl Built-In Commands"
9 .so man.macros
10 .BS
11 .SH NAME
12 auto_execok, auto_import, auto_load, auto_mkindex, auto_qualify, auto_reset, parray, tcl_findLibrary, tcl_endOfWord, tcl_nonwordchars, tcl_startOfNextWord, tcl_startOfPreviousWord, tcl_wordBreakAfter, tcl_wordBreakBefore, tcl_wordchars \- standard library of Tcl procedures
13 .SH SYNOPSIS
14 .nf
15 \fBauto_execok \fIcmd\fR
16 \fBauto_import \fIpattern\fR
17 \fBauto_load \fIcmd\fR
18 \fBauto_mkindex \fIdir pattern pattern ...\fR
19 \fBauto_qualify \fIcommand namespace\fR
20 \fBauto_reset\fR
21 \fBtcl_findLibrary \fIbasename version patch initScript enVarName varName\fR
22 \fBparray \fIarrayName\fR ?\fIpattern\fR?
23 \fBtcl_endOfWord \fIstr start\fR
24 \fBtcl_startOfNextWord \fIstr start\fR
25 \fBtcl_startOfPreviousWord \fIstr start\fR
26 \fBtcl_wordBreakAfter \fIstr start\fR
27 \fBtcl_wordBreakBefore \fIstr start\fR
28 .BE
29 .SH INTRODUCTION
30 .PP
31 Tcl includes a library of Tcl procedures for commonly-needed functions.
32 The procedures defined in the Tcl library are generic ones suitable
33 for use by many different applications.
34 The location of the Tcl library is returned by the \fBinfo library\fR
35 command.
36 In addition to the Tcl library, each application will normally have
37 its own library of support procedures as well; the location of this
38 library is normally given by the value of the \fB$\fIapp\fB_library\fR
39 global variable, where \fIapp\fR is the name of the application.
40 For example, the location of the Tk library is kept in the variable
41 \fBtk_library\fR.
42 .PP
43 To access the procedures in the Tcl library, an application should
44 source the file \fBinit.tcl\fR in the library, for example with
45 the Tcl command
46 .PP
47 .CS
48 \fBsource [file join [info library] init.tcl]\fR
49 .CE
50 .PP
51 If the library procedure \fBTcl_Init\fR is invoked from an application's
52 \fBTcl_AppInit\fR procedure, this happens automatically.
53 The code in \fBinit.tcl\fR will define the \fBunknown\fR procedure
54 and arrange for the other procedures to be loaded on-demand using
55 the auto-load mechanism defined below.
56 .SH "COMMAND PROCEDURES"
57 .PP
58 The following procedures are provided in the Tcl library:
59 .\" COMMAND: auto_execok
60 .TP
61 \fBauto_execok \fIcmd\fR
62 .
63 Determines whether there is an executable file or shell builtin
64 by the name \fIcmd\fR. If so, it returns a list of arguments to be
65 passed to \fBexec\fR to execute the executable file or shell builtin
66 named by \fIcmd\fR. If not, it returns an empty string. This command
67 examines the directories in the current search path (given by the PATH
68 environment variable) in its search for an executable file named
69 \fIcmd\fR. On Windows platforms, the search is expanded with the same
70 directories and file extensions as used by \fBexec\fR. \fBAuto_execok\fR
71 remembers information about previous searches in an array named
72 \fBauto_execs\fR; this avoids the path search in future calls for the
73 same \fIcmd\fR. The command \fBauto_reset\fR may be used to force
74 \fBauto_execok\fR to forget its cached information.
75 .RS
76 .PP
77 For example, to run the \fIumask\fR shell builtin on Linux, you would do:
78 .PP
79 .CS
80 exec {*}[\fBauto_execok\fR umask]
81 .CE
82 .PP
83 To run the \fIDIR\fR shell builtin on Windows, you would do:
84 .PP
85 .CS
86 exec {*}[\fBauto_execok\fR dir]
87 .CE
88 .PP
89 To discover if there is a \fIfrobnicate\fR binary on the user's PATH,
90 you would do:
91 .PP
92 .CS
93 set mayFrob [expr {[llength [\fBauto_execok\fR frobnicate]] > 0}]
94 .CE
95 .RE
96 .\" COMMAND: auto_import
97 .TP
98 \fBauto_import \fIpattern\fR
99 .
100 \fBAuto_import\fR is invoked during \fBnamespace import\fR to see if
101 the imported commands specified by \fIpattern\fR reside in an
102 autoloaded library. If so, the commands are loaded so that they will
103 be available to the interpreter for creating the import links. If the
104 commands do not reside in an autoloaded library, \fBauto_import\fR
105 does nothing. The pattern matching is performed according to the
106 matching rules of \fBnamespace import\fR.
107 .RS
108 .PP
109 It is not normally necessary to call this command directly.
110 .RE
111 .\" COMMAND: auto_load
112 .TP
113 \fBauto_load \fIcmd\fR
114 .
115 This command attempts to load the definition for a Tcl command named
116 \fIcmd\fR. To do this, it searches an \fIauto-load path\fR, which is
117 a list of one or more directories. The auto-load path is given by the
118 global variable \fBauto_path\fR if it exists. If there is no
119 \fBauto_path\fR variable, then the \fBTCLLIBPATH\fR environment variable is
120 used, if it exists. Otherwise the auto-load path consists of just the
121 Tcl library directory. Within each directory in the auto-load path
122 there must be a file \fBtclIndex\fR that describes one or more
123 commands defined in that directory and a script to evaluate to load
124 each of the commands. The \fBtclIndex\fR file should be generated
125 with the \fBauto_mkindex\fR command. If \fIcmd\fR is found in an
126 index file, then the appropriate script is evaluated to create the
127 command. The \fBauto_load\fR command returns 1 if \fIcmd\fR was
128 successfully created. The command returns 0 if there was no index
129 entry for \fIcmd\fR or if the script did not actually define \fIcmd\fR
130 (e.g. because index information is out of date). If an error occurs
131 while processing the script, then that error is returned.
132 \fBAuto_load\fR only reads the index information once and saves it in
133 the array \fBauto_index\fR; future calls to \fBauto_load\fR check for
134 \fIcmd\fR in the array rather than re-reading the index files. The
135 cached index information may be deleted with the command
136 \fBauto_reset\fR. This will force the next \fBauto_load\fR command to
137 reload the index database from disk.
138 .RS
139 .PP
140 It is not normally necessary to call this command directly; the
141 default \fBunknown\fR handler will do so.
142 .RE
143 .\" COMMAND: auto_mkindex
144 .TP
145 \fBauto_mkindex \fIdir pattern pattern ...\fR
146 .
147 Generates an index suitable for use by \fBauto_load\fR. The command
148 searches \fIdir\fR for all files whose names match any of the
149 \fIpattern\fR arguments (matching is done with the \fBglob\fR
150 command), generates an index of all the Tcl command procedures defined
151 in all the matching files, and stores the index information in a file
152 named \fBtclIndex\fR in \fIdir\fR. If no pattern is given a pattern of
153 \fB*.tcl\fR will be assumed. For example, the command
154 .RS
155 .PP
156 .CS
157 \fBauto_mkindex foo *.tcl\fR
158 .CE
159 .PP
160 will read all the \fB.tcl\fR files in subdirectory \fBfoo\fR and
161 generate a new index file \fBfoo/tclIndex\fR.
162 .PP
163 \fBAuto_mkindex\fR parses the Tcl scripts by sourcing them into a
164 child interpreter and monitoring the proc and namespace commands that
165 are executed. Extensions can use the (undocumented)
166 auto_mkindex_parser package to register other commands that can
167 contribute to the auto_load index. You will have to read through
168 auto.tcl to see how this works.
169 .PP
170 \fBAuto_mkindex_old\fR
171 (which has the same syntax as \fBauto_mkindex\fR)
172 parses the Tcl scripts in a relatively
173 unsophisticated way: if any line contains the word
174 .QW \fBproc\fR
175 as its first characters then it is assumed to be a procedure
176 definition and the next word of the line is taken as the
177 procedure's name.
178 Procedure definitions that do not appear in this way (e.g.\ they
179 have spaces before the \fBproc\fR) will not be indexed. If your
180 script contains
181 .QW dangerous
182 code, such as global initialization
183 code or procedure names with special characters like \fB$\fR,
184 \fB*\fR, \fB[\fR or \fB]\fR, you are safer using \fBauto_mkindex_old\fR.
185 .RE
186 .\" COMMAND: auto_reset
187 .TP
188 \fBauto_reset\fR
189 .
190 Destroys all the information cached by \fBauto_execok\fR and
191 \fBauto_load\fR. This information will be re-read from disk the next
192 time it is needed. \fBAuto_reset\fR also deletes any procedures
193 listed in the auto-load index, so that fresh copies of them will be
194 loaded the next time that they are used.
195 .\" COMMAND: auto_qualify
196 .TP
197 \fBauto_qualify \fIcommand namespace\fR
198 .
199 Computes a list of fully qualified names for \fIcommand\fR. This list
200 mirrors the path a standard Tcl interpreter follows for command
201 lookups: first it looks for the command in the current namespace, and
202 then in the global namespace. Accordingly, if \fIcommand\fR is
203 relative and \fInamespace\fR is not \fB::\fR, the list returned has
204 two elements: \fIcommand\fR scoped by \fInamespace\fR, as if it were
205 a command in the \fInamespace\fR namespace; and \fIcommand\fR as if it
206 were a command in the global namespace. Otherwise, if either
207 \fIcommand\fR is absolute (it begins with \fB::\fR), or
208 \fInamespace\fR is \fB::\fR, the list contains only \fIcommand\fR as
209 if it were a command in the global namespace.
210 .RS
211 .PP
212 \fBAuto_qualify\fR is used by the auto-loading facilities in Tcl, both
213 for producing auto-loading indexes such as \fIpkgIndex.tcl\fR, and for
214 performing the actual auto-loading of functions at runtime.
215 .RE
216 .\" COMMAND: auto_findLibrary
217 .TP
218 \fBtcl_findLibrary \fIbasename version patch initScript enVarName varName\fR
219 .
220 This is a standard search procedure for use by extensions during
221 their initialization. They call this procedure to look for their
222 script library in several standard directories.
223 The last component of the name of the library directory is
224 normally \fIbasenameversion\fR
225 (e.g., tk8.0), but it might be
226 .QW library
227 when in the build hierarchies.
228 The \fIpatch\fR argument is not used.
229 The \fIinitScript\fR file will be sourced into the interpreter
230 once it is found. The directory in which this file is found is
231 stored into the global variable \fIvarName\fR.
232 If this variable is already defined (e.g., by C code during
233 application initialization) then no searching is done.
234 Otherwise the search looks in these directories:
235 the directory named by the environment variable \fIenVarName\fR;
236 relative to the Tcl library directory;
237 relative to the executable file in the standard installation
238 bin or bin/\fIarch\fR directory;
239 relative to the executable file in the current build tree;
240 relative to the executable file in a parallel build tree.
241 .\" COMMAND: parray
242 .TP
243 \fBparray \fIarrayName\fR ?\fIpattern\fR?
244 .
245 Prints on standard output the names and values of all the elements in the
246 array \fIarrayName\fR, or just the names that match \fIpattern\fR (using the
247 matching rules of \fBstring match\fR) and their values if \fIpattern\fR is
248 given.
249 \fIArrayName\fR must be an array accessible to the caller of \fBparray\fR.
250 It may be either local or global.
251 .RS
252 .PP
253 For example, to print the contents of the \fBtcl_platform\fR array, do:
254 .PP
255 .CS
256 \fBparray\fR tcl_platform
257 .CE
258 .RE
259 .SS "WORD BOUNDARY HELPERS"
260 .PP
261 These procedures are mainly used internally by Tk.
262 .\" COMMAND: tcl_endOfWord
263 .TP
264 \fBtcl_endOfWord \fIstr start\fR
265 .
266 Returns the index of the first end-of-word location that occurs after
267 a starting index \fIstart\fR in the string \fIstr\fR. An end-of-word
268 location is defined to be the first non-word character following the
269 first word character after the starting point. Returns -1 if there
270 are no more end-of-word locations after the starting point. See the
271 description of \fBtcl_wordchars\fR and \fBtcl_nonwordchars\fR below
272 for more details on how Tcl determines which characters are word
273 characters.
274 .\" COMMAND: tcl_startOfNextWord
275 .TP
276 \fBtcl_startOfNextWord \fIstr start\fR
277 .
278 Returns the index of the first start-of-word location that occurs
279 after a starting index \fIstart\fR in the string \fIstr\fR. A
280 start-of-word location is defined to be the first word character
281 following a non-word character. Returns \-1 if there are no more
282 start-of-word locations after the starting point.
283 .RS
284 .PP
285 For example, to print the indices of the starts of each word in a
286 string according to platform rules:
287 .PP
288 .CS
289 set theString "The quick brown fox"
290 for {set idx 0} {$idx >= 0} {
291 set idx [\fBtcl_startOfNextWord\fR $theString $idx]} {
292 puts "Word start index: $idx"
293 }
294 .CE
295 .RE
296 .\" COMMAND: tcl_startOfPreviousWord
297 .TP
298 \fBtcl_startOfPreviousWord \fIstr start\fR
299 .
300 Returns the index of the first start-of-word location that occurs
301 before a starting index \fIstart\fR in the string \fIstr\fR. Returns
302 \-1 if there are no more start-of-word locations before the starting
303 point.
304 .\" COMMAND: tcl_wordBreakAfter
305 .TP
306 \fBtcl_wordBreakAfter \fIstr start\fR
307 .
308 Returns the index of the first word boundary after the starting index
309 \fIstart\fR in the string \fIstr\fR. Returns \-1 if there are no more
310 boundaries after the starting point in the given string. The index
311 returned refers to the second character of the pair that comprises a
312 boundary.
313 .\" COMMAND: tcl_wordBreakBefore
314 .TP
315 \fBtcl_wordBreakBefore \fIstr start\fR
316 .
317 Returns the index of the first word boundary before the starting index
318 \fIstart\fR in the string \fIstr\fR. Returns \-1 if there are no more
319 boundaries before the starting point in the given string. The index
320 returned refers to the second character of the pair that comprises a
321 boundary.
322 .SH "VARIABLES"
323 .PP
324 The following global variables are defined or used by the procedures in
325 the Tcl library. They fall into two broad classes, handling unknown
326 commands and packages, and determining what are words.
327 .SS "AUTOLOADING AND PACKAGE MANAGEMENT VARIABLES"
328 .\" VARIABLE: auto_execs
329 .TP
330 \fBauto_execs\fR
331 .
332 Used by \fBauto_execok\fR to record information about whether
333 particular commands exist as executable files.
334 .RS
335 .PP
336 Not normally usefully accessed directly by user code.
337 .RE
338 .\" VARIABLE: auto_index
339 .TP
340 \fBauto_index\fR
341 .
342 Used by \fBauto_load\fR to save the index information read from
343 disk.
344 .RS
345 .PP
346 Not normally usefully accessed directly by user code.
347 .RE
348 .\" VARIABLE: auto_noexec
349 .TP
350 \fBauto_noexec\fR
351 .
352 If set to any value, then \fBunknown\fR will not attempt to auto-exec
353 any commands.
354 .\" VARIABLE: auto_noload
355 .TP
356 \fBauto_noload\fR
357 .
358 If set to any value, then \fBunknown\fR will not attempt to auto-load
359 any commands.
360 .\" VARIABLE: auto_path
361 .TP
362 \fBauto_path\fR
363 .
364 If set, then it must contain a valid Tcl list giving directories to
365 search during auto-load operations (including for package index
366 files when using the default \fBpackage unknown\fR handler).
367 This variable is initialized during startup to contain, in order:
368 the directories listed in the \fBTCLLIBPATH\fR environment variable,
369 the directory named by the \fBtcl_library\fR global variable,
370 the parent directory of \fBtcl_library\fR,
371 the directories listed in the \fBtcl_pkgPath\fR variable.
372 Additional locations to look for files and package indices should
373 normally be added to this variable using \fBlappend\fR.
374 .RS
375 .PP
376 For example, to add the \fIlib\fR directory next to the running
377 script, you would do:
378 .PP
379 .CS
380 lappend \fBauto_path\fR [file dirname [info script]]/lib
381 .CE
382 .PP
383 Note that if the script uses \fBcd\fR, it is advisable to ensure that
384 entries on the \fBauto_path\fR are \fBfile normalize\fRd.
385 .RE
386 .\" VARIABLE: env(TCL_LIBRARY)
387 .TP
388 \fBenv(TCL_LIBRARY)\fR
389 .
390 If set, then it specifies the location of the directory containing
391 library scripts (the value of this variable will be
392 assigned to the \fBtcl_library\fR variable and therefore returned by
393 the command \fBinfo library\fR). If this variable is not set then
394 a default value is used.
395 .RS
396 .PP
397 Use of this environment variable is not recommended outside of testing.
398 Tcl installations should already know where to find their own script
399 files, as the value is baked in during the build or installation.
400 .RE
401 .\" VARIABLE: env(TCLLIBPATH)
402 .TP
403 \fBenv(TCLLIBPATH)\fR
404 .
405 If set, then it must contain a valid Tcl list giving directories to
406 search during auto-load operations. Directories must be specified in
407 Tcl format, using
408 .QW /
409 as the path separator, regardless of platform.
410 This variable is only used when initializing the \fBauto_path\fR variable.
411 .RS
412 .PP
413 A key consequence of this variable is that it gives a way to let the user
414 of a script specify the list of places where that script may use
415 \fBpackage require\fR to read packages from. It is not normally usefully
416 settable within a Tcl script itself \fIexcept\fR to influence where other
417 interpreters load from (whether made with \fBinterp create\fR or launched
418 as their own threads or subprocesses).
419 .RE
420 .SS "WORD BOUNDARY DETERMINATION VARIABLES"
421 These variables are only used in the \fBtcl_endOfWord\fR,
422 \fBtcl_startOfNextWord\fR, \fBtcl_startOfPreviousWord\fR,
423 \fBtcl_wordBreakAfter\fR, and \fBtcl_wordBreakBefore\fR commands.
424 .\" VARIABLE: tcl_nonwordchars
425 .TP
426 \fBtcl_nonwordchars\fR
427 .
428 This variable contains a regular expression that is used by routines
429 like \fBtcl_endOfWord\fR to identify whether a character is part of a
430 word or not. If the pattern matches a character, the character is
431 considered to be a non-word character. On Windows platforms, spaces,
432 tabs, and newlines are considered non-word characters. Under Unix,
433 everything but numbers, letters and underscores are considered
434 non-word characters.
435 .\" VARIABLE: tcl_wordchars
436 .TP
437 \fBtcl_wordchars\fR
438 .
439 This variable contains a regular expression that is used by routines
440 like \fBtcl_endOfWord\fR to identify whether a character is part of a
441 word or not. If the pattern matches a character, the character is
442 considered to be a word character. On Windows platforms, words are
443 comprised of any character that is not a space, tab, or newline. Under
444 Unix, words are comprised of numbers, letters or underscores.
445 .SH "SEE ALSO"
446 env(n), info(n), re_syntax(n)
447 .SH KEYWORDS
448 auto-exec, auto-load, library, unknown, word, whitespace
449 '\"Local Variables:
450 '\"mode: nroff
451 '\"End: