[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

DDump



I wanted Devel::Peek (sv_dump) to return me the content as a string of
better even as a hash at least twice the past week for completely
different problems.

Do you want me to expose the following working snippet to CPAN, extend
it to Devel::Peek in core, so people only get it with next updates, or
keep it to myself ...

--8<--- DDumper.pm
package DDumper;

use strict;
use warnings;

use Devel::Peek;
use Data::Dumper;

use base "Exporter";
use vars qw( $VERSION @EXPORT );
$VERSION = "0.05";
@EXPORT  = qw( DDumper DDump );

sub DDumper
{
    local $Data::Dumper::Sortkeys = 1;
    local $Data::Dumper::Indent   = 1;

    my $s = Data::Dumper::Dumper @_;
    $s =~ s!^(\s*)'([^']*)'\s*=>!sprintf "%s%-16s =>", $1, $2!gme;      # Align => '
    $s =~ s!^(?= *[]}](?:[;,]|$))!  !gm;
    $s =~ s!^(\s+)!$1$1!gm;
    return $s;
    } # DDumper

sub DDump
{
    local *ERR;
    open ERR, ">&STDERR";
    pipe my $rh, my $wh;
    unless (open STDERR, "|-") {
        close $rh;
        while (<>) {
            print $wh $_;
            }
        close $wh;
        exit;
        }
    close $wh;
    Dump @_;
    close STDERR;
    open STDERR, ">&ERR";
    my $dump = join "", <$rh>;
    close $rh;
    wantarray and
        return map { m/^\s* (\w+) \s* = \s* (.*)/x } split m/\n/, $dump;
    defined wantarray and
        return $dump;
    print STDERR $dump;
    } # DDump

"Indent";
-->8---

-- 
H.Merijn Brand          Amsterdam Perl Mongers  http://amsterdam.pm.org/
using & porting perl 5.6.2, 5.8.x, 5.10.x, 5.11.x on HP-UX 10.20, 11.00,
11.11, 11.23, and 11.31, SuSE 10.1, 10.2, and 10.3, AIX 5.2, and Cygwin.
http://mirrors.develooper.com/hpux/           http://www.test-smoke.org/
http://qa.perl.org      http://www.goldmark.org/jeff/stupid-disclaimers/


Follow-Ups from:
Nicholas Clark <nick@ccl4.org>

[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]