Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCL obtain the proc name in which I am

Tags:

proc

tcl

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.

like image 643
Narek Avatar asked Apr 04 '12 14:04

Narek


People also ask

What is PROC command in Tcl?

The proc command creates a new Tcl procedure named name, replacing any existing command or procedure there may have been by that name. Whenever the new command is invoked, the contents of body will be executed by the Tcl interpreter. Args specifies the formal arguments to the procedure.

What is a proc name?

If there was a chain of evaluations leading to the procedure then procname is the ''last name evaluated'' in the chain. If there is no name then the name unknown is used.

How do you call a procedure in Tcl?

To create a TCL procedure without any parameter you should use the proc keyword followed by the procedure name then the scope of your procedure. proc hello_world {} { // Use puts to print your output in the terminal. // If your procedure return data use return keyword. }

How do you call a proc inside a Tcl proc?

Tcl doesn't have nested procedures. You can call proc inside a procedure definition, but that's just creating a normal procedure (the namespace used for resolution of the name of the procedure to create will be the current namespace of the caller, as reported by namespace current ).


2 Answers

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.

like image 148
bmk Avatar answered Sep 21 '22 17:09

bmk


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.

like image 35
kostix Avatar answered Sep 22 '22 17:09

kostix