Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
more useful options
 
PerlMonks  

Re: "unique" filename?

by Fastolfe (Vicar)
on Dec 03, 2000 at 17:17 UTC ( [id://44687]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to "unique" filename?

For generating, say, unique temporary files, use IO::File::new_tmpfile. In addition there's the POSIX::tmpnam() function which would get you a unique filename (subject to a race condition, however, since there's a period between when it's generated and when it's used).

In a pinch, use something like this:

$num = 12345; $dir = "/some/dir/"; $num++ while -e "$dir/$num"; open(F, ">$dir/$num") or die ...
But of course, there's still a race condition. A more secure solution would be to take advantage of sysopen and O_EXCL to iterate through filenames:
use Fcntl; use FileHandle; $num++ while !sysopen(F, "$dir/$num", O_EXCL|O_CREAT, 0777) && $! eq " +File exists"; die "$dir/$num: $!" if $!;
Once you start adding all of the bells and whistles, though, you're just re-implementing IO::File new_tmpfile.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://44687]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.