How to copy binary files in Perl program

I have the below Perl code to make a copy of a binary file that I have.

$in = "test_file_binary.exe";
$out = "test_out_binary.exe";
open(IN,$in) || die "error opening ip file: $!" ;
open(OUT,">$out") || die "error opening op file: $!" ;
 print OUT $_;

But this version of code, the output binary file is of more size than the input binary file size, because this perl code seems to add a 0x0D (Carriage return) character before a 0x0A (newline) character in the input file, it its not already there.

If I use chomp , then it is removed even valid 0x0A characters present, and did not put them in the output file.

1] How can I fix this in the code above.

2] How can I solve this using the File::Copy module, any example code snip would be useful.

thank you.



Always use three-arg open.

open IN, '<:raw', $in or die "Couldn't open <$in: $!";
open OUT, '>:raw', $out or die "Couldn't open >$out: $!";

my ($len, $data);
while ($len = sysread IN, my $data, 4096) {
    syswrite OUT, $data, $len;
defined $len or die "Failed reading IN: $!"

However, File::Copy is so easy to use I don't understand why you wouldn't.

use File::Copy;

copy($in, $out) or die "Copy failed: $!";

Need Your Help

Error Loading Simple XML

php xml simplexml

I have an xml document (generated from msword 2010), which I am trying to process using simple xml.

how to use cin.ignore to ignore specified character in C++

c++ keyboard

I want to use cin.ignore() to get my data from keyboard.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.