Paste number 158520: mkdtemp.pl

Paste number 158520: mkdtemp.pl
Pasted by: phf
When:9 years, 4 months ago
Share:Tweet this! | http://paste.lisp.org/+3EBC
Channel:None
Paste contents:
Raw Source | XML | Display As
my @CHARS = (qw/ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
                 a b c d e f g h i j k l m n o p q r s t u v w x y z
                 0 1 2 3 4 5 6 7 8 9 _
               /);

sub mkdtemp {
    my $template = shift;
    my $path;
    for(my $i=0; $i<1000; $i++) {
        $path = $template;
        $path =~ s/X(?=X*$end)/$CHARS[ int( rand( @CHARS ) ) ]/ge;
        if(mkdir($path, 0700)) {
            chmod(0700, $path);
            return $path;
        } else {
            unless($!{EEXIST}) {
                die "couldn't create directory $path: $!";
            }
        }
    }
    
}

print (mkdtemp "/tmp/fooXXXXXX");

This paste has no annotations.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.