in reply to file locking

You can try os specific tools to see if a file is being used, for example fuser:
open FILE, "sample"; system("fuser sample") or print "File not in use\n"; close FILE; system("fuser sample") or print "File not in use\n";
Maybe Linux::Fuser from CPAN can help you.