1#!/usr/local/bin/perl -w 2# Test for File::Temp - POSIX functions 3 4use strict; 5use Test::More tests => 7; 6 7use File::Temp qw/ :POSIX unlink0 /; 8use FileHandle; 9ok(1); 10 11# TMPNAM - scalar 12 13print "# TMPNAM: in a scalar context: \n"; 14my $tmpnam = tmpnam(); 15 16# simply check that the file does not exist 17# Not a 100% water tight test though if another program 18# has managed to create one in the meantime. 19ok( !(-e $tmpnam )); 20 21print "# TMPNAM file name: $tmpnam\n"; 22 23# TMPNAM list context 24# Not strict posix behaviour 25(my $fh, $tmpnam) = tmpnam(); 26 27print "# TMPNAM: in list context: $fh $tmpnam\n"; 28 29# File is opened - make sure it exists 30ok( (-e $tmpnam )); 31 32# Unlink it - a possible NFS issue again if TMPDIR is not a local disk 33my $status = unlink0($fh, $tmpnam); 34if ($status) { 35 ok( $status ); 36} else { 37 SKIP: { 38 skip("Skip test failed probably due to \$TMPDIR being on NFS",1); 39 } 40} 41 42# TMPFILE 43 44$fh = tmpfile(); 45 46if (defined $fh) { 47 ok( $fh ); 48 print "# TMPFILE: tmpfile got FH $fh\n"; 49 50 $fh->autoflush(1) if $] >= 5.006; 51 52 # print something to it 53 my $original = "Hello a test\n"; 54 print "# TMPFILE: Wrote line: $original"; 55 print $fh $original 56 or die "Error printing to tempfile\n"; 57 58 # rewind it 59 ok( seek($fh,0,0) ); 60 61 # Read from it 62 my $line = <$fh>; 63 64 print "# TMPFILE: Read line: $line"; 65 ok( $original, $line); 66 67 close($fh); 68 69} else { 70 # Skip all the remaining tests 71 foreach (1..3) { 72 SKIP: { 73 skip("Skip test failed probably due to \$TMPDIR being on NFS",1); 74 } 75 } 76} 77 78 79 80 81