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

I'm using Data::Dumper to store a structure on disk ( which then I can read back and eval ). Its output is nicely tabulated. However it turns out that it is tabulated with spaces instead of tabs, so it outputs a ridiculously large namber of tabulating spaces which begin to matter if I have a large data structure. Is there a way to make Data::Dumper generate a minimalistic output ( which need not be human readable )?
'rnbqkbnr/pp1pp1pp/2p2p2/1N6/8/5N2/PPPPPP +PP/R1BQKB1R b KQkq -' => { + 'd8a5' => { + 'orig_eval' => 240, + 'visited' => 1, + 'fen_after' => 'rnb1kbnr/pp1pp +1pp/2p2p2/qN6/8/5N2/PPPPPPPP/R1BQKB1R w KQkq -', + 'eval' => 240 + }, + 'c6c5' => { + 'orig_eval' => 1276, + 'visited' => 1, + 'fen_after' => 'rnbqkbnr/pp1pp +1pp/5p2/1Np5/8/5N2/PPPPPPPP/R1BQKB1R w KQkq -', + 'eval' => 1276 + }, + 'g7g6' => { + 'orig_eval' => 1234, + 'visited' => 1, + 'fen_after' => 'rnbqkbnr/pp1pp +2p/2p2pp1/1N6/8/5N2/PPPPPPPP/R1BQKB1R w KQkq -', + 'eval' => 1234 + },

Replies are listed 'Best First'.
Re: Is there a way to reduce Data::Dumper's output size?
by cursion (Pilgrim) on Feb 10, 2015 at 21:08 UTC
    perldoc Data::Dumper
    reveals a couple of things, Indent and Freeze. Indent can reduce the spaces and keep it human readable, while Freeze can do anything you want. Or just gzip the output files.
Re: Is there a way to reduce Data::Dumper's output size?
by GotToBTru (Prior) on Feb 10, 2015 at 21:02 UTC

    Have you looked at the documentation?

    Dum Spiro Spero
Re: Is there a way to reduce Data::Dumper's output size?
by Laurent_R (Canon) on Feb 10, 2015 at 21:23 UTC
    You could reprocess the dumper output before storing it by replacing any sequence of one (or two) or more spaces by a single space:
    $dumper_output =~ s/ +/ /g;
    The result will not be pretty, but you'll save quite a bit of space.

    Or you could use a module more dedicated to what you want to do than Data::Dumper, such as, for example Storable, which produces a more compact output.

    Update: crossed out my first suggestion because, as pointed out by Anonymous Monk below, it might mess things up is either keys or values of the hash have multiple spaces.

    Je suis Charlie.
      $dumper_output =~ s/ +/ /g;

      Probably not a good idea...

      use Data::Dumper; my $dumper_output = Dumper({"foo bar"=>"quz\n baz"}); print $dumper_output; $dumper_output =~ s/ +/ /g; print $dumper_output; __END__ $VAR1 = { 'foo bar' => 'quz baz' }; $VAR1 = { 'foo bar' => 'quz baz' };
        Ooops, yes, you are quite right, I did not think of the cases where keys or values might have multiple spaces. This would usually not happen where I work (because we usually normalize keys and values), but, yes, there is a danger of messing things up. Thank you for for the comment.

        Je suis Charlie.
      I'm trying Storable. Especially because the doc says: 'The heart of Storable is written in C for decent speed. Extra low-level optimization have been made when manipulating perl internals, to sacrifice encapsulation for the benefit of a greater speed.' My data gets so big that speed matters.
        You really should try Sereal.
Re: Is there a way to reduce Data::Dumper's output size?
by ikegami (Patriarch) on Feb 11, 2015 at 20:05 UTC

    Looks like you want JSON. Or anything else. I wouldn't use DD for serializing.

    But you could use

    local $Data::Dumper::Indent = 0; local $Data::Dumper::Terse = 1;

    Don't use Terse you can you don't have two references to the same variable.

Re: Is there a way to reduce Data::Dumper's output size?
by locked_user sundialsvc4 (Abbot) on Feb 11, 2015 at 00:31 UTC

    May I cordially suggest that you, instead, use one of several alternatives which are expressly designed to store data structures:

    • freeze/thaw ... not my favorite anymore.
    • XML
    • JSON
    • YAML

    You will find, in CPAN, multiple robust implementations, ready to go.

      I also tried the JSON module. Its output is almost the same size that of freeze ( only marginally bigger ). Its advantage that it is pure text, still somewhat human readable. I will see how fast it is when my structure gets really big.
        Give a try to Sereal.
        L*
        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Currently I'm trying freeze/thaw. Why did you abandon using it? Is it unsafe?