Subject: | Objects in trace() args() are stringified |
When a method is called on an object the first argument to the method
call is the object, but the argument returned via
trace()->frame(1)->args() is a stringified representation of the object.
The following program demonstrates the problem:
use strict;
use warnings;
use Exception::Class qw(MyExceptionClass);
package MyExceptionClass;
sub full_message {
my $self = shift;
my $method = $self->trace()->frame(1)->subroutine();
my $object = ($self->trace()->frame(1)->args())[0];
my $class = ref $object;
return "$method called on $object, an instance of $class";
}
package MyClass;
sub new { return bless {}, shift }
sub boom { MyExceptionClass->throw() }
package main;
my $obj = MyClass->new();
eval { $obj->boom() };
print $@->as_string();
__END__
That outputs
MyClass::boom called on MyClass=HASH(0x1824ac8), an instance of
rather than the expected
MyClass::boom called on MyClass=HASH(0x1824ac8), an instance of MyClass
I believe this is a bug in Exception::Class rather than
Devel::StackTrace because the following program using Devel::StackTrace
directly works fine:
use strict;
use warnings;
package MyClass;
use Devel::StackTrace;
sub new { return bless {}, shift }
sub trace {
my $self = shift;
my $trace = Devel::StackTrace->new();
my $method = $trace->frame(1)->subroutine();
my $object = ($trace->frame(1)->args())[0];
my $class = ref $object;
print "$method called on $object, an instance of $class";
}
package main;
my $obj = MyClass->new();
$obj->trace();
__END__
That outputs
MyClass::trace called on MyClass=HASH(0x1824ac8), an instance of MyClass
as expected.