This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
A proper tempfile function that can be used multiple times.
[perl5.git] / t / test.pl
index 3aeb843..f0b4879 100644 (file)
--- a/t/test.pl
+++ b/t/test.pl
@@ -619,10 +619,32 @@ sub unlink_all {
     }
 }
 
+my @tmpfiles;
+END { unlink_all @tmpfiles }
+
+# Avoid ++, avoid ranges, avoid split //
+my @letters = 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);
+sub tempfile {
+    my $count = 0;
+    do {
+       my $temp = $count;
+       my $try = "tmp$$";
+       do {
+           $try .= $letters[$temp % 26];
+           $count = int ($temp / 26);
+       } while $temp;
+       if (!-e $try) {
+           # We have a winner
+           push @tmpfiles, $try;
+           return $try;
+       }
+       $count = $count + 1;
+    } while $count < 26 * 26;
+    die "Can't find temporary file name starting 'tmp$$'";
+}
 
-my $tmpfile = "misctmp000";
-1 while -f ++$tmpfile;
-END { unlink_all $tmpfile }
+# This is the temporary file for _fresh_perl
+my $tmpfile = tempfile();
 
 #
 # _fresh_perl
@@ -658,8 +680,8 @@ sub _fresh_perl {
 
     # Clean up the results into something a bit more predictable.
     $results =~ s/\n+$//;
-    $results =~ s/at\s+misctmp\d+\s+line/at - line/g;
-    $results =~ s/of\s+misctmp\d+\s+aborted/of - aborted/g;
+    $results =~ s/at\s+tmp\d+[A-Z][A-Z]?\s+line/at - line/g;
+    $results =~ s/of\s+tmp\d+[A-Z][A-Z]?\s+aborted/of - aborted/g;
 
     # bison says 'parse error' instead of 'syntax error',
     # various yaccs may or may not capitalize 'syntax'.