Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Esteemed monks,

What is the best way to escape from multilevel, deeply nested calls to the main program (say with the call nesting level close to a hundred) that resulted in error somewhere close the most deep nesting level (unwinding of failed calls).

Can I use exceptions mechanism for this to jump immediately to the main program? If yes, how?

For example, in the example below (for demonstration purposes only) we need to exit from all levels if we detect a loop in recursion calls. And the required code is rather ugly and inefficient: checks need to be made both on entry and after each subroutine call in each module.

use v5.10; use warnings; use strict 'subs'; use feature 'state'; $global_failure=0; $rc=d('d'); say "\nProgram ended"; say "Failure_code is $global_failure"; exit $rc; sub d { state $recursion_level=0; $recursion_level++; if ($recursion_level>10){ $global_failure++; return $recursion_level; } print $_[0].$recursion_level; $rc=e('e'); if( $rc ){ $global_failure++; $global_failure++; print '-'.$_[0].$recursion_level; $recursion_level--; return $recursion_level; } $recursion_level--; return 0; } sub e { state $recursion_level=0; $recursion_level++; if ($recursion_level>10){ $global_failure++; print '-'.$_[0].$recursion_level; $recursion_level--; return $recursion_level; } print $_[0].$recursion_level; $rc=f('f'); if ($rc) { $global_failure++; print '-'.$_[0].$recursion_level; $recursion_level--; return $rc; } $recursion_level--; return 0; } sub f { state $recursion_level=0; $recursion_level++; if ($recursion_level>10){ $global_failure++; print '-'.$_[0].$recursion_level; $recursion_level--; return $recursion_level; } print $_[0].$recursion_level; $rc=f('f'); if ($rc) { $global_failure++; $recursion_level--; print '-'.$_[0].$recursion_level; return $rc; } $recursion_level--; return 0; }
... ... ...

In reply to Propagaing error from deeply nested calles by likbez

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2021-11-29 11:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?