On May 9, 2010, at 11:57 AM, John W. Krahn wrote:
> This *may* work -- UNTESTED:
>
> use Fcntl ':seek';
> my $filename = 'binary_file';
> open my $FH, '+<:raw', $filename or die "Cannot open '$filename' $!";
> read $FH, my $data, -s $FH or die "Cannot read from '$filename' $!";
> length $data == -s $FH or die "Could only read ", length $data, " bytes from
> a ", -s $FH, " byte file.\n";
> $data =~ s/(?<=SASI {6}.{20}).{20}(?=2009$schno)/$mybinary/s;
> seek $FH, 0, SEEK_SET or die "Cannot seek on '$filename' $!";
> print $FH $data;
And it does! But doing a bit more dabbling, I tried replacing your last three
lines with:
$data =~ m/(?<=SASI {6}.{20}).{20}(?=2009$schno)/s; # change from s/// to m//
seek $FH, $-[0], SEEK_SET or die "Cannot seek on '$filename' $!"; # seek to
offset of match
print $FH $mybinary; # just replace 20 bytes
... which also works, and avoids potentially re-writing a large amount of data.
Any reason I might not want to do it this way?
(It occurs to me that if the file is really that large, I might have a more
serious problem reading it all into memory in the first place.)
Thanks!
Chap
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/