in reply to Reading directories and parsing standard names

In the spirit of thinking outside the box. do the file numbers have to be sequential or start at 1? When I've needed to produce unique files within a directory, it's often been sufficient to use the current time as part of the filename, viz:
$name = 'foo_' . time() . '.bar'; or my($sec, $min, $hr, $day, $mon, $year) = (gmtime)[0..5]; $name = sprintf("foo_%04d%02d%02d.%02d%02d%02d.bar", $year + 1900, $mon + 1, $day, $hr, $min, $sec);
As long as you don't try to create more than one file per second, this will produce a new filename every time.

Replies are listed 'Best First'.
Re: Re: Reading directories and parsing standard names
by abstracts (Hermit) on Aug 12, 2001 at 14:06 UTC
    Hello

    To generate unique names for files, you can even use the File::Temp module. The function tempfile, given a template, returns the filehandle and filename of the new file that was just opened. This way, you don't need to worry about the one-sec-time or other problems.

    use File::Temp qw/tempfile/; ($fh, $filename) = tempfile( $template, DIR => $dir, SUFFIX => '.dat', + CLEANUP => 0);
    Hope this helps,,,

    Aziz,,,