I wanted to know how to implement GOTO in tcl.
I am writing a test case where I have say 5 steps.
If my step 1 fails I don’t want to proceed further and I want to skip the existing things and goto a common clean up section.
Please help me with if there are any GOTO commands in tcl.
Thanks,
Ramya.
There is no
gotoin Tcl, and for relatively technical reasons it’s impossible to implement one.But you can do what you’re after in other ways. Since you’re dealing with test cases, I hope you’re using the
tcltestpackage for the job. With that, you can specify cleanup code quite easily:You can skip out of the body of a test easily by just doing a
return; thetcltest::testcommand will detect it and treat that as the result. It is usually better to try to keep each test independent of the others though: that makes it much easier to track down what’s going wrong when a test fails.If you’re not using
tcltest, you are still best refactoring into something where you can usereturnto skip out early. You can combine that withtry…finally…(either natively in Tcl 8.6, or with this code on the Tcler’s Wiki) to make things easy: