about summary refs log tree commit diff
path: root/editor/pnmquant
diff options
context:
space:
mode:
Diffstat (limited to 'editor/pnmquant')
-rwxr-xr-xeditor/pnmquant37
1 files changed, 20 insertions, 17 deletions
diff --git a/editor/pnmquant b/editor/pnmquant
index 5edbe85e..0e317f30 100755
--- a/editor/pnmquant
+++ b/editor/pnmquant
@@ -11,7 +11,6 @@
 use strict;
 use English;
 use Getopt::Long;
-#use File::Temp "tempfile";  # not available before Perl 5.6.1
 use File::Spec;
 #use Fcntl ":seek";  # not available in Perl 5.00503
 use Fcntl;  # gets open flags
@@ -22,22 +21,26 @@ my ($SEEK_SET, $SEEK_CUR, $SEEK_END) = (0, 1, 2);
 
 sub tempFile($) {
 
-# Here's what we'd do if we could expect Perl 5.6.1 or later, instead
-# of calling this subroutine:
-#    my ($file, $filename) = tempfile("pnmquant_XXXX", 
-#                                     SUFFIX=>".pnm", 
-#                                     DIR=>File::Spec->tmpdir())
-#                                     UNLINK=>$TRUE);
-    my ($suffix) = @_;
-    my $fileName;
-    local *file;  # For some inexplicable reason, must be local, not my
-    my $i;
-    $i = 0;
-    do {
-        $fileName = File::Spec->tmpdir() . "/pnmquant_" . $i++ . $suffix;
-    } until sysopen(*file, $fileName, O_RDWR|O_CREAT|O_EXCL);
-
-    return(*file, $fileName);
+    # We trust Perl's File::Temp to do a better job of creating the temp
+    # file, but it doesn't exist before Perl 5.6.1.
+
+    if (eval { require File::TempX; 1 }) {
+        return File::Temp::tempfile("pnmquant_XXXX", 
+                                    SUFFIX=>".pnm", 
+                                    DIR=>File::Spec->tmpdir(),
+                                    UNLINK=>$TRUE);
+    } else {
+        my ($suffix) = @_;
+        my $fileName;
+        local *file;  # For some inexplicable reason, must be local, not my
+        my $i;
+        $i = 0;
+        do {
+            $fileName = File::Spec->tmpdir() . "/pnmquant_" . $i++ . $suffix;
+        } until sysopen(*file, $fileName, O_RDWR|O_CREAT|O_EXCL);
+
+        return(*file, $fileName);
+    }
 }