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

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

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

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


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.


I went looking for the same thing and found this lovely little Perl function, explicitly meant to generate results like 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...

Comments

  1. Jadiel

    • 2017/10/14

    is a very sad one as it went from one of the top web programming languages, but, as time went on, it became viewed as a write-only language which triggered its demise. In the end, it had a good run, but it would be better to forget about this language and focus on something that is popular right now.

  2. Caden

    • 2020/12/14

    Perl is the Swiss Army chainsaw of scripting languages: powerful and adaptable. It was first developed by Larry Wall, a linguist working as a systems administrator for NASA in the late 1980s, as a way to make report processing easier.

  3. Kody

    • 2016/9/23

    -e commandline: may be used to enter one line of script. Multiple -e commands may be given to build up a multi-line script. If -e is given, perl will not look for a 

  4. Bernard

    • 2021/3/9

    We recommend writing web scripts in PHP, not CGI / Perl. PHP is much better suited to the Web and takes less “overhead,” meaning that scripts will run faster and the server will be able to handle more simultaneous users on your site.

  5. Diaz

    • 2020/7/13

    Built-in Facilities. Unlike Perl, which is a general purpose scripting language that you can use for a wide variety of purposes (and not just 

  6. Hugo

    • 2017/12/28

    The Perl Maven tutorial will teach you the basics of the Perl programming language. You'll be able to write simple scripts, analyze log files and read and write 

  7. Hayden

    • 2018/2/14

    Createuser.pl The following is the simplest example of a Perl script using Essbase.pm. The script establishes a connection to the Essbase Server, creates a user, and disconnects. # Use the Essbase.pm module.

  8. Richard

    • 2016/7/4

    A Perl program consists of a sequence of declarations and statements, which run from the top to the bottom. Loops, subroutines, and other control structures allow you to jump around within the code. Every simple statement must end with a semicolon (;).

  9. Ferrari

    • 2020/11/19

    www.tutorialspoint.com › perl

  10. Westley

    • 2018/5/6

    The downside of Perl is its declining popularity. This language isn’t in top demand right now, so it’s getting harder and harder to find a full team of high-level Perl developers. Performance. Which one is victorious in the Python vs PHP performance 2018 battle? Or is it Ruby vs Python performance competition that matters? Let’s figure

  11. Ambrose

    • 2017/4/2

    Perl is a programming language developed by Larry Wall, especially designed for text processing. It stands for Practical Extraction and Report Language. It runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. This tutorial provides a complete understanding on Perl.

  12. Dorian

    • 2020/12/10

    Before we can install Perl on your windows system, you'll need to download it. Go to ActiveState's ActivePerl home page (ActiveState is http://www.activestate.com/). Click on 'Free Download'. There is no need to fill out any of the contact information on the next page in order to download ActivePerl. Click 'Next' when you're ready, and on the download page, scroll down the list to find the Windows distribution.

  13. Andy

    • 2016/10/20

    0. These modules enable you to run Gateway command lines from a Perl script: GIKCMDPL: module for local command calls; GIKCCMDPL 

  14. Aryan

    • 2015/6/5

    Perl - Highly capable, feature-rich programming language with over 26 years of development. PHP - A popular general-purpose scripting language that is especially suited to web development. Python - A clear and powerful object-oriented programming language, comparable to Perl, Ruby, Scheme, or Java.

  15. Victor

    • 2020/6/14

    What is the definition of Perl? What is the meaning of Perl? How do you use Perl in a sentence? What are synonyms for Perl?

  16. Hudson

    • 2020/9/10

    This chapter is designd to quickly get you up to speed on the syntax that is expected in Perl. A Perl program consists of a sequence of declarations and statements, which run from the top to the bottom. Loops, subroutines, and other control structures allow you to jump around within the code. Every simple statement must end with a semicolon (;).

  17. Clayton

    • 2016/12/17

    Is Perl still used?

  18. Roger

    • 2015/11/24

    Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was officially changed to Raku in October 2019.

  19. Brixton

    • 2016/2/9

    Posted: Mar 28, 2016

  20. Sean

    • 2018/11/30

    Code Examples CSc 231 Perl: This page links a large number of small code examples for use in lecture. Basic Constructs: Perl Hello, World! Hello Again: Scalar Variables:

  21. Azariah

    • 2019/1/11

    .

  22. Koroveshi

    • 2021/3/17

    Perl is a high-level programming language that’s easier to learn when compared with Python. Python is more robust, scalable, and stable when compared to Perl. While Perl code can be messy, featuring many paths to accomplish the same goal, Python is clean and streamlined. Hello, and welcome to your new coding journey!

  23. Gunner

    • 2016/8/7

  24. Easton

    • 2018/5/15

    PERL is a high-level, general-purpose, interpreted, dynamic programming language. Perl is a term stands for "Practical Extraction and Reporting Language" even though there is no acronym for Perl. It was introduced by Larry Wall in 1987. Perl was specially designed for text editing.

  25. Turner

    • 2021/4/6

    Perl file name can contain numbers, symbols, letters and underscore (_), however spaces are not allowed in the file name. for e.g. hello_world.pl is a valid file 

  26. Kamdyn

    • 2021/2/21

    sub f { @_ }, function definition (variable number of arguments). return(5), function return value (breaks the control flow). no syntax needed(6), function return 

  27. Evans

    • 2020/10/25

    Perl is a general-purpose, high level interpreted and dynamic programming language. It was developed by Larry Wall, in 1987. There is no 

  28. Stefan

    • 2020/2/10

    There are some variables which have a predefined and special meaning in Perl. They are the variables that use punctuation characters after the usual variable indicator ($, @, or %), such as $_ ( explained below ). Most of the special variables have an english like long name, e.g., Operating System Error variable $! can be written as $OS_ERROR.

  29. Iker

    • 2015/6/25

    Top definition. perl Whew, that perl coding guy is due for his monthly shower today! Perl is a script programming language that is similar in syntax to the C 

  30. Berisha

    • 2020/3/3

    Search For Info About Learn perl programming. Browse & Get Results Instantly. FastQuickSearch Provides Comprehensive Information About Your Query. Visit Us.

  31. Joe

    • 2018/9/28

    Perl is the Swiss Army chainsaw of scripting languages: powerful and At this point you have some basic knowledge of Perl syntax and a few 

  32. Kohen

    • 2015/2/5

    .

  33. Gray

    • 2018/5/29

    Download and install Padre, the Perl IDE/editor (Strawberry Perl version 5.12.3 comes as part of the install, you also get many other useful CPAN modules as well). Log out and back in (or reboot) Go to your start menu, then click the "Perl command" link To confirm the installation worked type: perl -v

  34. Jovanni

    • 2021/7/24

    What is difference between Perl and Python?

  35. Nehemiah

    • 2020/4/1

    Perl is a lot alike to C syntactically. And is simple for the users who have knowledge of C, and C++ programming languages. Python is a widely used high-level, general-purpose programming language. By Guido van Rossum it was originally created in 1991 and developed by Python Software Foundation.

Comments are closed.

Recent Posts