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

I have a rather strange problem. I would like to open a binary file, save it into a variable, and finally save that data to disk:
open(FH,"file.exe"); binmode(FH); my $binary; while(<FH>) { $binary .= $_; } close(FH); open(FH,">newfile"); print FH $binary; close(FH);
I should then be able to pull the binary data from that file, or execute it if it was an executable. However, the above code segment produces a file that is much larger than the original.

Replies are listed 'Best First'.
Re: Binary Data
by JamesNC (Chaplain) on Jun 12, 2003 at 02:30 UTC
    Simple Solution: You forgot to use binmode again is all :)
    open(FH,"file.exe"); binmode(FH); my $binary; while(<FH>) { $binary .= $_; } close(FH); open(FH,">newfile"); binmode FH; #<---- Add this and it works just fine :) print FH $binary; close(FH);
    Cheers, James
Re: Binary Data
by djantzen (Priest) on Jun 12, 2003 at 01:56 UTC
      I'm not looking for copy. In some cases, the $binary variable will be manipulated, but I will not know until it is read in.