You are given either an IO::File object or a typeglob (\*STDOUT or Symbol::symbol_to_ref('main::FH')); how would you go about determining if it is a read or write handle? The interface cannot be extended to pass this information (I am overriding close to add calls to flush and sync before the actual close).
Currently I am attempting to flush and sync the filehandle and ignoring the error 'Invalid argument' (which is what I get when I attempt to flush or sync a read filehandle):
eval { $fh->flush; 1 } or do { #this seems to exclude flushes on read handles unless ($! =~ /Invalid argument/) { croak 'could not flush $fh: $!'; } }; eval { $fh->sync; 1 } or do { #this seems to exclude syncs on read handles unless ($! =~ /Invalid argument/) { croak 'could not sync $fh: $!'; } };
Have a look at the fcntl options. Maybe
F_GETFLwithO_ACCMODE.Edit: I did a little googling and playing over lunch and here is some probably non-portable code but it works for my Linux box, and probably any Posix system (perhaps even Cygwin, who knows?).
Example output: