什么是Perl相当于PHP的print_r()? [英] What is Perl's equivalent to PHP's print_r()?
本文介绍了什么是Perl相当于PHP的print_r()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
注意@tchrist建议 Data :: Dump over Data :: Dumper 。我没有意识到这一点,但从外观来看,似乎更容易使用,并且更容易理解结果。
上述链接中显示的示例片段。
使用数据: :倾销
包Foo;
sub new {bless {'a'=> 1,'b'=> sub {returnfoo}},$ _ [0]};
包装Fuz; #一个奇怪的REF-REF-SCALAR对象
sub new {bless \($ _ = \'fu\'z'),$ _ [0]};
包主;
$ foo = Foo-> new;
$ fuz = Fuz-> new;
$ boo = [1,[],abcd,\ * foo,
{1 => 'a',023 => 'b',0x45 => 'c'},
\\p\q\'r,$ foo,$ fuz];
########
#简单用法
########
$ bar = eval(Dumper $ boo));
print($ @)if $ @;
print Dumper($ boo),Dumper($ bar); #pretty print(no array indices)
$ Data :: Dumper :: Terse = 1; #不要在可行的情况下输出名称
$ Data :: Dumper :: Indent = 0; #关掉所有漂亮的打印
打印Dumper($ boo),\\\
;
$ Data :: Dumper :: Indent = 1; #温和漂亮的打印
打印Dumper($ boo);
$ Data :: Dumper :: Indent = 3; #美式打印数组索引
print Dumper($ boo);
$ Data :: Dumper :: Useqq = 1; #打印双引号字符串
print Dumper($ boo);
I find print_r in PHP extremely useful, but wonder if there is anything remotely equivalent in Perl?
解决方案
Note @tchrist recommends Data::Dump over 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.
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);
这篇关于什么是Perl相当于PHP的print_r()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文