15

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

proc nameOfTheProc {} {

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

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

4

3 回答 3

14

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

proc nameOfTheProc {} {

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

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

于 2012-04-04T14:20:47.980 回答
6

实现问题所暗示的正确惯用方法是这样使用return -code error $message

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

这样,您的过程将完全按照普通 Tcl 命令在对它们被调用的内容不满意时的行为方式进行:这将导致调用站点出现错误。

于 2012-04-04T15:30:03.617 回答
5

如果您正在运行的 Tcl 8.5 或更高版本,该info frame命令将返回一个字典而不是一个列表。所以修改代码如下:

proc nameOfTheProc {} {
   puts "This is [dict get [info frame [info frame]] proc]"
}
于 2012-04-04T14:33:16.010 回答