如何知道我所处的过程名称是什么。我的意思是我需要这个:TCL获取我所在的过程名称
proc nameOfTheProc {} {
#a lot of code here
puts "ERROR: You are using 'nameOfTheProc' proc wrongly"
}
所以我想获得“nameOfTheProc”,但不是硬编码。所以当有人改变proc的名字时,它仍然可以正常工作。
如何知道我所处的过程名称是什么。我的意思是我需要这个:TCL获取我所在的过程名称
proc nameOfTheProc {} {
#a lot of code here
puts "ERROR: You are using 'nameOfTheProc' proc wrongly"
}
所以我想获得“nameOfTheProc”,但不是硬编码。所以当有人改变proc的名字时,它仍然可以正常工作。
可以使用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
你会得到的程序调用深度的水平,你目前在外层一个将返回过程的名称。本身。
如果您运行的Tcl 8.5或更高版本的info frame
命令将返回一个字典而不是列表。因此,修改代码如下:
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命令会执行这些命令:它会在调用站点上导致错误。
+1个不错的问题,它产生了很多有趣的答案。 – 2012-04-04 16:40:16