There are to approaches that come to mind. Use
File::CounterFile (the inc() method will return a unique value) or use
File::Temp (or just the guts at
Avoiding race condition with sysopen) and append the names to an order file (you still really need locking) to maintain your order, then just process the order file top to bottom.