1N/A#!/usr/local/bin/perl -w
1N/A# Test for File::Temp - POSIX functions
1N/A
1N/Ause strict;
1N/Ause Test;
1N/ABEGIN { plan tests => 7}
1N/A
1N/Ause File::Temp qw/ :POSIX unlink0 /;
1N/Ause FileHandle;
1N/Aok(1);
1N/A
1N/A# TMPNAM - scalar
1N/A
1N/Aprint "# TMPNAM: in a scalar context: \n";
1N/Amy $tmpnam = tmpnam();
1N/A
1N/A# simply check that the file does not exist
1N/A# Not a 100% water tight test though if another program
1N/A# has managed to create one in the meantime.
1N/Aok( !(-e $tmpnam ));
1N/A
1N/Aprint "# TMPNAM file name: $tmpnam\n";
1N/A
1N/A# TMPNAM list context
1N/A# Not strict posix behaviour
1N/A(my $fh, $tmpnam) = tmpnam();
1N/A
1N/Aprint "# TMPNAM: in list context: $fh $tmpnam\n";
1N/A
1N/A# File is opened - make sure it exists
1N/Aok( (-e $tmpnam ));
1N/A
1N/A# Unlink it - a possible NFS issue again if TMPDIR is not a local disk
1N/Amy $status = unlink0($fh, $tmpnam);
1N/Aif ($status) {
1N/A ok( $status );
1N/A} else {
1N/A skip("Skip test failed probably due to \$TMPDIR being on NFS",1);
1N/A}
1N/A
1N/A# TMPFILE
1N/A
1N/A$fh = tmpfile();
1N/A
1N/Aif (defined $fh) {
1N/A ok( $fh );
1N/A print "# TMPFILE: tmpfile got FH $fh\n";
1N/A
1N/A $fh->autoflush(1) if $] >= 5.006;
1N/A
1N/A # print something to it
1N/A my $original = "Hello a test\n";
1N/A print "# TMPFILE: Wrote line: $original";
1N/A print $fh $original
1N/A or die "Error printing to tempfile\n";
1N/A
1N/A # rewind it
1N/A ok( seek($fh,0,0) );
1N/A
1N/A # Read from it
1N/A my $line = <$fh>;
1N/A
1N/A print "# TMPFILE: Read line: $line";
1N/A ok( $original, $line);
1N/A
1N/A close($fh);
1N/A
1N/A} else {
1N/A # Skip all the remaining tests
1N/A foreach (1..3) {
1N/A skip("Skip test failed probably due to \$TMPDIR being on NFS",1);
1N/A }
1N/A}
1N/A
1N/A
1N/A
1N/A