loga has asked for the wisdom of the Perl Monks concerning the following question:

I just started working with perl. how do i determine the file mode? whether it has open in write mode or append mode etc.., thanks, loga..
  • Comment on How do i determine which mode the file has open ? (write or append mode)

Replies are listed 'Best First'.
Re: How do i determine which mode the file has open ? (write or append mode)
by salva (Canon) on Apr 21, 2006 at 09:28 UTC
    with fnctl:
    use Fcntl qw(F_GETFL O_APPEND); sub is_append { my $fh = shift; my $status = fcntl($fh, F_GETFL, 0); return $status & O_APPEND; }
Re: How do i determine which mode the file has open ? (write or append mode)
by ashokpj (Hermit) on Apr 21, 2006 at 09:48 UTC
    use FileHandle::Fmode qw(:all); #$fh and FH are open filehandles print is_R($fh), "\n"; #it return true if file open in read mode print is_W(\*FH), "\n";#it return true if file open in write mode
Re: How do i determine which mode the file has open ? (write or append mode)
by syphilis (Archbishop) on Apr 21, 2006 at 09:59 UTC
    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
Re: How do i determine which mode the file has open ? (write or append mode)
by blazar (Canon) on Apr 21, 2006 at 09:47 UTC

    While this is actually an interesting question, and I appreciate very much salva's answer, I would say that it's not something that would pop up frequently. Indeed in many years I've never felt the need to find out the open mode of a filehandle: generally you're the person who opens it in the first place and thus you do know how to deal with it. Since you claim to be a newbie, I'd be curious as to know what you need this for. After all this may be an XY Problem...

      Actually I can think of quite a few use cases for this: in an abstract sense you aren't always the person who opened the file in the first place such as when you are passing a filehandle to a subroutine or method, particularly if the subroutine or method is written by a third party. I'm actually going to have some code that does this in the next release of Term::ReadKey to catch some 'bugs' that seem to crop up on windows.

      /J\

        Indeed. Though, even if I pass the handle to a sub or method, end even if they're written by someone else, in most cases I do know what they will do with it, i.e. whether they will read from it or write into it. I can imagine situations in which I won't know and I will desire to find out the open mode of the handle, but I would regard them as atypical, especially for a newbie: this is why I asked...