All of these solutions using -e are introducing a race condition(*) into your code. If you can guarantee there's only ever going to be a single instance of this running at any given time, you might decide that's OK and live dangerously.
If you can't (or you're sufficiently paranoid to do it correctly the first time :), you need to look at using sysopen and the O_EXCL flag. You keep trying to open filenames until one succeeds, then you go on and write into it. See also perlopentut for examples using sysopen in this way.
(*) The race condition is that your test for the file's existence and the opening of the filename aren't a single, atomic action. It's entirely possible that two separate processes could check for the existence of the same filename simultaneously and both think they both can write to the same "unique" filename. Using sysopen and O_EXCL makes the test-and-open a single action which either succeeds or fails as one. Many security holes can be traced back to race conditions; do it right the first time and avoid them to begin with.
In reply to Re: unique filename
by Fletch
in thread unique filename
by lax
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |