Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Data::Dumper question

by perl5ever (Pilgrim)
on Apr 01, 2009 at 03:39 UTC ( [id://754609]=perlquestion: print w/replies, xml ) Need Help??

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

With Data::Dumper there doesn't seem to be a way to configure a Dumper object and then use it dump multiple objects. For instance, I'd like to use it this way:
use Data::Dumper; my $d = new Data::Dumper(); $d->Terse(0)->Purity(1)->Deepcopy(1)->Indent(0); ... print $d->Dump($this); ... print $d->Dump($that); ... print $d->Dump($otherthing); ...
It seems that the thing to dump is part of the constructor, so you are forced to create a new Dumper object (and configure it) every time you want to dump a different object.

Or am I missing something?

Replies are listed 'Best First'.
Re: Data::Dumper question
by Anonymous Monk on Apr 01, 2009 at 03:59 UTC
    No, Data::Dumper is clever like that :)
    sub Dump { require Data::Dumper; local $Data::Dumper::Terse=0; local $Data::Dumper::Purity=1; local $Data::Dumper::Deepcopy=1; local $Data::Dumper::Indent=0; Data::Dumper::Dumper(@_); } print Dump( $this); print Dump( $that); print Dump( $otherthing);
Re: Data::Dumper question
by haoess (Curate) on Apr 01, 2009 at 09:54 UTC
    RTFM?
    $OBJ->Values([ARRAYREF])

    Queries or replaces the internal array of values that will be dumped. When called without arguments, returns the values. Otherwise, returns the object itself.

    Update (example code):

    print $d->Values([$this])->Dump(); print $d->Values([$that])->Dump(); print $d->Values([$otherthing])->Dump();

    -- Frank

      I guess I did overlook that. Thanks! For convenience it would be nice, though, if that capability was integrated into the Dump method.
      I guess I did overlook that. Thanks!
      Here's another problem -- when you set the thing to be dumped via Values(), the Dumper object retains a reference to the thing to be dumped.

      It would be a lot nicer if it would just hold on to the dumpee only for the duration of the Dump call.

Re: Data::Dumper question
by Bloodnok (Vicar) on Apr 01, 2009 at 10:02 UTC
    I have to admit to, even in OO code, never using the OO interface to Data::Dumper - I use something along the lines of...
    use Data::Dumper; . . print Dumper $some_ref; # Dump single structure . . # Dump multi structures - providing non-default names for each print Data::Dumper->Dump [$ref1, $ref2],[qw/ref1 ref2/];
    A user level that continues to overstate my experience :-))
Re: Data::Dumper question
by ikegami (Patriarch) on Apr 01, 2009 at 07:24 UTC
    It does indeed have a weird interface.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://754609]
Approved by wfsp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-19 02:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found