This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
8c9fd554e7eacf305aa7ebd10371d1e9307b76e1
[perl5.git] / t / lib / ftmp-posix.t
1 #!/usr/bin/perl -w
2 # Test for File::Temp - POSIX functions
3
4 BEGIN {
5         chdir 't' if -d 't';
6         unshift @INC, '../lib';
7         require Test; import Test;
8         plan(tests => 7);
9 }
10
11 use strict;
12
13 use File::Temp qw/ :POSIX unlink0 /;
14 ok(1);
15
16 # TMPNAM - scalar
17
18 print "# TMPNAM: in a scalar context: \n";
19 my $tmpnam = tmpnam();
20
21 # simply check that the file does not exist
22 # Not a 100% water tight test though if another program 
23 # has managed to create one in the meantime.
24 ok( !(-e $tmpnam ));
25
26 print "# TMPNAM file name: $tmpnam\n";
27
28 # TMPNAM list context
29 # Not strict posix behaviour
30 (my $fh, $tmpnam) = tmpnam();
31
32 print "# TMPNAM: in list context: $fh $tmpnam\n";
33
34 # File is opened - make sure it exists
35 ok( (-e $tmpnam ));
36
37 # Unlink it 
38 ok( unlink0($fh, $tmpnam) );
39
40 # TMPFILE
41
42 $fh = tmpfile();
43
44 ok( $fh );
45 print "# TMPFILE: tmpfile got FH $fh\n";
46
47 $fh->autoflush(1) if $] >= 5.006;
48
49 # print something to it
50 my $original = "Hello a test\n";
51 print "# TMPFILE: Wrote line: $original";
52 print $fh $original
53   or die "Error printing to tempfile\n";
54
55 # rewind it
56 ok( seek($fh,0,0) );
57
58
59 # Read from it
60 my $line = <$fh>;
61
62 print "# TMPFILE: Read line: $line";
63 ok( $original, $line);
64
65 close($fh);