Create a unique directory name; with name accurate to the minute, perhaps useful for saving output files
sub makename { my $time = localtime(); my @time = (split(/\s+/,$time)); my @hr = (split(/\:/,$time[3])); $madename = "$time[1]$time[2]$time[4]-$hr[0]:$hr[1]"; return $madename; }

Replies are listed 'Best First'.
Re: Create unique scratchdir name
by rob_au (Abbot) on May 13, 2002 at 03:22 UTC
    The returned value from this function is highly predictive and as such could easily be hijacked by users with maligned intent - This issue is discussed and referenced in Using Temporary Files in Perl.

    A better approach for temporary or unique file or directory name generation would be to make use of the exported *BSD, glibc and POSIX functions of File::Temp, which has be incorporated as a core module in 5.7.x.

     

      Rob,

      you're assuming that's an issue. It might not be - depends on context. Eg, a daily scan of a log file? useful and not a problem.

      hsinclai, you can call localtime in array context. Doing so, an optimisation of the above might be:

      sub makename { my @time = localtime(); return sprintf("%04d-%02d-%02d_%02d:%02d:%02d", $time[5]+1900, $time +[4]+1, @time[3,2,1,0]); }

      Update: thanks I0 - oops.

      --
      seek(JOB,$$LA,0);

        Young Jedi, have you forgotten all that was taught to you? Never underestimate the power of the dark side - or forever will it hold you in its grasp.

        Plug those security holes, even if you believe that Jimi Hendrix in a pink UFO would sooner crash into your living room than this hole be exploited.

        --
        Ash OS durbatulk, ash OS gimbatul,
        Ash OS thrakatulk, agh burzum-ishi krimpatul!
        Uzg-Microsoft-ishi amal fauthut burguuli.

        You mean @time[3,2,1,0]
        My fault for not putting that in context; part of utility script in a trusted environment..
        Thanks for the snippet too.
        $Id: .signature,v .99 Sun May 12 19:48:45 2002 hsinclai Exp $ Program terminated {7} abnormal CONTACT YOUR SUPERVISOR