about summary refs log tree commit diff
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2011-07-23 01:30:56 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2011-07-23 01:30:56 +0000
commitb19c0ebbdecdf15bac62772f4584ad854b97137b (patch)
tree969c5a2a8a66882274fb88da3d6cb629ba03655d
parentf4210a0cc7d9c7e9773f21be453e540f3dcaaff4 (diff)
downloadnetpbm-mirror-b19c0ebbdecdf15bac62772f4584ad854b97137b.tar.gz
netpbm-mirror-b19c0ebbdecdf15bac62772f4584ad854b97137b.tar.xz
netpbm-mirror-b19c0ebbdecdf15bac62772f4584ad854b97137b.zip
Use File::Temp if it is available
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1518 9d0c8265-081b-0410-96cb-a4ca84ce46f8
-rw-r--r--doc/HISTORY3
-rwxr-xr-xeditor/pnmquant37
2 files changed, 23 insertions, 17 deletions
diff --git a/doc/HISTORY b/doc/HISTORY
index 0b43eb5a..07009aa5 100644
--- a/doc/HISTORY
+++ b/doc/HISTORY
@@ -10,6 +10,9 @@ not yet  BJH  Release 10.56.00
               no fully transparent area, output PNG is fully opaque.
               Introduced in 10.29.
 
+              pnmquant: use File::Temp::tempfile() instead of local
+              approximation if it is available.
+
               pnmquant: work with older Perl that doesn't have 3-argument open.
 
 11.06.29 BJH  Release 10.55.00
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);
+    }
 }