Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The only reasonable way to backtrack multiple levels up the call stack is by throwing an exception/dying.</p> <p>That said, you <em>really</em> should reconsider what you want to do. A programmer (including yourself, six months from now) will expect that, when a function call completes, the statement after it will execute (unless an exception is thrown). Violating that expectation will cause bugs which are caused in <code>handle_error</code>, but appear to be with the code which called <code>handle_error</code>, making them extremely difficult to debug. This is Not A Good Thing.</p> <p>You're also making the assumption that there is <em>absolutely no situation</em> in which continuing after an error is handled will be appropriate. Hardcoding an assumption like that is practically a sure guarantee that, as soon as you've had time to forget it, you'll come across a case where you need to continue after calling <code>handle_error</code> (and then waste huge amounts of time trying to figure out why the code after <code>handle_error</code> doesn't get run).</p> <p>And then there's the assumption that you'll <em>always</em> want to skip back <em>exactly</em> two levels in the call stack. That's another assumption that will fail as soon as it's hardcoded. Not only will there be cases where the calling code should continue, there will also be cases where you need to go three levels up the call stack.</p> <p>So just have <code>handle_error</code> exit by calling <code>die</code> instead of <code>return</code> and trap the exception at the appropriate level where execution should continue. You don't know every place where the sub will ever be called, so you can't predict how many levels back it will need to go.</p> <p>In the code at hand, if the extra line to just say <code>return</code> is bothering you, you can use <code>return $x-&gt;handle_error;</code> You can even get rid of the enclosing scope and make it <code>return $x-&gt;handle_error if $x-&gt;{size} &gt; 1000;</code> There - three lines removed rather than just one, plus a pair of braces and two pairs of parentheses as a free bonus.</p> <p>Finally, I would also suggest changing the name of <code>handle_error</code> to better reflect what it actually does. (<code>report_error</code>, maybe?) "Handling an error" usually means cleaning things up to resolve the error so that execution to continue. If you want your <code>handle_error</code> to prevent the code which called it from continuing, then it seems very unlikely that it's cleaning things up to make continuation possible and, once again, it will cause nasty, hard-to-debug surprises for future programmers using this code.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload