# Open the file open my $SEM, "the_file"; if (flock($SEM, LOCK_EX)) { # lock it #do stuff } close $SEM;