simeon2000 has asked for the wisdom of the Perl Monks concerning the following question:

I've been a longtime programmer of perl. However, recently I have started using php as well. PHP's print_r and var_dump functions are great for debugging large data structures.

In the past, I never used this in perl, because I usually kept my data structures pretty simple. But with the ease of use of print_r in particular, I have begun longing for such a function in perl. Is there a recursive pretty-data printer in perl? Is there a module for this? Any assistance would be appreciated

--
Simeon2000
JAPU

Replies are listed 'Best First'.
Re: print_r or var_dump?
by stefan k (Curate) on Oct 09, 2001 at 17:14 UTC
    OF course there ist ;-)
    use Data::Dumper; # ... %some_complicated_hash = (); # fill hash with values... print Dumper(\%some_complicated_hash);
    That should pretty much do what you want plus you get valid perl code from it.

    Regards... Stefan
    you begin bashing the string with a +42 regexp of confusion

      -=- MamuT -=-
      Another feature is if you write it to file.
      use Data::Dumper; my $conf = ( foo => 'foofoo', bar => 'barbar', ); open CONF,"myconfig.conf"; print CONF Dumper($conf); close CONF;
      In another program you make it easy to load it back to Perl.
      my $VAR1; #you must declare VAR1 from file if you use strict open CONF,"myconfig.conf"; my $conf = eval (join "",<CONF>); close CONF;
      Now you have stored structure in $conf. Same you can do with perl object.

      Mamut
Re: print_r or var_dump?
by mirod (Canon) on Oct 09, 2001 at 17:30 UTC

    I like Data::Denter myself. It's a plugin replacement for Data::Dumper (just change the use line and that's all). The output is more compact than Data::Dumper's, and I find it easier to read.

Re: print_r or var_dump?
by MZSanford (Curate) on Oct 09, 2001 at 17:14 UTC
    I would look at Data::Dumper. It is in the standard distribution, and does a good job of it.
    "They shall not overcome. Whoever told them that the truth shall set them free was obviously and grossly unfamiliar with federal law."
        -- John Ashcroft
Re: print_r or var_dump?
by Rhose (Priest) on Oct 09, 2001 at 17:15 UTC
    While I am not familiar with the functionality of print_r and var_dump, I use Data::Dumper to display perl data structures.

    Does this help?

Re: print_r or var_dump?
by Rudif (Hermit) on Oct 10, 2001 at 03:22 UTC
    If you would like to have a graphical browser for perl structures, it's easy:

    - install package Tk

    - install package Tk::ObjScanner

    - run the demo below

    #!perl -w # This demo of Tk-ObjScanner # is a stripped-down version of the test file Tk-ObjScanner\t\basic.t # The module Tk::ObjScanner by Dominique Dumont is available on CPAN a +nd on PPM use strict ; package Toto ; # creates a moderately complicated perl structure sub new { my $type = shift ; my $tkstuff = shift ; my $scalar = 'dummy scalar ref value'; open (FILE,"$0") || die "can't open myself !\n"; my $glob = \*FILE ; # ??? my $self = { 'key1' => 'value1', 'array' => [qw/a b sdf/, {'v1' => '1', 'v2' => 2},'dfg'], 'key2' => { 'sub key1' => 'sv1', 'sub key2' => 'sv2' }, 'some_code' => sub {print "some_code\n";}, 'piped|key' => {a => 1 , b => 2}, 'scalar_ref_ref' => \\$scalar, 'filehandle' => $glob, 'empty string' => '', 'non_empty string' => ' ', 'long' => 'very long line'.'.' x 80 , 'is undef' => undef, 'some text' => "some \n dummy\n Text\n", 'tk widget' => $tkstuff } ; bless $self,$type; } package main; use Tk ; use Tk::ObjScanner ; my $mw = MainWindow-> new ; my $dummytolookat = new Toto ($mw); $mw -> ObjScanner ( 'caller' => $dummytolookat, #destroyable => 0, title => 'test scanner' ) -> pack(expand => 1, fill => 'both') ; MainLoop ; # Tk's
    Both packages are available from CPAN or from PPM (Active State)

    Rudif

Re: print_r or var_dump?
by simeon2000 (Monk) on Oct 09, 2001 at 17:18 UTC
    Thanks a lot, guys. Sorry to ask such a question. Thanks for the VERY quick help :)

    --
    Simeon2000
    JAPU