On Sat, 2 Nov 2002 16:00:44 -0600, [EMAIL PROTECTED] (John Pitchko) wrote: >Hi all. > >I've been racking my brains out trying to get Perl to write binary files for me. Here >is my situation. For my > >I was thinking that I would record the output from Data::Dumper into a scalar and >write this scalar in binary mode to the disk. Can anyone give me any help with >this????
Here are 3 simple binary file examples. The first one writes a binary file, the second one reads it. The third is an example of using Storable to save a hash. #write #####################################################3 #!/usr/bin/perl open(ZZ,"+>zz") or die "Can't open zz: $!"; binmode ZZ; for(0..19){ $array[$_]= 2 * $_; print "$array[$_] "; $array1[$_]= pack ("L*",$array[$_]); print ZZ $array1[$_]; } close ZZ; exit 0; ####################################################### #read ################################################## #!/usr/bin/perl use strict; use warnings; $/='undef'; open (ZZ, "<zz") or die $!; binmode ZZ; my $file=(<ZZ>); my @nums = (unpack "L*",$file); print "@nums\n"; close ZZ; #################################################### #Storable ##################################################### #!/usr/bin/perl use Storable qw(store retrieve freeze thaw dclone); %color = ('Blue' => 0.1, 'Red' => 0.8, 'Black' => 0, 'White' => 1); store(\%color, 'Storedcolors') or die "Can't store %a in Storedcolors!\n"; $colref = retrieve('Storedcolors'); die "Unable to retrieve from Storedcolors!\n" unless defined $colref; printf "Blue is still %lf\n", $colref->{'Blue'}; $colref2 = dclone(\%color); $str = freeze(\%color); printf "Serialization of %%color is %d bytes long.\n", length($str); $colref3 = thaw($str); ########################################################### -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]