Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

getProcName

28 views
Skip to first unread message

Cecil Westerhof

unread,
Dec 16, 2017, 2:28:07 PM12/16/17
to
I am writing some functions I think could be useful. One is getting
the name of the current proc:
proc getProcName {} {
set currentProc [info level 0]
set callingProc [info level 1]
if {${currentProc} eq ${callingProc}} {
error [format "ERROR: %s not called from a proc" ${currentProc}]
}
return ${callingProc}
}

Should [info level 1] not give an error when it is not called from a
proc?
When entering in the shell:
set currentProc [info level 0]

I get:
bad level "0"
while evaluating {set currentProc [info level 0]}

So I would expect the same for level 1 when called from the shell and
not from a proc.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Luis Alejandro Muzzachiodi

unread,
Dec 16, 2017, 7:36:52 PM12/16/17
to

Cecil Westerhof

unread,
Dec 16, 2017, 9:28:06 PM12/16/17
to
In a way that is what I do. Only instead of putting the code in every
function I wrote a function to get it. And because
info level 0

does not work when you are not in proc. I would expect:
info level 1

not to work when the proc is not called from a proc.

Andreas Leitgeb

unread,
Dec 17, 2017, 12:14:57 PM12/17/17
to
Cecil Westerhof <Ce...@decebal.nl> wrote:
> In a way that is what I do. Only instead of putting the code in every
> function I wrote a function to get it. And because
> info level 0
> does not work when you are not in proc. I would expect:
> info level 1
> not to work when the proc is not called from a proc.

I suggest re-reading the doc for info level:

" info level ?number?
" If number is not specified, this command returns a number
" giving the stack level of the invoking procedure, or 0 if the
" command is invoked at top-level. If number is specified, then
" the result is a list consisting of the name and arguments for
" the procedure call at level number on the stack.

pay extra attention here:

" If number is
" positive then it selects a particular stack level (1 refers
" to the top-most active procedure, 2 to the procedure
" it called, and so on);

And that's what you probably need instead: (0 or negative levels)

" otherwise it gives a level relative
" to the current level (0 refers to the current procedure,
" -1 to its caller, and so on).

You probably want to query info level -1 from your proc.

" See the uplevel command for
" more information on what stack levels mean.

For some hands-on experimenting, I suggest:

proc top lvl {med $lvl}
proc med lvl {bot $lvl}
proc bot lvl {info level $lvl}
foreach lvl {-3 -2 -1 0 1 2 3 4} { catch { top $lvl } msg; puts "$lvl: $msg" }

Spoiler: the observed behaviour is entirely correct wrt the doc.

0 new messages