Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

TCL use elseif on new line

Writer Matthew Harrington

I like to structure if {} {} elseif {} {} in multiple lines when the statement block is rather short like below.

if {cond1} {do1}
elseif {cond2} {do2}
elseif {cond3} {do3}

But TCL doesn't let me do it. Gives : invalid command name "elseif"

It works when I open the braces around the 'do' statements into multiple line but that looks so ugly.

if {cond1} {do1
} elseif {cond2} {do2
} elseif {cond3} {do3}

What's the fundamental issue in TCL preventing it from recognizing an elseif on the next line after the if ?

Thanks, Gert

1 Answer

A line break terminates the current command. To have a command continue on the next line, the newline character must be escaped or quoted.

If the newline is directly preceded by a backslash, the backslash, the newline and all tabs and spaces following in sequence will be replaced by a single space character.

if {cond1} {do1} \
elseif {cond2} {do2} \
elseif {cond3} {do3} \
else {do4}

If the newline is inside braces, it has no syntactic function. It is just another character in the string enclosed by braces and passed to the command. This is useful when you need to pass scripts consisting of several commands to e.g. an if command: the script will be re-interpreted within the command, and those newlines will resume their function there.

if {cond1} {do1
} elseif {cond2} {do2
} elseif {cond3} {do3
} else {do4}

Typical Tcl style is to write commands with script arguments like this:

if {cond1} { do1
} elseif {cond2} { do2
} elseif {cond3} { do3
} else { do4
}

This visual style isn't to everyone's taste, but one can get used to it.

Documentation: Tcl

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.