open my $fh, '<+:raw', $file; # raw so file is read/written in bytes, not characters my $where = tell($fh); while (<$fh>) { ...; # modify 1 or more bytes seek $fh, $where, SEEK_SET; print $fh, $_; $where = tell($fh); } close $fh;