Commit | Line | Data |
1c19c868 |
1 | #!/usr/local/bin/perl -w |
262eb13a |
2 | # Test for File::Temp - POSIX functions |
3 | |
4 | use strict; |
5 | use Test; |
6 | BEGIN { plan tests => 7} |
7 | |
8 | use File::Temp qw/ :POSIX unlink0 /; |
9 | ok(1); |
10 | |
11 | # TMPNAM - scalar |
12 | |
13 | print "# TMPNAM: in a scalar context: \n"; |
14 | my $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. |
19 | ok( !(-e $tmpnam )); |
20 | |
21 | print "# TMPNAM file name: $tmpnam\n"; |
22 | |
23 | # TMPNAM array context |
24 | # Not strict posix behaviour |
25 | (my $fh, $tmpnam) = tmpnam(); |
26 | |
27 | print "# TMPNAM: in array context: $fh $tmpnam\n"; |
28 | |
29 | # File is opened - make sure it exists |
30 | ok( (-e $tmpnam )); |
31 | |
32 | # Unlink it |
33 | ok( unlink0($fh, $tmpnam) ); |
34 | |
35 | # TMPFILE |
36 | |
37 | $fh = tmpfile(); |
38 | |
39 | ok( $fh ); |
40 | print "# TMPFILE: tmpfile got FH $fh\n"; |
41 | |
42 | $fh->autoflush(1) if $] >= 5.006; |
43 | |
44 | # print something to it |
45 | my $original = "Hello a test\n"; |
46 | print "# TMPFILE: Wrote line: $original"; |
47 | print $fh $original |
48 | or die "Error printing to tempfile\n"; |
49 | |
50 | # rewind it |
51 | ok( seek($fh,0,0) ); |
52 | |
53 | |
54 | # Read from it |
55 | my $line = <$fh>; |
56 | |
57 | print "# TMPFILE: Read line: $line"; |
58 | ok( $original, $line); |
59 | |
60 | close($fh); |