perl - How can I test if I can write to a filehandle? -


I have some subroutines that call me like myWrite ($ fileName, \ @data) Are there. myWrite () opens the file and writes the data in some way. I want to modify myWrite so that I can call or as the first argument with a file hand as the first argument. (The main reason for this modification is to open the module instead of the calling script to open the file. I would be happy to hear if there is a better solution to say how to write an IO substrateine.)

To do this, I have to check whether the first input var is a file handle. I came to know how to do this by reading

Now my question is : I also want to test Not whether I can write to the file-Tand. I do not understand how to do this.

What I want to do here:

  sub myWrite {my ($ writeTo, $ data) = @_; My $ FH; If (to write iFileHandle ($ write)) # # I can die "You are an unethical person \ n" unless (to write $ (to convert)); # But how do I do this? Write $ fh = $; } Other {open $ FH, "& gt;", $ write; } ...}  

I just need to know that I can write file scope, though it would be good to see some common solutions that tell you whether you handle the file With open ">>" or "& lt;", or if it is not open, etc.

(Note that it is related but does not seem to answer my question.)

 Open <,> , & lt; ',' / Some / file '; Open b, '& gt;', '/ some / other-file'; {Local $! = 0; My $ n = syswrite A, ""; #Result: $ nKnow, $! "Bad file descriptor"} {Local is $! = 0; My $ n = syswrite B, ""; #Result: $ n is 0, $! It looks like your mileage might vary, but something like this might happen on the right path: 

  Usage Fcntl; $ Flags = FCTL handles, F_GETFL, 0; # ("$ Flags & amp; O_ACCMODE) and (O_WRONLY | O_RDWR))" {Print "handle is writeable ... \ n"}  

Comments