Variable dump

Discussion in 'Perl Misc' started by Bill H, Jul 29, 2007.

  1. Bill H

    Bill H Guest

    Is there a way in perl to dump all the variables used and their
    current values to a file without printing everyone of them
    individually?

    Bill H
     
    Bill H, Jul 29, 2007
    #1
    1. Advertising

  2. Bill H

    -berlin.de Guest

    Bill H <> wrote in comp.lang.perl.misc:
    > Is there a way in perl to dump all the variables used and their
    > current values to a file without printing everyone of them
    > individually?


    Not easily. You'd have a chance with package variables. For
    lexicals you'd need something like PadWalker, which is a bit
    tricky to use.

    Most people settle for printing selected values at selected
    places for debugging. The module Data::Dumper helps with
    viewing complex data structures.

    I have a module Report.pm in my private Perl library which
    applies Data::Dumper to a list of Perl expressions. I find
    that I never use it, but I'm appending it anyway. It uses
    a little-known feature to evaluate expressions in the caller's
    context.

    Anno

    --------------------------------------------------------------------
    package Report;
    use strict; use warnings; # @^~`
    use base 'Exporter';
    our @EXPORT = qw( report);

    {
    # evaluate in caller context
    package DB;
    sub _reporter_report {
    for my $expr ( @_ ) {
    print Report::answer( $expr, eval $expr);
    }
    }
    }
    *Report::report = \ &DB::_reporter_report;

    use Data::Dumper ();
    use Scalar::Util ();

    sub answer {
    my ( $expr, @val) = @_;
    my $ans;
    if ( $@ ) {
    $ans = "$expr: $@" if $@;
    $ans =~ s/ at \(eval .*$//;
    } else {
    if ( @val == 1 ) {
    $ans = answer_scalar( $expr, @val);
    } else {
    $ans =join ', ' => map answer_scalar( $expr, $_), @val;
    $ans = "($ans)";
    }
    }
    $ans;
    }

    sub answer_scalar {
    my ( $expr, $val) = @_;
    my $ans;
    if ( !defined $val ) {
    $ans = "$expr = undef;\n";
    } elsif ( ref $val ) {
    ( $ans = Data::Dumper::Dumper $val) =~ s/\$VAR1\b/$expr/g;
    my $indent = ' ' x ( 8 + length( $expr) - length( '$VAR'));
    $ans =~ s/^ {8}/$indent/mg;
    } elsif (
    Scalar::Util::looks_like_number( $val) or
    ref( \ $val) eq 'GLOB'
    ) {
    $ans = "$expr = $val;\n";
    } else {
    $ans = "$expr = '$val';\n";
    }
    $ans;
    }

    '@^~`'; # it's true
    __END__

    Use like this:

    #!/usr/local/bin/perl
    use strict; use warnings; $| = 1;

    use Report;

    my $x = 123;
    my $y = [ qw( one two)];
    my %z = (
    hihi => [ 456, 789],
    haha => { hoho => 'huhu'},
    );

    report qw( $x $y $z{haha});
     
    -berlin.de, Jul 29, 2007
    #2
    1. Advertising

  3. Bill H

    Sisyphus Guest

    "Bill H" <> wrote in message
    news:...
    > Is there a way in perl to dump all the variables used and their
    > current values to a file without printing everyone of them
    > individually?
    >


    Closest I could find is PadWalker (
    http://search.cpan.org/~robin/PadWalker-1.5/PadWalker.pm ).

    It handles lexical variables, but not globals (afaict) :

    --------------------------------
    use PadWalker;
    use warnings;

    my $s = 'hello world';
    my @array = (1, 3, 5);
    our $t = 'hello again';
    our @array2 = (5, 7, 9);

    $h1 = PadWalker::peek_my(0);
    %deref = %{$h1};

    print "\n\"my\" variables\n";

    for(keys(%deref)) {
    print "$_ : $deref{$_} \n";
    print "@{$deref{$_}} \n" if ref($deref{$_}) eq "ARRAY";
    print ${$deref{$_}}, "\n" if ref($deref{$_}) eq "SCALAR";
    }


    print "\n\"our\" variables\n";

    $h1 = PadWalker::peek_our(0);
    %deref = %{$h1};

    for(keys(%deref)) {
    print "$_ : $deref{$_} \n";
    print "@{$deref{$_}} \n" if ref($deref{$_}) eq "ARRAY";
    print ${$deref{$_}}, "\n" if ref($deref{$_}) eq "SCALAR";
    }
    --------------------------------

    Which outputs:

    --------------------------------
    "my" variables
    @array : ARRAY(0xb54914)
    1 3 5
    $s : SCALAR(0xb54944)
    hello world

    "our" variables
    $t : SCALAR(0x26aa184)
    hello again
    @array2 : ARRAY(0x2716884)
    5 7 9
    --------------------------------

    Faik, there may well be something better.

    Hope this helps.

    Cheers,
    Rob
     
    Sisyphus, Jul 29, 2007
    #3
  4. Bill H

    Bill H Guest

    On Jul 29, 10:29 am, -berlin.de wrote:
    > Bill H <> wrote in comp.lang.perl.misc:
    >
    > > Is there a way in perl to dump all the variables used and their
    > > current values to a file without printing everyone of them
    > > individually?

    >
    > Not easily. You'd have a chance with package variables. For
    > lexicals you'd need something like PadWalker, which is a bit
    > tricky to use.
    >
    > Most people settle for printing selected values at selected
    > places for debugging. The module Data::Dumper helps with
    > viewing complex data structures.
    >


    That was what I thought I would need to do, but since perl has so much
    in it I thought maybe it would do this too.

    Bill H
     
    Bill H, Jul 29, 2007
    #4
  5. Bill H

    Reto Guest

    I'm sure there is way. You could take a look at Data::Dumper.

    # -- example
    $d = Data::Dumper->new(@data);
    print $d->Dump;

    Additionally, I suggest to see this page which might help to solve
    your problem by another approach:

    http://www.infocopter.com/perl/monitored-variables.html

    --reto


    On 29 Jul., 14:08, Bill H <> wrote:
    > Is there a way in perl to dump all the variables used and their
    > current values to a file without printing everyone of them
    > individually?
    >
    > Bill H
     
    Reto, Aug 1, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. halfdog
    Replies:
    12
    Views:
    12,471
  2. mfglinux
    Replies:
    11
    Views:
    712
    Roberto Bonvallet
    Sep 12, 2007
  3. Brian Takita

    Variable value "dump"

    Brian Takita, Aug 26, 2005, in forum: Ruby
    Replies:
    3
    Views:
    135
    Dave Burt
    Aug 27, 2005
  4. Dan Smorey Jr.

    Variable Dump

    Dan Smorey Jr., Dec 27, 2005, in forum: Ruby
    Replies:
    2
    Views:
    105
    Lou Vanek
    Dec 27, 2005
  5. Ralph Shnelvar
    Replies:
    0
    Views:
    153
    Ralph Shnelvar
    Dec 25, 2009
Loading...

Share This Page