Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCL: Return to a higher level?

Tags:

tcl

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 ?

like image 608
thedp Avatar asked Sep 02 '25 10:09

thedp


1 Answers

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.

like image 82
Donal Fellows Avatar answered Sep 05 '25 16:09

Donal Fellows