Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Quick Dumper

by liz (Monsignor)
on Dec 03, 2003 at 12:19 UTC ( #311879=snippet: print w/replies, xml ) Need Help??
Description: I use this code a lot when I want a quick and easy removable way to look at the contents of objects of classes I'm developing, and to be able to look at other data-structures as well.

Just add the code below to your main program, and you can do the following:

my $foo = new Foo; $foo->dump; # dumps automatically to STDERR print $foo->dump; #dumps to STDOUT print FOO $foo->dump; # dumps to filehandle FOO
or, if you don't have objects but want to quickly dump a data-structure:
a->dump( $foo ); # dumps automatically to STDERR print a->dump( $foo ); #dumps to STDOUT print FOO a->dump( $foo ); # dumps to filehandle FOO

I wonder whether it would make sense to make a "dump" pragma for this and upload to CPAN.

sub UNIVERSAL::dump {

# Obtain the object / class
# Make sure that we have Data::Dumper
# Return Dumper output if not in void context
# Send Dumper output to STDERR otherwise

    my $self = shift;
    require Data::Dumper;
    return Data::Dumper::Dumper( @_ ? @_ : $self ) if defined wantarra
    print STDERR Data::Dumper::Dumper( @_ ? @_ : $self );
} #UNIVERSAL::dump
Replies are listed 'Best First'.
Re: Quick Dumper
by zentara (Archbishop) on Dec 03, 2003 at 15:44 UTC
    Just for the benefit of monks who normally don't check out all the new arrivals on cpan.....this reminds me of a new cool module which does something similar to this, it's callled Tk::Objscanner It is sort of reminiscent of ptkdb, but it will pop a Tk window with the dump of any object. It has a useful companion module Tk::ObjEditor.
    # user code to produce data Tk::ObjScanner::scan_object($mydata) ; # resume user code
Re: Quick Dumper
by diotalevi (Canon) on Dec 03, 2003 at 13:09 UTC
    Sure, as long as you don't call it a pragma. I'd expect some sort of scoping then and your code is all about being global.
      Indeed. It's intended as a debugging tool. Ease of use is the only goal! Something that you would add as:
      perl -Mdump yourprogram
      and which would then allow you to call the "dump" method on your objects without any other changes to your program.


        What, just like that? I'm not sure how that would work since you'd have to have built in the ->dump method calls beforehand. Or do you include things like $obj->dump if $obj->can( 'dump' ) so it works in both cases?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://311879]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2016-10-25 23:48 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (330 votes). Check out past polls.