How can I control the formatting of the output of Data :: Dumper?

I am using the Data::Dumper::Dumper() method. The solution is good, but can be made compact and more attractive.

How can I control this? What are the best alternatives?

+6
perl dump data-dumper
source share
7 answers

Take a look at Data :: Dump for something similar to Data :: Dumper, but perhaps better for printing.

Edit (20120304) . I completely forgot this question, but today it was supported, and it clogged my memory. If I had to recommend something today (3 years later) for pretty typing in Perl, I would certainly go with Data :: Printer . From Data :: Printer own Rationale:

Data :: Dumper is a fantastic tool designed to reinforce data structures so that they are eval 'backwards.

The fact is that many people continue to use it (and similar ones, for example, Data Data Dump) for printing data structures and objects on the screen for checking and debugging, and when using these modules for this, it does not mean that you should .

Here is Data :: Printer. It is intended for only one thing and only one thing: display Perl variables and objects on the screen, correctly formatted (for human verification)

+20
source share

If you want to serialize the output for storage (and not for display), see Storable freeze() and thaw() . I compress whenever I see that Data: Dumper is used to save data structures in a database or cache .:(

+5
source share

I usually use Data :: Dump :: Streamer, but as others have said, only when the Data :: Dumper options are not enough.

+4
source share

One alternative to * to Data :: Dumper would be JSON and its implementation of Perl JSON .

* Is it better for you to decide.

+3
source share

If you are just looking for the output file: Smart::Comments .

You just use it.

 use Smart::Commments; 

And then you put any simple variable in a three-character comment, for example:

 my $v = black_box_process(); ### $v 

And he unloads it in almost the most beautiful press.

You can also manage more complex expressions as follows:

 ### ( $a && ( $b ^ ( $c || $d ))) : ( $a && ( $b ^ ( $c || $d ))) 

But you have to watch it on the "colon path".

 ### $My::Package::variable 

or

###% My :: Package ::

never worked in my experience. If I want them to work, I need something like this:

 my %stash = %My::Package::; ### %stash 

It also performs a number of other tricks that you can see by reading the documentation.

+2
source share

One option is to use Data :: Dumper :: Perltidy , which is (more or less) a replacement for Data :: Dumper :: Dumper (), but which uses Perltidy to format the output.

+2
source share

Data :: Dumper :: Concise is another feature.

 use Data::Dumper::Concise; warn Dumper($var); 

equivalent to:

 use Data::Dumper; { local $Data::Dumper::Terse = 1; local $Data::Dumper::Indent = 1; local $Data::Dumper::Useqq = 1; local $Data::Dumper::Deparse = 1; local $Data::Dumper::Quotekeys = 0; local $Data::Dumper::Sortkeys = 1; warn Dumper($var); } 
+2
source share

All Articles