use Fcntl ':flock'; ... flock $fh, LOCK_EX or exit $status; ...