Copy.pl - Copying Binary Files

This section provides a tutorial example of copying a file in binary mode using the binmode() function on input and output.

As a simple example program of input and output data in binary mode, I wrote Copy.pl to copy binary files:

#- Copy.pl
#- Copyright (c) HerongYang.com. All Rights Reserved.
#
   ($in, $out) = @ARGV;
   die "Missing input file name.\n" unless $in;
   die "Missing output file name.\n" unless $out;
   $byteCount = 0;
   open(IN, "< $in");
   binmode(IN);
   open(OUT, "> $out");
   binmode(OUT);
   while (read(IN,$b,1)) {
      $byteCount++;
      print(OUT $b);
   }
   close(IN);
   close(OUT);
   print "Number of bytes copied = $byteCount\n";
   exit;

To test this program, I used the following commands:

herong> copy c:\windows\system32\find.exe find.exe

herong> copy.pl find.exe find_copy.exe

Number of bytes copied = 21504

herong> find_copy.exe /?

Searches for a text string in a file or files.
FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" ...
...

On an old Windows system, I used the following commands:

herong> copy c:\winnt\system32\mem.exe mem.exe

herong> copy.pl mem.exe mem_copy.exe

Number of bytes copied = 39386

herong> mem_copy
    655360 bytes total conventional memory
    655360 bytes available to MS-DOS
    633776 largest executable program size

   1048576 bytes total contiguous extended memory
         0 bytes available contiguous extended memory
    941056 bytes available XMS memory
           MS-DOS resident in High Memory Area

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

 File Handles and Data Input/Output

Open Files in Binary Mode

 binmode() - Opening Files for Binary Input

 binmode() - Opening Files for Binary Output

Copy.pl - Copying Binary Files

 Bin2Hex.pl - Converting Binary Data to Hex Numbers

 Open Directories and Read File Names

 File System Functions and Operations

 Image and Picture Processing

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server

 CGI.pm Module for Building Web Pages

 LWP::UserAgent and Web Site Testing

 Converting Perl Script to Executable Binary

 Managing Perl Engine and Modules on macOS

 Archived Tutorials

 References

 Full Version in PDF/EPUB