in reply to Unexpected File Results

Testing on non-CygWin (Activestate Win32), trying to replicate the possible test/binary condition. This (highly refactored) code, when reading and writing binary (raw), works perfectly. Changing the output mode back to Text reproduces the behaviour described in the OP.

use strict; use warnings; my $self = { data => 'PM_618194_in.dat', process => 'patient' }; my $config = { blocksize => 550, startblock => 3 }; my $max_chunk_size = 1_000_000; my $chunk_size = int( $max_chunk_size / $config->{blocksize} ) * $config->{blocksize} ; open my $parent_fh, '<:raw', $self->{data} or die "Cannot open '$self->{data}' for incremental parsing: $!"; my $bytes_read = read $parent_fh, my $junk, $config->{startblock}; if ( $bytes_read != $config->{startblock} ) { warn "Tried to read $config->{startblock} bytes, ". "but got $bytes_read bytes!\n"; } $/ = \$chunk_size; # Set <> for fixed blocksize reads. my $file_num; while ( <$parent_fh> ) { $file_num++; my $filename = sprintf 'tmp/%s%02d.dat', $self->{process}, $file_num; open my $out_fh, '>:raw', $filename # open my $out_fh, '>', $filename or die "Cannot open '$filename' for incremental writing: $!"; print $out_fh $_ or warn; close $out_fh or warn; } close $parent_fh or warn;
BTW, you can save yourself a good bit of code by factoring out the {startblock} read, and using $/, as I have above. Also, when your files are binary, it is *always* most correct to use binary mode, even when your OS (like Unix) does not care.