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

后端 未结 5 1164
我在风中等你
我在风中等你 2021-02-05 03:59

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

5条回答
  •  别那么骄傲
    2021-02-05 04:38

    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.

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

提交回复
热议问题