Home › Tcl Control Flow Commands tcllib
control
Procedures for control flow structures.
literal type exactly as shown
argument replace with your value
?optional? may be omitted
Synopsis#
package require Tcl 8.5 9
package require control ?0.1.4?
control::control command option ?arg arg ...?
control::assert expr ?arg arg ...?
control::do body ?option test?
control::no-op ?arg arg ...?
Description#
The control package provides a variety of commands that provide additional flow of control structures beyond the built-in ones provided by Tcl. These are commands that in many programming languages might be considered keywords, or a part of the language itself. In Tcl, control flow structures are just commands like everything else.
Commands#
control::controlcommand option ?arg arg ...?#-
The
controlcommand is used as a configuration command for customizing the other public commands of the control package. The command argument names the command to be customized. The set of valid option and subsequent arguments are determined by the command being customized, and are documented with the command. control::assertexpr ?arg arg ...?#-
When disabled, the
assertcommand behaves exactly like theno-opcommand.When enabled, the
assertcommand evaluates expr as an expression (in the same way thatexprevaluates its argument). If evaluation reveals that expr is not a valid boolean expression (according to [string is boolean -strict]), an error is raised. If expr evaluates to a true boolean value (as recognized byif), thenassertreturns an empty string. Otherwise, the remaining arguments toassertare used to construct a message string. If there are no arguments, the message string is "assertion failed: $expr". If there are arguments, they are joined byjointo form the message string. The message string is then appended as an argument to a callback command, and the completed callback command is evaluated in the global namespace.The
assertcommand can be customized by thecontrolcommand in two ways:[
control::control assert enabled?boolean?] queries or sets whethercontrol::assertis enabled. When called without a boolean argument, a boolean value is returned indicating whether thecontrol::assertcommand is enabled. When called with a valid boolean value as the boolean argument, thecontrol::assertcommand is enabled or disabled to match the argument, and an empty string is returned.[
control::control assert callback?command?] queries or sets the callback command that will be called by an enabledasserton assertion failure. When called without a command argument, the current callback command is returned. When called with a command argument, that argument becomes the new assertion failure callback command. Note that an assertion failure callback command is always defined, even whenassertis disabled. The default callback command is [return -code error].Note that
control::asserthas been written so that in combination with [namespace import], it is possible to use enabledassertcommands in some namespaces and disabledassertcommands in other namespaces at the same time. This capability is useful so that debugging efforts can be independently controlled module by module.% package require control % control::control assert enabled 1 % namespace eval one namespace import ::control::assert % control::control assert enabled 0 % namespace eval two namespace import ::control::assert % one::assert {1 == 0} assertion failed: 1 == 0 % two::assert {1 == 0} control::dobody ?option test?#-
The
docommand evaluates the script body repeatedly until the expression test becomes true or as long as (while) test is true, depending on the value of option beinguntilorwhile. If option and test are omitted the body is evaluated exactly once. After normal completion,doreturns an empty string. Exceptional return codes (break,continue,error, etc.) during the evaluation of body are handled in the same way thewhilecommand handles them, except as noted inLIMITATIONS, below. control::no-op?arg arg ...?#-
The
no-opcommand takes any number of arguments and does nothing. It returns an empty string.
Limitations#
Several of the commands provided by the control package accept arguments that are scripts to be evaluated. Due to fundamental limitations of Tcl's catch and return commands, it is not possible for these commands to properly evaluate the command [return -code $code] within one of those script arguments for any value of $code other than ok. In this way, the commands of the control package are limited as compared to Tcl's built-in control flow commands (such as if, while, etc.) and those control flow commands that can be provided by packages coded in C. An example of this difference:
% package require control
% proc a {} {while 1 {return -code error a}}
% proc b {} {control::do {return -code error b} while 1}
% catch a
1
% catch b
0
Bugs, ideas, feedback#
This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category control of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. Please also report any ideas for enhancements you may have for either package and/or documentation.
When proposing code changes, please provide unified diffs, i.e the output of diff -u.
Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar.
Category#
Programming tools