http://www.perlmonks.org?node_id=1034144

unlinker has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Venerable Monks,

I have a need to return from a deeply nested function, not to the caller, but to a higher point up the call stack. After grovelling at the feet of Google, I understand that, what I need are "Continuations" - functions that not only specify what to return, but also specify where to return to, up the call stack.

The only way I can think of doing this is by throwing exceptions (say at nesting level 4) and catching them at a specified position up the call chain (say at nesting level 1). But it somehow seems wrong (what if a function between 1 and 4 catches it? What if some other legitimate exceptions are thrown?).

Can I implement continuations in Perl in a more disciplined/organized fashion? If yes, can the wise monks here educate me with an example please?