How can I make Data::Dumper
write a dump into a file?
Asked
Active
Viewed 2.6k times
9

Mariusz Jamro
- 30,615
- 24
- 120
- 162

joe
- 34,529
- 29
- 100
- 137
-
5`Dumper()` simply outputs a string, use it like any other string going to a file (see below). – Jack M. Jul 14 '09 at 15:25
-
i have huge dump value , so i need to keep that into file and having look . – joe Jul 14 '09 at 15:27
-
@JACK I agree with your point – joe Jul 14 '09 at 15:29
3 Answers
26
Don't forget that you can specify the file handle to print
to as in
print $LOG Dumper( \%some_complex_hash );
or use File::Slurp:
write_file 'mydump.log', Dumper( \%some_complex_hash );
Further thoughts: You might want to get into the habit of using:
warn Dumper( \%some_complex_hash );
and redirecting standard error to a file when you invoke your script (how you do this depends on the shell). For example:
C:\Temp> sdf.pl 2>dump

Sinan Ünür
- 116,958
- 15
- 196
- 339
-
-
1If all I want is to dump a complex data structure to a file for debugging purposes, it is more self contained than open / print / close: no filehandles or error messages to mess with. – Sinan Ünür Jul 14 '09 at 16:40
-
@SinanÜnür how is $LOG supposed to be used. should $LOG point to a file open ($LOG, '>', 'file.txt'); – patz Sep 02 '16 at 20:53
-
8
The question is a bit unclear, but are you looking for something like this?
open my $FH, '>', 'outfile';
print $FH Dumper(\%data);
close $FH;
You can restore the data later by using eval
.

Sinan Ünür
- 116,958
- 15
- 196
- 339

Michiel Buddingh
- 5,783
- 1
- 21
- 32
-
6For storing and restoring later, Storable is a much better idea than Data::Dumper + eval: http://search.cpan.org/perldoc?Storable – Telemachus Jul 14 '09 at 16:37
-
1Telemachus is correct. Storable, YAML, JSON, DBM::Deep or any of one million other serialization modules is a better choice than Data::Dumper + eval. – daotoad Jul 14 '09 at 17:01