Coder's Guild Mailing List

writing binary files in perl

Posted by Frank Hale on 1999-05-03

Okay I have the following perl script which I want to write the data in
binary to a file.

binmode has no effect on Unix systems which don't distinguish from
binary and text files. I am on RedHat Linux 5.2. 

#!/usr/bin/perl

$a = 0x01;
$b = 0x02;
$c = 0x03;

open (BIN, ">bin.dat");
print BIN $a,$b,$c;
close (BIN);

This produces an ordinary text file with the numbers 1,2,3. I want it to
produce a binary file with the binary equivilents of 1,2,3. 

I want a binary file just like an executable file is binary. Hopefully
this will make more sense. You shouldn't be able to look at it in a text
editor and see 123. It should look like a bunch of junk just like if you
looked at an executable in a text editor.

Incase you want some ammusement go to comp.lang.perl.misc and read all
the smart ass emails people have written about my question. 

I even got some smart ass emails from Tom Christansen on this subject.
He is co-author of The Perl Cookbook and Programming Perl. Its truly a
small world!!

-- 
From:      Frank Hale
Email:     frankhale@xxxxxxxx.xxx.xxx
ICQ:       7205161
Website:   http://www.franksstuff.com (DOWN FOR A WHILE)