Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you best attribute errors in a Perl method to the caller
    primarykey
    data
    text
    <p>Given a method that may fail with warnings and/or errors, I want the error method to show up at the caller. Fir instance this script:</p> <pre><code>foo(0); # line 1 sub foo { 1 / shift; # line 4 } </code></pre> <p>Produces the error <code>Illegal division by zero at foo.pl line 4</code>, but I want <code>Illegal division by zero at foo.pl line 1</code>. There should be several ways if I put the method in a module or if I wrap the method body in eval, but I have not found an easy way like this: </p> <pre><code>sub foo { attributeErrorsToCaller; # do some magic 1 / shift; } </code></pre> <p>Is there such a way?</p> <hr> <p><strong>EDIT:</strong> <a href="https://stackoverflow.com/questions/6339816/how-do-you-best-attribute-errors-in-a-perl-method-to-the-caller/6340348#6340348">mirod's answer</a> comes close not what I was looking for:</p> <pre><code>Foo::foo(0); # line 1 package Foo; use diagnostics -traceonly; BEGIN { disable diagnostics; } sub foo { enable diagnostics; 1 / shift; # line 9 } </code></pre> <p>Without <code>enable diagnostics</code> the error message is <code>Illegal division by zero at foo.pl line 9.</code>. With <code>enable diagnostics</code> it is still too verbose, but this may also be useful:</p> <pre><code>Uncaught exception from user code: Illegal division by zero at foo.pl line 10. at foo.pl line 10 Foo::foo(0) called at foo.pl line 2 </code></pre> <p>I bet I could <a href="http://search.metacpan.org/#/showsrc/diagnostics" rel="nofollow noreferrer">hack diagnostics</a> to get exactely the feature I want, but using diagnostics as raw module is probably more recommended.</p>
    singulars
    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.
 

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