17

How to know what is the name of the proc in which I am. I mean I need this:

proc nameOfTheProc {} {

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

so I want to obtain "nameOfTheProc" but not hard-code. So that when someone will change the proc name it will still work properly.

Narek
  • 38,779
  • 79
  • 233
  • 389

3 Answers3

17

You can use the info level command for your issue:

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]'"
}

With the inner info level you will get the level of the procedure call depth you are currently in. The outer one will return the name of the procedure itself.

bmk
  • 13,849
  • 5
  • 37
  • 46
6

The correct idiomatic way to achieve what's implied in your question is to use return -code error $message like this:

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

This way your procedure will behave exactly in a way stock Tcl commands do when they're not satisfied with what they've been called with: it would cause an error at the call site.

kostix
  • 51,517
  • 14
  • 93
  • 176
  • This does not tell me what proc was called, does it? – Narek Apr 05 '12 at 08:30
  • 1
    @Narek, you will see this from the stack trace which will include both the error message and the name of the procedure returned the error. If you will catch that error (i.e. not allow the runtime to terminate the program and dump the stack trace) you'll be able to inspect the situation using tools described in the [`return`](http://www.tcl.tk/man/tcl8.5/TclCmd/return.htm) manual (`errorInfo` etc). – kostix Apr 05 '12 at 11:22
  • 1
    @Narek, basically it's advised to study Tcl code written by professionals (with modules from [tcllib](http://tcllib.sf.net) being good examples). You would see that they make heavy use of that `return -code error` idiom, because calling `error` (or using other means for error reporting) is for signalizing "external" runtime errors such as inability to acquire a resource or complete a requested operation etc, while `return -code error` is for reporting "static", semantic errors such as calling a command in a wrong way. – kostix Apr 05 '12 at 11:30
5

If your running Tcl 8.5 or later the info frame command will return a dict rather than a list. So modify the code as follows:

proc nameOfTheProc {} {
   puts "This is [dict get [info frame [info frame]] proc]"
}
Jackson
  • 5,627
  • 2
  • 29
  • 48
  • That's not fully true. `info level` will still return a list (at least in version 8.5). But `info frame` returns a dict. – bmk Apr 04 '12 at 14:46
  • @bmk Your quite right - I've fixed the answer so it's info frame in the text. – Jackson Apr 04 '12 at 14:51
  • Info level is about argument lists, info frame is about general frame descriptors. The two complement each other. – Donal Fellows Apr 04 '12 at 16:49