Posted to tcl by kostix at Sat Jun 07 13:51:59 GMT 2008view raw

  1. # Returns a fully-qualified name of the command that has invoked
  2. # the caller of this procedure.
  3. # To put is simple: if ::one::bar has invoked ::two::foo, the
  4. # ::two::foo proc can use [caller] to know that its caller
  5. # is ::one::bar
  6. # If the caller of this proc has no caller (i.e. it was called
  7. # on level 0), this proc returns empty string.
  8. # You can specify 2, 3, etc as the argument to get info about
  9. # the caller of the caller and so on (think of [uplevel]).
  10.  
  11. proc caller {{level 1}} {
  12. incr level
  13. if {[catch {info level -$level} prc]} {
  14. return ""
  15. } else {
  16. return [namespace which -command [lindex $prc 0]]
  17. }
  18. }
  19.