Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl: Force stacktrace for "can't call method on undefined"

Tags:

exception

perl

I'm using Perl and Catalyst as Web-Framework.

How can I globally force a stacktrace if an exception like Can't call method "XXX" on an undefined value... is thrown?

Suppose the following code in your Controller/Root.pm

use DateTime;

sub test  :Local :Args(0) {
    my ( $self, $c ) = @_;
    my $now = DateTime->now(time_zone=>'local');
    my $tmp = undef;

    my $throwing = $tmp - $now; #this will throw an exception!
    $c->res->body("OK");
}

Opening http://localhost:3000/test throws of course the exception:

Caught exception in MyApp::Controller::Root->test "Can't call method "subtract_datetime" on an undefined value at /usr/local/lib/perl/5.10.1/DateTime.pm line 1619, line 1003."

The stacktrace is missing!

How can I force in such a case a stacktrace?

I.e. I would like to get (not a real output, only composed by hand):

MyApp::Controller::Root::__ANON__('Can\'t call method "subtract_datetime" on an undefined value at /usr/local/lib/perl/5.10.1/DateTime.pm line 1619, <DATA> line 1003.^J') called at /home/user/MyApp/script/../lib/MyApp/Controller/Root.pm line 114
MyApp::Controller::Root::test('MyApp::Controller::Root=HASH(0x20583b60)', 'MyApp=HASH(0x206e9140)') called at /usr/local/share/perl/5.10.1/Catalyst/Action.pm line 65
Catalyst::Action::execute('Catalyst::Action=HASH(0x20668e88)', 'MyApp::Controller::Root=HASH(0x20583b60)', 'MyApp=HASH(0x206e9140)') called at /usr/local/share/perl/5.10.1/Catalyst.pm line 1687
eval {...} at /usr/local/share/perl/5.10.1/Catalyst.pm line 1687
Catalyst::execute('MyApp=HASH(0x206e9140)', 'MyApp::Controller::Root', 'Catalyst::Action=HASH(0x20668e88)') called at /usr/local/share/perl/5.10.1/Catalyst/Action.pm line 60
[ and the rest of the exception ...]

With this I know exactly that the exception was caused by the line 114 in Root.pm

like image 307
Stefan Profanter Avatar asked Jul 19 '26 10:07

Stefan Profanter


1 Answers

I am not sure if it works within Catalyst, but usually you can force a stacktrace by including the CPAN module Carp::Always, e.g. by writing somewhere in your program use Carp::Always; or starting the perl script with the -MCarp::Always switch.

like image 196
Slaven Rezic Avatar answered Jul 21 '26 02:07

Slaven Rezic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!