S
Sisyphus
Hi,
If I try to read from a filehandle opened for appending, I get no warning
about the fact that the filehandle is not readable. Why is that ?
Here's the demo script:
use warnings;
# Create a test file:
open(WR, '>', 'temp.txt') or die "Can't open for writing: $!";
print WR "line 1\nline2\n\nline4\nline5\n";
close(WR) or die "Can't close after writing: $!";
# Open for appending, then try to read:
open($fh, '>>', 'temp.txt') or die "Can't open: $!";
seek($fh, 0, 0);
@lines = <$fh>;
close($fh) or die "Can't close: $!";
print for @lines;
__END__
@lines is empty as it should be, but no warning gets issued.
If I replace the '>>' with a '+>>', then @lines is filled with the contents
of the file.
Even at the XS level, we are unable to tell if a filehandle opened for
appending is readable or not:
use warnings;
use Inline C => Config =>
BUILD_NOISY => 1;
use Inline C => <<'EOC';
void my_fmode(SV * handle) {
int x;
IO *io;
io = sv_2io(handle);
x = IoTYPE(io);
if (x == IoTYPE_RDONLY) printf ("read only\n");
if (x == IoTYPE_WRONLY) printf ("write only\n");
if (x == IoTYPE_RDWR) printf ("read_write\n");
if (x == IoTYPE_APPEND) printf ("append\n");
}
EOC
open($fh1, '+>>', 'temp.txt')
or die "Can't open fh1 for reading and appending: $!";
open($fh2, '>>', 'temp.txt')
or die "Can't open fh2 for appending: $!";
my_fmode($fh1);
my_fmode($fh2);
close($fh1) or die "Can't close fh1: $!";
close($fh2) or die "Can't close fh2: $!";
__END__
For both filehandles, x is set to the same value (ie to IoTYPE_APPEND) so we
have no chance of determining whether it's a '>>' filehandle or a '+>>'
filehandle.
Are these oversights, or are there sound reasons for these behaviours ?
I don't really know why anyone would open a file for appending only and then
try to read from it .... but I would have thought that such an action should
be detectable/reportable in some way.
Cheers,
Rob
If I try to read from a filehandle opened for appending, I get no warning
about the fact that the filehandle is not readable. Why is that ?
Here's the demo script:
use warnings;
# Create a test file:
open(WR, '>', 'temp.txt') or die "Can't open for writing: $!";
print WR "line 1\nline2\n\nline4\nline5\n";
close(WR) or die "Can't close after writing: $!";
# Open for appending, then try to read:
open($fh, '>>', 'temp.txt') or die "Can't open: $!";
seek($fh, 0, 0);
@lines = <$fh>;
close($fh) or die "Can't close: $!";
print for @lines;
__END__
@lines is empty as it should be, but no warning gets issued.
If I replace the '>>' with a '+>>', then @lines is filled with the contents
of the file.
Even at the XS level, we are unable to tell if a filehandle opened for
appending is readable or not:
use warnings;
use Inline C => Config =>
BUILD_NOISY => 1;
use Inline C => <<'EOC';
void my_fmode(SV * handle) {
int x;
IO *io;
io = sv_2io(handle);
x = IoTYPE(io);
if (x == IoTYPE_RDONLY) printf ("read only\n");
if (x == IoTYPE_WRONLY) printf ("write only\n");
if (x == IoTYPE_RDWR) printf ("read_write\n");
if (x == IoTYPE_APPEND) printf ("append\n");
}
EOC
open($fh1, '+>>', 'temp.txt')
or die "Can't open fh1 for reading and appending: $!";
open($fh2, '>>', 'temp.txt')
or die "Can't open fh2 for appending: $!";
my_fmode($fh1);
my_fmode($fh2);
close($fh1) or die "Can't close fh1: $!";
close($fh2) or die "Can't close fh2: $!";
__END__
For both filehandles, x is set to the same value (ie to IoTYPE_APPEND) so we
have no chance of determining whether it's a '>>' filehandle or a '+>>'
filehandle.
Are these oversights, or are there sound reasons for these behaviours ?
I don't really know why anyone would open a file for appending only and then
try to read from it .... but I would have thought that such an action should
be detectable/reportable in some way.
Cheers,
Rob