in reply to binmode i/o for perl -pi in-place editing
Update: edited out beginning line info (byte offset, etc) from the debug output in the hopes that the lines will display better on Perl Monks. "debug" is a standard Windows command from the Command Line.#!usr/bin/perl use strict; use warnings; open (IN, '<','src.bin') or die $!; binmode IN; open (OUT, '>','srcnew.bin') or die $!; binmode OUT; my $inbuf = do {local $/; <IN> }; $inbuf =~ s/w/v/; print OUT $inbuf; __END__ C:\PROJEC~1\testing>debug src.bin -d 62 69 6E 0A 00 65 6E 63-6F 64 65 64 00 66 69 6C bin..encoded.fil 65 00 72 3A 0D 00 6E 3A-0A 00 6E 72 3A 0A 0D 00 e.r:..n:..nr:... 72 6E 3A 0D 0A 00 77 69-74 68 00 45 4F 4C 2D 6C rn:...with.EOL-l 69 6B 65 00 73 65 71 75-65 6E 63 65 73 00 00 00 ike.sequences... 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................ -q C:\PROJEC~1\testing>debug srcnew.bin -d 62 69 6E 0A 00 65 6E 63-6F 64 65 64 00 66 69 6C bin..encoded.fil 65 00 72 3A 0D 00 6E 3A-0A 00 6E 72 3A 0A 0D 00 e.r:..n:..nr:... 72 6E 3A 0D 0A 00 76 69-74 68 00 45 4F 4C 2D 6C rn:...vith.EOL-l 69 6B 65 00 73 65 71 75-65 6E 63 65 73 00 00 00 ike.sequences... 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................ -q
I will also add as a comment that although it is possible to preserve the line endings in a file, I almost always find that this is not desirable. You can wind up with mixed line endings that Perl can deal with, but other programs cannot. Perl will write a line ending appropriate for the platform being used for that "write". So: read the line, print the line is often the best way. That "normalizes" the line endings.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: binmode i/o for perl -pi in-place editing
by pryrt (Abbot) on Dec 09, 2016 at 15:17 UTC | |
by shmem (Chancellor) on Dec 09, 2016 at 16:38 UTC |