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

Re: TIMTOWTDI Challenge: Create a filename

by xdg (Monsignor)
on Jan 09, 2009 at 05:13 UTC ( #735081=note: print w/replies, xml ) Need Help??

in reply to TIMTOWTDI Challenge: Create a filename

Assuming a quick-and-dirty temporary filename is desired:

my $filename = int rand 2**31;

Or pick a large random number of your choice. Plus if you need to generate the *same* temporary filenames on successive runs, you can set the seed for rand() ahead of time.

Similar in nature, but random alphanumerics, not unlike what File::Temp does internally:

my @chars = ('a' .. 'z', 'A' .. 'Z'); my $filename = join("", map { $chars[int rand(0+@chars)] } 0 .. 7);


Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Replies are listed 'Best First'.
Re^2: TIMTOWTDI Challenge: Create a filename
by pobocks (Chaplain) on Jan 11, 2009 at 07:18 UTC

    To fully exploit the marvelous utility of the unix file system naming conventions, how about this?

    my $max_num = 255; my @chars = ('a' .. 'z', 'A' .. 'Z',0 .. 9, '_'); my $filename = join("", map { $chars[int rand(0+@chars)] } 1 .. $max_n +um);

    I don't have the energy right now, but a further helpful revision would be filling in all of the characters Unix allows in filenames. Oh, wait. That would be all of them except null. The mind boggles ;-)

    for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://735081]
[erix]: Henry VIII has a lot to answer for :P
[LanX]: erix sure next time you miss a conference nearby we drop by ...
[LanX]: did you even go to the Dutch Perl Workshop in Utrecht?
[erix]: haha ok. I'm going to Glasgow though, and make a long road trip round the isles
[erix]: [Lanx} not the last time but the one before (there have been two)
[LanX]: when did you move to Utrecht, you used to live in a windmill on a dyke walking around in wooden shoes. ..
[LanX]: ... did you lie to me? OO
erix wouldn't mind a nice old windmill to live in... Mueller/Miller/ Molenaar

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2017-12-15 16:42 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (439 votes). Check out past polls.