title: How can I test if I can write to a filehandle?
body: <p>I have some subroutines that I call like this <code>myWrite($fileName, \@data)</code>.  <code>myWrite()</code> opens the file and writes out the data in some way.  I want to modify <code>myWrite</code> so that I can call it as above <em>or</em> with a filehandle as the first argument.  (The main reason for this modification is to delegate the opening of the file to the calling script rather than the module.  If there is a better solution for how to tell an IO subroutine where to write, i'd be glad to hear it.)</p>
  
  <p>In order to do this, I must test whether the first input var is a filehandle.  I figured out how to do that by reading <a href="https://stackoverflow.com/questions/3214647/what-is-the-best-way-to-determine-if-a-scalar-holds-a-filehandle">this question</a>.  </p>
  
  <p><strong>Now here's my question</strong>: I also want to test whether I can write to this filehandle.  I can't figure out how to do that.</p>
  
  <p>Here's what I want to do:</p>
  
  <pre><code>sub myWrite {
    my ($writeTo, $data) = @_;
    my $fh;
    if (isFilehandle($writeTo)) { # i can do this
      die "you're an immoral person\n" 
        unless (canWriteTo($writeTo)); # but how do I do this?
      $fh = $writeTo;
    } else {
      open $fh, "&gt;", $writeTo;
    }
    ...
  }
  </code></pre>
  
  <p>All I need to know is if I can write to the filehandle, though it would be nice to see some general solution that tells you whether you're filehandle was opened with ">>" or "&lt;", or if it isn't open, etc.</p>
  
  <p>(Note that <a href="https://stackoverflow.com/questions/520196/how-can-i-check-if-a-filehandle-is-open-in-perl">this question</a> is related but doesn't seem to answer my question.)</p>