我有一些複雜的對象結構,我使用Data :: Printer來檢查它們。一個沒有足夠幫助的例子是:當一個對象(容器)的某個字段是另一個對象(子對象)時,該子對象在DDP的輸出中只顯示爲類名。我也希望看到孩子的價值。如何使Data :: Printer在內部顯示字符串化值?
讓我們有一個例子:
{
package Child;
use Moo;
use overload '""' => "stringify";
has 'value', is => 'ro';
sub stringify {
my $self = shift;
return "<Child:" . $self->value . ">";
}
}
{
package Container;
use Moo;
has 'child', is => 'ro';
}
my $child_x = Child->new(value => 'x');
print "stringified child x: $child_x\n";
my $child_y = Child->new(value => 'y');
print "stringified child y: $child_y\n";
my $container_x = Container->new(child => $child_x);
my $container_y = Container->new(child => $child_y);
use DDP;
print "ddp x: " . p($container_x) . "\n";
print "ddp y: " . p($container_y) . "\n";
輸出:
stringified child x: <Child:x>
stringified child y: <Child:y>
ddp x: Container {
Parents Moo::Object
public methods (2) : child, new
private methods (0)
internals: {
child Child # <- note this
}
}
ddp y: Container {
Parents Moo::Object
public methods (2) : child, new
private methods (0)
internals: {
child Child # <- and this
}
}
正如你看到的,孩子們都在輸出區分。我希望看到在那個地方的字符串化,除了或不是類名之外。
謝謝你讓我走上正軌。我也注意到了這個段落,但是由於某種原因,認爲使用這個功能會很困難,而不是。我將以後代爲例來回答這個問題。 – Arry