Thanks for that one!
It was indeed my backup sw (WD anywhere backup). After disabling it, the test script runs to 10000 loops without errors.
Followup question: why does this happen and is there an easy workaround (apart from disabling the backup sw)? I mean, I don't think I've had problems with deleting or modifying files, only renaming... And I don't think I've had issues outside of perl.
elef should file a bug report / complaint against this backup software. It doesn't even have to use the Backup API. It can simply specify full sharing when opening the file. Simply add in the FILE_SHARE_DELETE bit (which also allows renaming) when opening the file (though, the Backup API likely provides other benefits).
Errors became less frequent, but they're still there. I'll try your suggestion. I really like the idea of sleeping increasing intervals between attempts, too.
Edit: did the test, and the solution works. I also added some reporting to see how many extra attempts are needed for the renamings to succeed. The second attempt was sufficient every time, and it was needed ~50 times before the script got to 10000 loops. I'll implement this with 5 attempts max (just in case the backup sw spends more time on larger files) and forget about the issue. Thanks.
One of the solutions that you could use in conjunction with your script is Unlocker. Although the screen shots highlight the GUI, there's also a command line version. If you used that, your script should then be able to do the rename.