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