Search code examples
perlfile-iofilehandle

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


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>);

Solution

  • 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.