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

Hi, I have a large data structure and I want to dump it to a file.
use Data::Dumper; print FILE Dumper $data
works OK but it creates the dump in the memory, which I'd like to avoid. Is there a way to direct the dump right into the file?

Replies are listed 'Best First'.
Re: Stream Dumper
by Arunbear (Prior) on May 23, 2007 at 14:51 UTC
    Yes, you can use Data::Dump::Streamer: e.g.
    use Data::Dump::Streamer; use Fatal qw/open/; open my $fh, '>', 'somefile'; my $x = {cat => [qw/man du/]}; Dump($x)->To($fh)->Out();
    somefile then contains:
    $HASH1 = { cat => [ 'man', 'du' ] };
    Updated example to include intialisation of $x and output.
Re: Stream Dumper
by tilly (Archbishop) on May 23, 2007 at 14:51 UTC
    Use Data::Dump::Streamer. Glancing at the module, the code likely looks something like: Dump(@data)->To($filehandle)->Out();

    If that doesn't work, then play with it and ask demerphq if you really can't figure it out.

Re: Stream Dumper
by blazar (Canon) on May 23, 2007 at 14:35 UTC
    works OK but it creates the dump in the memory, which I'd like to avoid. Is there a way to direct the dump right into the file?

    Interesting question. I understand what you mean. I don't know if there's a ready made solution, but if not, then I suppose that D::D builds a string incrementally: so you may examine its code to make it incrementally write to a given file instead.

      IMO this will be quite nontrivial to do.

      ---
      $world=~s/war/peace/g

        IMO this will be quite nontrivial to do.

        Appearently the good news are that D::D::S already did that. One more module to bookmark!

    A reply falls below the community's threshold of quality. You may see it by logging in.