Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Generating Random Filenames

by willyyam (Priest)
on Apr 11, 2005 at 16:51 UTC ( #446673=perlquestion: print w/replies, xml ) Need Help??

willyyam has asked for the wisdom of the Perl Monks concerning the following question:

I needed a temporary file, but I wanted it to have a random name so a) I'd be less likely to succeed the "exists" test and b) so I don't have to delete my temp file during debugging. After reading this node I came up with this:

for ($i = 1; $i <= 8; $i++) { $filename .= chr( int(rand 26) + 97 ); } open(TMPFILE, ">$filename.html");

The question is, is this a good method? Am I being wrong-headed in the first place? Any pointers would be appreciated. Thanks.

Replies are listed 'Best First'.
Re: Generating Random Filenames
by gam3 (Curate) on Apr 11, 2005 at 17:03 UTC
    Look at File::Temp. Then you won't have to reinvent the wheel.

    -- gam3
    A picture is worth a thousand words, but takes 200K.
Re: Generating Random Filenames
by Roy Johnson (Monsignor) on Apr 11, 2005 at 17:04 UTC

    Caution: Contents may have been coded under pressure.
Re: Generating Random Filenames
by cog (Parson) on Apr 11, 2005 at 16:57 UTC
    I don't consider that to be a good method.

    From the top of my head, I'd go with something like:

    $filename = time . "_${$}_" . (int rand(1024));

    The method you're proposing might be enough for what you're doing, but if you have the process running hundreds of times, you still have the possibility of failure.

    With the code I presented you, the only possibility of failure involves running the same process id twice at the same moment (only possible on very icky OS's that are not finished yet (I believe) or if you change the date/time of the machine and then bump into the same process id in the very same exact moment!

    And still, the rand(1024) would have to output the same value again :-)

Re: Generating Random Filenames
by jbrugger (Parson) on Apr 12, 2005 at 04:54 UTC
    And if you really want to do it yourself, i'd suggest you create a filname based on a md5 string, i would do it something like this:
    ... package mdCrypt; require Digest::MD5 qw(md5_hex); sub createMD5 { my $self = shift; my ($string) = join (',',@_); return md5_hex($string); } ... my $filename = $mdCryptObj->createMD5(localtime());
    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: Generating Random Filenames
by sh1tn (Priest) on Apr 11, 2005 at 17:13 UTC
    my $file_rand; do { $file_rand .= int rand(time) for 1..3 } while -e $file_rand;

      This code creates a race condition. Between the time the -e test is run, and the file is open, that file could have been created. It would be better to use sysopen with appropriate flags (e.g. you can have it open for writing but fail if the file already exists), or even better to use File::Temp, which does that for you.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://446673]
Approved by RazorbladeBidet
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2019-12-08 15:14 GMT
Find Nodes?
    Voting Booth?

    No recent polls found