如何在tcl中实现goto [英] how to implement goto in tcl

查看:35
本文介绍了如何在tcl中实现goto的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在 tcl 中实现 GOTO.我正在编写一个测试用例,其中我说了 5 个步骤.如果我的第 1 步失败了,我不想再继续下去,我想跳过现有的东西并转到一个通用的清理部分.

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.

如果 tcl 中有任何 GOTO 命令,请帮助我.

Please help me with if there are any GOTO commands in tcl.

谢谢,拉米亚.

推荐答案

Tcl 中没有没有 goto,并且由于相对技术原因,不可能实现.

There is no goto in Tcl, and for relatively technical reasons it's impossible to implement one.

但是您可以通过其他方式做您想做的事情.由于您正在处理测试用例,我希望您使用 tcltest 包来完成这项工作.有了它,你可以很容易地指定清理代码:

But you can do what you're after in other ways. Since you're dealing with test cases, I hope you're using the tcltest package for the job. With that, you can specify cleanup code quite easily:

tcltest::test test-1.1 "verify that the foo works" -setup {
    allocate some resources
} -body {
    whatever to do the test...
    return [our results]
} -cleanup {
    drop those resources
    make sure that we are nice and clean
} -result "the expected test result"

只需执行return,您就可以轻松跳出测试主体;tcltest::test 命令将检测它并将其视为结果.不过,通常最好让每个测试独立于其他测试:这样可以更容易地在测试失败时追踪出了什么问题.

You can skip out of the body of a test easily by just doing a return; the tcltest::test command 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.

如果您不使用 tcltest,您仍然最好重构为可以使用 return 提前跳过的内容.您可以将其与 try...finally...(在 Tcl 8.6 中原生,或与 此代码在 Tcler 的 Wiki 上)使事情变得简单:

If you're not using tcltest, you are still best refactoring into something where you can use return to skip out early. You can combine that with try…finally… (either natively in Tcl 8.6, or with this code on the Tcler's Wiki) to make things easy:

proc doThings {} {
    try {
        # do thing-1
        if {$no_more} return
        # do thing-2
        if {$no_more} return
        # do thing-3
        if {$no_more} return
        # do thing-4
        if {$no_more} return
        # do thing-5
    } finally {
        # do cleanup
    }
}

这篇关于如何在tcl中实现goto的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆