Perl 相当于 PHP 的 print_r() 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/229168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:59:23  来源:igfitidea点击:

What is Perl's equivalent to PHP's print_r()?

phpperldebugging

提问by Steve M

I find print_r in PHP extremely useful, but wonder if there is anything remotely equivalent in Perl?

我发现 PHP 中的 print_r 非常有用,但想知道 Perl 中是否有任何远程等效的东西?

回答by Vinko Vrsalovic

Note @tchrist recommends Data::Dumpover Data::Dumper. I wasn't aware of it, but from the looks of it, seems like it's both far easier to use and producing better looking and easier to interpret results.

注意 @tchrist 建议使用Data::Dump 而不是Data::Dumper。我不知道它,但从它的外观来看,它似乎更容易使用,并且产生更好看和更容易解释的结果。

Data::Dumper:

数据::倾销者

A snippet of the examples shown in the above link.

上面链接中显示的示例片段。

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);

回答by mirod

As usually with Perl, you might prefer alternative solutions to the venerable Data::Dumper:

与 Perl 一样,您可能更喜欢古老的 Data::Dumper 的替代解决方案:

  • Data::Dump::Streamerhas a terser output than Data::Dumper, and can also serialize some data better than Data::Dumper,
  • YAML(or Yaml::Syck, or an other YAML module) generate the data in YAML, which is quite legible.
  • Data::Dump::Streamer的输出比Data::Dumper更简洁,也能比Data::Dumper更好的序列化一些数据,
  • YAML(或Yaml::Syck或其他 YAML 模块)在 YAML 中生成数据,非常清晰。

And of course with the debugger, you can display any variable with the 'x' command. I particularly like the form 'x 2 $complex_structure' where 2 (or any number) tells the debugger to display only 2 levels of nested data.

当然,使用调试器,您可以使用“x”命令显示任何变量。我特别喜欢 ' x 2 $complex_structure'形式,其中 2(或任何数字)告诉调试器只显示 2 级嵌套数据。

回答by Aristotle Pagaltzis

An alternative to Data::Dumperthat does not produce valid Perl code but instead a more skimmable format (same as the xcommand of the Perl debugger) is Dumpvalue. It also consumes a lot less memory.

一个替代数据::自卸车不产生有效的Perl代码,而是一个更skimmable格式(同xPerl调试器的命令)是Dumpvalue。它也消耗更少的内存。

As well, there is Data::Dump::Streamer, which is more accurate in various edge and corner cases than Data::Dumperis.

同样,还有Data::Dump::Streamer,它在各种边缘和角落情况下比Data::Dumper更准确。

回答by kixx

I use Data::Dump, it's output is a bit cleaner than Data::Dumper's (no $VAR1), it provides quick shortcuts and it also tries to DTRT, i.e. it will print to STDERR when called in void context and return the dump string when not.

我使用Data::Dump,它的输出比 Data::Dumper 的更清晰(没有 $VAR1),它提供快速快捷方式,它还尝试 DTRT,即在 void 上下文中调用时它将打印到 STDERR 并返回转储字符串不时。

回答by Stan James

I went looking for the same thing and found this lovely little Perl function, explicitly meant to generate results like print_r().

我去寻找同样的东西,发现了这个可爱的小 Perl 函数,明确地意味着生成像 print_r() 这样的结果。

The author of the script was asking your exact question in a forum here.

该脚本的作者在此处的论坛中询问了您的确切问题。

print objectToString($json_data);

Gives this output:

给出这个输出:

HASH {
time                             => 1233173875
error                            => 0
node                             => HASH {
    vid                              => 1011
    moderate                         => 0
    field_datestring                 => ARRAY {
        HASH {
            value                            => August 30, 1979
        }
    }

    field_tagged_persons             => ARRAY {
        HASH {
            nid                              => undef
        }
    }

...and so on...