Someone correct me if I'm wrong, but all you need to do is check the return code on the flock() call. If you got the lock, do your stuff, otherwise, do something else.
# Open the file
open my $SEM, "the_file";
if (flock($SEM, LOCK_EX)) { # lock it
#do stuff
}
close $SEM;