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

I'm having loads of fun with perl on Pocket_PC from General Paranoyaxc Software and the PerlIDE there too. Of course, Perl is stripped a bit bare for the environment. One of the things I was wanting was Data::Dumper. Silly me, I thot, "Why not write a dumbed down version that's perl only." Trouble is, I couldn't figure how to generically de-ref a blessed object. This is probably impossible? I'm assuming no use strict.
# NOTE: THIS IS BUSTED, but interesting... sub DDumper { my($v,$i)=@_; # v=what we're dumping, i=indent $i="\n" unless $i; $i.=' '; my $r=ref($v); my $s=''; # s is the string we'll return when done if($r eq HASH) { $s.= "{ "; for (keys %$v) { $s.= Dumper($_,$i)." => ".Dumper($v->{$_},$i).",$i"; } $s.= " }$i"; } elsif($r eq ARRAY) { $s.= "[ "; for ( @$v) { $s.= Dumper($_,$i).",$i"; } $s.= " ]$i"; } elsif($r) { $s.= "bless( ".Dumper(%{$v},$i).", '$r') $i"; } else { $s.= "'$v' "; } $s; }
For discussion sake, let's assume all blessed objects are hashes. Does that help?

This snippit recurses endlessly on blessed objects (I think) and has the curious property of doubling square brackets and doing goofy things with quotes and commas in hashes.

Well, it was an interesting recursive diversion...

j

Replies are listed 'Best First'.
Re: Data::Dumper in perl-only?
by PrakashK (Pilgrim) on Jan 24, 2002 at 03:46 UTC
      Denter is pure perl. I think it oughta work on my Pocket_PC.

      thx,
      j

Re: Data::Dumper in perl-only?
by FoxtrotUniform (Prior) on Jan 24, 2002 at 03:12 UTC

    If you have access to a copy of Advanced Perl Programming, by Sriram Srinivasan, you will probably find the pretty-printing example near the end of the second chapter useful; it does pretty much exactly what you want.

    --
    :wq
Re: Data::Dumper in perl-only?
by perrin (Chancellor) on Jan 24, 2002 at 03:40 UTC
    Isn't Data::Dumper pure perl? I thought the XS part was optional.
Re: Data::Dumper in perl-only?
by theorbtwo (Prior) on Jan 24, 2002 at 05:59 UTC
      Ahhh... very interesting... See, this is why I need Perl Monks to enlighten me. I wouldn't have guessed that the stringified version gave more than ref() did. Thanks, theorbtwo!
        Er. Actually you want to watch out that the objects dont have "" overloaded.

        In order to be totally safe you need to use

        overload::StrVal($obj)
        Also, instead of the above mentioned Data::Denter (which I dont like) I would recommend Data::Dump (which I do).

        Yves / DeMerphq
        --
        Data::PrettySimple