TCL获取我所在的proc名称

时间:2012-04-04 14:10:37

标签: tcl proc

如何知道我所在的proc的名称是什么。我的意思是我需要这个:

proc nameOfTheProc {} {

    #a lot of code here
    puts "ERROR: You are using 'nameOfTheProc' proc wrongly"
}

所以我想获得“nameOfTheProc”但不是硬代码。因此,当有人更改proc名称时,它仍然可以正常工作。

3 个答案:

答案 0 :(得分:11)

您可以使用info level命令解决您的问题:

proc nameOfTheProc {} {

    #a lot of code here
    puts "ERROR: You are using '[lindex [info level [info level]] 0]' proc wrongly"
    puts "INFO:  You specified the arguments: '[lrange [info level [info level]] 1 end]'"
}

使用内部info level,您将获得当前所在的过程调用深度级别。外部将返回过程本身的名称。

答案 1 :(得分:5)

如果运行Tcl 8.5或更高版本info frame命令将返回dict而不是列表。所以修改代码如下:

proc nameOfTheProc {} {
   puts "This is [dict get [info frame [info frame]] proc]"
}

答案 2 :(得分:5)

在您的问题中实现暗示的正确惯用方法是使用return -code error $message,如下所示:

proc nameOfTheProc {} {
    #a lot of code here
    return -code error "Wrong sequence of blorbs passed"
}

通过这种方式,您的过程将完全按照Tcl命令执行的方式执行,因为它们对所调用的内容不满意:它会导致呼叫站点出错。

相关问题