TCL获取我所在的进程名称

TCL获取我所在的进程名称,tcl,proc,Tcl,Proc,如何知道我所在的进程的名称。我的意思是我需要这个: proc nameOfTheProc {} { #a lot of code here puts "ERROR: You are using 'nameOfTheProc' proc wrongly" } 所以我想获得“程序名”,但不是硬代码。因此,当有人更改进程名称时,它仍能正常工作 您可以使用info-level命令来处理您的问题: proc nameOfTheProc {} { #a lot of code

如何知道我所在的进程的名称。我的意思是我需要这个:

proc nameOfTheProc {} {

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

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

您可以使用
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]'"
}

使用内部
信息级别
可以获得当前所处的过程调用深度级别。外部命令将返回过程本身的名称。

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

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

正确的惯用方法是使用
return-code error$message
如下:

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

这样,您的过程将完全按照股票Tcl命令在对调用内容不满意时的方式运行:这将导致调用站点出错。

[info-level[info-level]]
可以写成
[info-level 0]
…这不是完全正确的<代码>信息级别仍将返回列表(至少在8.5版中)。但是
info frame
返回一个dict.@bmk你说得对-我已经修正了答案,所以它是文本中的info frame。info level是关于参数列表的,info frame是关于一般框架描述符的。这两个问题相辅相成。+1个好问题,它给出了许多有趣的答案。这并没有告诉我调用了什么proc,是吗?@Narek,您将从堆栈跟踪中看到这一点,其中包括错误消息和返回错误的过程的名称。如果您将捕获该错误(即不允许运行时终止程序并转储堆栈跟踪),您将能够使用手册中描述的工具(
errorInfo
etc)检查情况。@Narek,基本上建议您学习专业人员编写的Tcl代码(模块是很好的示例)。您会看到,他们大量使用了
返回-代码错误
的习惯用法,因为调用
错误
(或使用其他错误报告方法)是为了表示“外部”运行时错误,例如无法获取资源或完成请求的操作等,而
返回-代码错误
是为了报告“静态”,语义错误,例如以错误的方式调用命令。