How can I return from a proc to a higher context?
For example: If proc X called another proc Y which called a third proc Z - is there a way to return from Z directly back to X ?
views:
41answers:
2
A:
if You are call "x process" from "z proc" then loop will create in the your process flow..
heyram
2010-08-16 08:47:00
I actually I meant to return to the place where Y was called in X, to continue the program from there. (Y will not be called over and over again)
thedp
2010-08-16 08:50:32
i think below answer is suitable for u
heyram
2010-08-16 08:55:54
@heyram: you might want to consider rewriting your answer to use proper words. Using phrases like "u r" for "you are" lower the credibility of your answer by a few orders of magnitude.
Bryan Oakley
2010-08-16 13:43:43
+6
A:
From 8.5 onwards, yes. The return
command has a -level
option which is used to do just that:
return -level 2 $someValue
Thus, for example:
proc X {} {
puts "X - in"
Y
puts "X - out"
}
proc Y {} {
puts "Y - in"
Z
puts "Y - out"
}
proc Z {} {
puts "Z - in"
return -level 2 "some value"
puts "Z - out"
}
X
produces this output:
X - in
Y - in
Z - in
X - out
Note that doing this reduces the reusability of Z
, but that's your business.
Donal Fellows
2010-08-16 08:50:59
I've always used `uplevel 1 [list return $some_value]` to do this which works even on older versions of tcl.
slebetman
2010-08-17 22:21:54
@slebetman: Did you ever check that in a simple harness like the one above? Because when I do, it also prints out “`Y - out`”, indicating that it isn't doing what you're hoping it does…
Donal Fellows
2010-08-18 08:02:26