We should open file with write intent for using
flock(fh, LOCK_EX) for portability. Because
flock(fh, LOCK_EX) is failed without write intent on some platforms(such as Solaris) and platforms which does not provide
flock. (In this case Perl emulates
flock with other API).
perldoc -f flock says
Note that the fcntl(2) emulation of flock(3) requires that FILEHANDLE be open with read intent to use LOCK_SH and requires that it be open with write intent to use LOCK_EX.