我发现PHP中的print_r非常有用,但想知道在Perl中是否有任何远程等效的东西?
注意@tchrist建议Data :: Dump over Data :: Dumper.我不知道它,但从它的外观来看,似乎它更容易使用,产生更好看,更容易解释结果.
Data :: Dumper:
以上链接中显示的示例片段.
use Data::Dumper; package Foo; sub new {bless {'a' => 1, 'b' => sub { return "foo" }}, $_[0]}; package Fuz; # a weird REF-REF-SCALAR object sub new {bless \($_ = \ 'fu\'z'), $_[0]}; package main; $foo = Foo->new; $fuz = Fuz->new; $boo = [ 1, [], "abcd", \*foo, {1 => 'a', 023 => 'b', 0x45 => 'c'}, \\"p\q\'r", $foo, $fuz]; ######## # simple usage ######## $bar = eval(Dumper($boo)); print($@) if $@; print Dumper($boo), Dumper($bar); # pretty print (no array indices) $Data::Dumper::Terse = 1; # don't output names where feasible $Data::Dumper::Indent = 0; # turn off all pretty print print Dumper($boo), "\n"; $Data::Dumper::Indent = 1; # mild pretty print print Dumper($boo); $Data::Dumper::Indent = 3; # pretty print with array indices print Dumper($boo); $Data::Dumper::Useqq = 1; # print strings in double quotes print Dumper($boo);
与Perl一样,您可能更喜欢古老的Data :: Dumper的替代解决方案:
Data :: Dump :: Streamer的输出比Data :: Dumper高,并且还可以比Data :: Dumper更好地序列化一些数据,
YAML(或Yaml :: Syck,或其他YAML模块)在YAML中生成数据,这是非常清晰的.
当然,使用调试器,您可以使用'x'命令显示任何变量.我特别喜欢表单' x 2 $complex_structure
',其中2(或任何数字)告诉调试器只显示2级嵌套数据.
Data :: Dumper的替代方法是不生成有效的Perl代码,而是更易于浏览的格式(与x
Perl调试器的命令相同)是Dumpvalue.它也消耗了更少的内存.
同样,还有Data :: Dump :: Streamer,它在各种边缘和角落情况下比Data :: Dumper更准确.