1control(n) Tcl Control Flow Commands control(n)
2
3
4
5______________________________________________________________________________
6
8 control - Procedures for control flow structures.
9
11 package require Tcl 8.2
12
13 package require control ?0.1.3?
14
15 control::control command option ?arg arg ...?
16
17 control::assert expr ?arg arg ...?
18
19 control::do body ?option test?
20
21 control::no-op ?arg arg ...?
22
23_________________________________________________________________
24
26 The control package provides a variety of commands that provide addi‐
27 tional flow of control structures beyond the built-in ones provided by
28 Tcl. These are commands that in many programming languages might be
29 considered keywords, or a part of the language itself. In Tcl, control
30 flow structures are just commands like everything else.
31
33 control::control command option ?arg arg ...?
34 The control command is used as a configuration command for cus‐
35 tomizing the other public commands of the control package. The
36 command argument names the command to be customized. The set of
37 valid option and subsequent arguments are determined by the com‐
38 mand being customized, and are documented with the command.
39
40 control::assert expr ?arg arg ...?
41 When disabled, the assert command behaves exactly like the no-op
42 command.
43
44 When enabled, the assert command evaluates expr as an expression
45 (in the same way that expr evaluates its argument). If evalua‐
46 tion reveals that expr is not a valid boolean expression
47 (according to [string is boolean -strict]), an error is raised.
48 If expr evaluates to a true boolean value (as recognized by if),
49 then assert returns an empty string. Otherwise, the remaining
50 arguments to assert are used to construct a message string. If
51 there are no arguments, the message string is "assertion failed:
52 $expr". If there are arguments, they are joined by join to form
53 the message string. The message string is then appended as an
54 argument to a callback command, and the completed callback com‐
55 mand is evaluated in the global namespace.
56
57 The assert command can be customized by the control command in
58 two ways:
59
60 [control::control assert enabled ?boolean?] queries or sets
61 whether control::assert is enabled. When called without a bool‐
62 ean argument, a boolean value is returned indicating whether the
63 control::assert command is enabled. When called with a valid
64 boolean value as the boolean argument, the control::assert com‐
65 mand is enabled or disabled to match the argument, and an empty
66 string is returned.
67
68 [control::control assert callback ?command?] queries or sets
69 the callback command that will be called by an enabled assert on
70 assertion failure. When called without a command argument, the
71 current callback command is returned. When called with a com‐
72 mand argument, that argument becomes the new assertion failure
73 callback command. Note that an assertion failure callback com‐
74 mand is always defined, even when assert is disabled. The
75 default callback command is [return -code error].
76
77 Note that control::assert has been written so that in combina‐
78 tion with [namespace import], it is possible to use enabled
79 assert commands in some namespaces and disabled assert commands
80 in other namespaces at the same time. This capability is useful
81 so that debugging efforts can be independently controlled module
82 by module.
83
84
85 % package require control
86 % control::control assert enabled 1
87 % namespace eval one namespace import ::control::assert
88 % control::control assert enabled 0
89 % namespace eval two namespace import ::control::assert
90 % one::assert {1 == 0}
91 assertion failed: 1 == 0
92 % two::assert {1 == 0}
93
94
95 control::do body ?option test?
96 The do command evaluates the script body repeatedly until the
97 expression test becomes true or as long as (while) test is true,
98 depending on the value of option being until or while. If option
99 and test are omitted the body is evaluated exactly once. After
100 normal completion, do returns an empty string. Exceptional
101 return codes (break, continue, error, etc.) during the evalua‐
102 tion of body are handled in the same way the while command han‐
103 dles them, except as noted in LIMITATIONS, below.
104
105 control::no-op ?arg arg ...?
106 The no-op command takes any number of arguments and does noth‐
107 ing. It returns an empty string.
108
110 Several of the commands provided by the control package accept argu‐
111 ments that are scripts to be evaluated. Due to fundamental limitations
112 of Tcl's catch and return commands, it is not possible for these com‐
113 mands to properly evaluate the command [return -code $code] within one
114 of those script arguments for any value of $code other than ok. In
115 this way, the commands of the control package are limited as compared
116 to Tcl's built-in control flow commands (such as if, while, etc.) and
117 those control flow commands that can be provided by packages coded in
118 C. An example of this difference:
119
120 % package require control
121 % proc a {} {while 1 {return -code error a}}
122 % proc b {} {control::do {return -code error b} while 1}
123 % catch a
124 1
125 % catch b
126 0
127
128
130 break, continue, expr, if, join, namespace, return, string, while
131
133 assert, control, do, flow, no-op, structure
134
135
136
137control 0.1.3 control(n)