如何知道当前进程的名称?我的意思是我需要这个:
proc nameOfTheProc {} {
#a lot of code here
puts "ERROR: You are using 'nameOfTheProc' proc wrongly"
}
我想获取“nameOfTheProc”,但不要硬编码。这样当有人更改存储过程名称时,它仍将正常工作。
您可以使用 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
,你可以获得当前所在过程调用深度的级别。而外部则会返回所在过程本身的名称。
[info level [info level]]
可以写作[info level 0]
... - Donal Fellows实现您问题中所暗示的正确惯用方法是使用return -code error $message
,如下所示:
proc nameOfTheProc {} {
#a lot of code here
return -code error "Wrong sequence of blorbs passed"
}
return
手册中描述的工具来检查情况(errorInfo
等)。 - kostixreturn -code error
这种惯用语法,因为调用error
(或使用其他方式进行错误报告)是为了信号化“外部”运行时错误,例如无法获取资源或完成请求的操作等,而return -code error
则用于报告“静态”的语义错误,例如以错误的方式调用命令。 - kostixinfo frame
命令将返回一个字典而不是列表。因此,请按以下方式修改代码:proc nameOfTheProc {} {
puts "This is [dict get [info frame [info frame]] proc]"
}
info level
仍然会返回一个列表(至少在8.5版本中)。但是 info frame
返回一个字典。 - bmk