in reply to generating random filenames

While others have pointed out your error, and shown you the "Perlish" loop syntax that helps you avoid this type of error, you should know that the filename being random in no way ensures that a file of that name does not already exist, especially in this case if someone calls your subroutine with a small value of $length_limit. Better would be to incorporate a timestamp of some kind in addition to the random characters, but you still need to check that the file does not exist before you start to use the name.

For some other ideas on this topic, please have a look at "unique" filename?, which I located in a few seconds using SuperSearch - try it!

--
I'd like to be able to assign to an luser

Replies are listed 'Best First'.
Re: Re: generating random filenames
by particle (Vicar) on Jul 10, 2001 at 20:10 UTC
    right-o, albannach!

    my personal favorite is

    use POSIX; use IO::Handle; # create temp file my $TMPFH = new IO::Handle; do { $tmpfile = $mytmpdir . tmpnam() } until sysopen(TMPFH, $tmpfile, O_RDWR|O_CREAT|O_EXCL); # do something, like write to the tempfile foreach(@list) { print TMPFH $_,$n } # ... close TMPFH; # do something, like read in the tempfile... # when you're done, delete it unlink($tmpfile) or warn("WARN: cannot delete $tmpfile! $!");

    ~Particle

Re: Re: generating random filenames
by John M. Dlugosz (Monsignor) on Jul 10, 2001 at 23:02 UTC
    I like to use a UUID for the name. Some operating systems have a function for that (e.g. GetTempFileName in Win32).