Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: How do I tell if a handle is open for writing?

by Zaxo (Archbishop)
on Nov 07, 2005 at 16:31 UTC ( #506415=note: print w/ replies, xml ) Need Help??


in reply to How do I tell if a handle is open for writing?

stat or, more conveniently, the -w file test operator. Both work fine on open handles, though we're mostly used to feeding them file names.

Update: Oops, got the wrong end of the stick. Here's how to check the open flags to see if the handle is currently writable:

use Fcntl; sub writable { my $fh = shift; (O_WRONLY | O_RDWR) & fcntl( $fh, F_GETFL, my $slush); } for (qw/ > < >> +< +> /) { open my $fh, $_, 'foo' or warn $! and next; print "$_\t", writable($fh)? 'writable.': 'not writable.', $/; } __END__ > writable. < not writable. >> writable. +< writable. +> writable.

After Compline,
Zaxo


Comment on Re: How do I tell if a handle is open for writing?
Download Code
Re^2: How do I tell if a handle is open for writing?
by sauoq (Abbot) on Nov 07, 2005 at 17:11 UTC

    That isn't what he is asking for...

    $ touch x; perl -le 'open X,"x"; print -w X; print X "x" or die $!' 1 Bad file descriptor at -e line 1.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re^2: How do I tell if a handle is open for writing?
by DrWhy (Chaplain) on Nov 07, 2005 at 17:16 UTC
    Correct me if I'm wrong here, but doesn't -w myfilehandle tell you whether the file that myfilehandle is pointing to has its writable bits set? That's not what I want to know. I want to know if the filehandle is open for writing. You could open a writable file for reading. Then you couldn't write to it with that handle even though passing that handle to -w would still return true. Or have I been smoking the curtains again?

    --DrWhy

    "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

      Or have I been smoking the curtains again?

      No, the curtains are safe. I've added to my reply a sub that Does What You Want.

      After Compline,
      Zaxo

        Ah. That looks much more to the point :) I like this fcntl()-based solution better than sgifford's solution (print nothing) because it more directly answers the question at hand. Which is not to say that sgifford's solution is bad; it has the advantage that it will catch some writability problems not related to how the handle was opened, plus it looks less complex to the human reader of your code.

        Unfortunately, I'm doing this on Win32 :( Win32 doesn't implement the fcntl() function, so I'll go with the 'print nothing' solution for now.

        --DrWhy

        "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

Re^2: How do I tell if a handle is open for writing?
by dragonchild (Archbishop) on Feb 28, 2006 at 18:38 UTC
    I'm trying to extend this to a comparable readable() function.
    sub readable { my $fh = shift; (O_RDONLY | O_RDWR) & fcntl( $fh, F_GETFL, my $slush); } for (qw/ > < >> +< +> /) { open my $fh, $_, 'foo' or warn $! and next; print "$_\t", readable($fh)? 'readable.': 'not readable.', $/; } __END__ > not readable. < not readable. >> not readable. +< readable. +> readable.
    Huh??!

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://506415]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2014-12-18 11:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (50 votes), past polls