Subject: | broken seek implementation, patch included |
IO::Scalar functionality differs from basic file IO with respect to the seek() method. Bug can be reproduced with
use IO::Scalar;
my $fh;
open($fh, '>/var/tmp/test.dat');
&test($fh);
close($fh);
$fh = new IO::Scalar;
&test($fh);
sub test {
my $fh = shift;
$fh->print('test'); print(tell($fh));
seek($fh, 0,0); print(tell($fh));
$fh->print('test'); print(tell($fh) . "\n");
}
It will print
404
408
Patch attached fixes this, however it causes unit tests to break.
421c421,442
< *$self->{Pos} = length(${*$self->{SR}} .= join('', @_) . (defined($\) ? $\ : ""));
---
>
> my $curpos = *$self->{Pos};
> my $data = join('', @_) . (defined($\) ? $\ : "");
> my $datlen = length($data);
> my $curlen = length(${*$self->{SR}});
>
> if ($curpos == $curlen) {
> ${*$self->{SR}} .= $data;
> *$self->{Pos} = $curpos + $datlen;
> return(1);
> }
>
> my $sum = $curpos + $datlen;
> if ($sum > $curlen) {
> ${*$self->{SR}} = substr(${*$self->{SR}}, 0, $curpos) . $data;
> *$self->{Pos} = $sum;
> }
> else {
> substr(${*$self->{SR}}, $curpos, $datlen) = $data;
> *$self->{Pos} = $sum;
> }
>