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

In the following minimal example, I get an error message from the Math::Complex package, but no stack trace or indication that the error happened at line 4 of the program. In a situation like this, how do I force a stack trace to be output so that I can find out where the error is happening? Is this package somehow defeating perl's normal error-reporting mechanisms? Is this a bug, or just a bad design choice?

Code:

use Math::Complex; use strict; my $x = 1+1*i; print $x*undef;
Output:
Use of uninitialized value $z2 in multiplication (*) at /usr/share/per +l/5.14/Math/Complex.pm line 471. Use of uninitialized value $z2 in multiplication (*) at /usr/share/per +l/5.14/Math/Complex.pm line 471. 0

Replies are listed 'Best First'.
Re: how to force a stack trace
by educated_foo (Vicar) on Mar 13, 2013 at 16:22 UTC
      Thanks, educated_foo, McA, Athanasius, and Dallaylaen -- that was very helpful! -Ben
Re: how to force a stack trace
by McA (Priest) on Mar 13, 2013 at 16:25 UTC

    Hi,

    have a look at Carp::Always, Devel::SimpleTrace and perldoc perlvar searching for %SIG.

    Best regards
    McA

Re: how to force a stack trace
by Athanasius (Archbishop) on Mar 13, 2013 at 17:10 UTC
    Is this package somehow defeating perl's normal error-reporting mechanisms?

    No, the messages are warnings, not errors (i.e. exceptions). They appear because Math::Complex invokes the warnings pragma on line 73 of the source code (version 1.59).

    Carp::Always will display the line number you are seeking. But it won’t produce a stack trace, because no exception is thrown:

    3:06 >perl 573_SoPW.pl Use of uninitialized value $z2 in multiplication (*) at C:\Perl\lib\pe +rl5/Math/Complex.pm line 489. Math::Complex::_multiply('Math::Complex=HASH(0x186e5e4)', unde +f, '') called at 573_SoPW.pl line 5 Use of uninitialized value $z2 in multiplication (*) at C:\Perl\lib\pe +rl5/Math/Complex.pm line 489. Math::Complex::_multiply('Math::Complex=HASH(0x186e5e4)', unde +f, '') called at 573_SoPW.pl line 5 0 3:06 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: how to force a stack trace
by Dallaylaen (Chaplain) on Mar 13, 2013 at 18:18 UTC

    Sometimes I use the following method for debugging hard to track warnings (there's actually a Log4perl-compatible object instead of print there, use whatever fits you):

    use Carp; local $SIG{__WARN__} = sub { print( Carp::longmess (shift) ); };

    This is a "trick" however and should be agreed upon by the rest of the team. See also this node for sigwarn/sigdie explanation.