Perl Tutorials - Herong's Tutorial Examples - v6.02, by Herong Yang
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
Data Types: Values and Variables
Expressions, Operations and Simple Statements
Name Spaces and Perl Module Files
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
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
Socket Communication Over the Internet
XML::Simple Module - XML Parser and Generator
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