in reply to How do i determine which mode the file has open ? (write or append mode)

Not long ago I uploaded FileHandle::Fmode to CPAN. I'm not actually sure that it has been used much (if at all) but you might find it useful.

It doesn't distinguish between "write mode" and "append mode" (which, when I read your post more closely, is what you've posted about). Ottomh, I don't know how you can distinguish between a "clobbered/writable" filehandle and an "unclobbered/appendable" filehandle with pre-5.6 perl. But with perl 5.6 and later the distinction can be readily facilitated and I'll post a new version of FileHandle::Fmode that makes that distinction (for perl 5.6 and later only)tonight. In the meantime, if you have Inline::C, you might be able to make use of the following code (which is probably a little light-on wrt error checking).
use warnings; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'EOC'; SV * my_fmode(SV * handle) { int x; IO *io; io = sv_2io(handle); x = IoTYPE(io); if (x == IoTYPE_RDONLY) return newSVuv(1); if (x == IoTYPE_WRONLY) return newSVuv(2); if (x == IoTYPE_RDWR) return newSVuv(3); if (x == IoTYPE_APPEND) return newSVuv(4); warn("Unable to determine mode\n"); return 0; } EOC open($fh1, '>', 'test1.txt') or die "Can't open \$fh1: $!"; open($fh2, '>>', 'test2.txt') or die "Can't open \$fh2: $!"; print my_fmode($fh1), " ", my_fmode($fh2), "\n"; close($fh1) or die "Can't close \$fh1: $!"; close($fh2) or die "Can't close \$fh2: $!";


Cheers,
Rob
  • Comment on Re: How do i determine which mode the file has open ? (write or append mode)
  • Download Code