How to obtain the name of a procedure from within it.
| Tcl |
1 2 3 4 5 | proc myProc {args} {
set procName [lindex [info level 0] 0]
puts "You called \"$procName\""
puts "The full call was \"[info level 0]\""
}
|
Discussion
[info level n] returns information about a specific level in the callstack of procedures, namely the name of the command in that level and its arguments. This information is in a list and the name of the command is the first element. Level 0 refers to the currently executing procedure.


Comments
Some fixes. The proc as shown would constantly return "myProc", as it is asked for its name. Also, "puts" should be left to the caller. I propose to rewrite it as
A practical application is in an overloaded widget procedure.
Is this more useful? Would the following name return proc be of more use?
Sign in to comment