How can I check if a filehandle is open in Perl?

matt picture matt · Feb 6, 2009 · Viewed 49.7k times · Source

Is there a way to check if a file is already open in Perl? I want to have a read file access, so don't require flock.

 open(FH, "<$fileName") or die "$!\n" if (<FILE_IS_NOT_ALREADY_OPEN>);
 #  or something like
 close(FH) if (<FILE_IS_OPEN>);

Answer

chaos picture chaos · Feb 6, 2009

Please see the answer regarding openhandle() from Scalar::Util. The answer I originally wrote here was once the best we could do, but it's now badly outdated.