in reply to Problem with Pack
You fell prey to the idiosyncrasies of C and DOS, who together make a difference between text files and binary files.
Each file is by default in text mode, which means that every byte aequivalent to \n is translated into the byte sequence 0d 0a by Perl or by the underlying OS.
The solution is to always use binmode() on files that you want to handle as binary files.
So, use the following idiom for files that you want Perl to handle as binary files. It dosen't make any difference on systems that don't distinguish text and binary files :
use strict; # Always use strict local *FILE; # This declares FILE as a local variable. # This is NOT necessary for variables with # UPPERCASE names, as strict # ignores these, see [jcwren]s post # below for an excellent explanation. # But localizing a file variable prevents # us from messing up other variables of # the same name. open FILE, "> $filename" or die "Couldn't create '$filename': $!\n"; binmode FILE; # do your stuff close FILE;
Update : In the code above, there was the following statement, which is not true (I didn't know that) :
I've changed my main CODE block to reflect what actually happens and why I think that using local is still a good idea :-). Thanks to jcwren, who pointed this out to me (in the below post).local *FILE; # With strict, we need to predeclare *FILE # (not necessary for builtin file handles such + as STDIN)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(jcwren) Re: Problem with Pack
by jcwren (Prior) on Sep 22, 2000 at 17:54 UTC | |
by tilly (Archbishop) on Sep 23, 2000 at 13:08 UTC |