in reply to Automatically creating incremental file names

My suggestion would be to use the time function from Time::HiRes.

use Time::HiRes qw[time]; printf "%.6f\n", time for 1..10; 1054051486.970288 1054051486.980302 1054051486.990316 1054051487.000331 1054051487.010345 1054051487.010345 1054051487.020360 1054051487.030374 1054051487.040388 1054051487.050403
That should give you a unique time (to the microsecond if your platform supports it), and should be okay until something like 2038?

Update: The timestamp is only a starting point.
If you are creating your temp files on shared media (suggestion:Don't!), or if you are running on a multi-processor machine, then you would need to combine the timestamps with additional information to aceive uniqueness. Processor ID in the latter case would work.
In the former case, which I would advise against, but if you have access to the hardcoded MAC addresses, this is a good starting point.

A bonus is that the resultant filenames are easily compared so cleaning up the directory periodically should be trivial.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

Replies are listed 'Best First'.
Re: Re: Automatically creating incremental file names
by halley (Prior) on May 27, 2003 at 17:39 UTC

    Note you output two identical names: 1054051487.010345, not a good trait for making unique filenames. You're at the mercy of the platform's performance if you depend on timestamps alone. Combining the value with something else that's dynamic, such as a PID and/or a simple $counter++ is preferable, but still weak.

    To really pick unique symbols, you should look for GUID or UUID generators, that's what they're for. A thousand machines running side by side won't even be able to pick the same symbol twice. Data::UUID seems useful.

    If you want unique temporary filenames, please look for modules that do this. File::Temp for example. These often handle file cleanup too.

    Simple timestamps, counters, and -e checks may introduce new race conditions only discovered when you try to scale up to a few hundred or thousand independent concurrent tasks. Don't reinvent the wheel, just to rediscover the limitations of a naive approach.

    --
    [ e d @ h a l l e y . c c ]

      Piffle!

      Addendum: The output shown, was not a list of names, but a list of times.

      Have you looked in File::Temp?

      Have you tried using it? Have you seen how slow and cumbersome it is?

      Have you seen the race conditions it creates?

      Have you seen the list of caveats and exclusions when used on systems other that *nix? Ie. VMS, Win32.

      ANY mechanism for determining unique file names that works by guessing a possible candidate, and then looking to see if it already exists, is doomed to failure in multi-processor and/or shared media environments.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
Re: Automatically creating incremental file names
by Abigail-II (Bishop) on May 27, 2003 at 18:19 UTC
    That should give you a unique time (to the microsecond if you platform supports it), and should be okay until something like 2038?

    That doesn't give any garantee for uniqueness when there is more than one processor in the machine. And those will be commonplace long before 2038. If you want uniqueness, don't depend on only a clock.

    Abigail

      Agreed. In a multi-processor environments, additional steps must be taken.

      Likewise, if storing temporary file on shared media.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller