Commit | Line | Data |
e2cf2bdb |
1 | #!/usr/bin/perl -w |
262eb13a |
2 | # Test for File::Temp - POSIX functions |
3 | |
e2cf2bdb |
4 | BEGIN { |
5 | chdir 't' if -d 't'; |
20822f61 |
6 | @INC = '../lib'; |
e2cf2bdb |
7 | require Test; import Test; |
8 | plan(tests => 7); |
9 | } |
10 | |
262eb13a |
11 | use strict; |
262eb13a |
12 | |
13 | use File::Temp qw/ :POSIX unlink0 /; |
f5cf745e |
14 | use FileHandle; |
15 | |
262eb13a |
16 | ok(1); |
17 | |
18 | # TMPNAM - scalar |
19 | |
20 | print "# TMPNAM: in a scalar context: \n"; |
21 | my $tmpnam = tmpnam(); |
22 | |
23 | # simply check that the file does not exist |
24 | # Not a 100% water tight test though if another program |
25 | # has managed to create one in the meantime. |
26 | ok( !(-e $tmpnam )); |
27 | |
28 | print "# TMPNAM file name: $tmpnam\n"; |
29 | |
91e74348 |
30 | # TMPNAM list context |
262eb13a |
31 | # Not strict posix behaviour |
32 | (my $fh, $tmpnam) = tmpnam(); |
33 | |
91e74348 |
34 | print "# TMPNAM: in list context: $fh $tmpnam\n"; |
262eb13a |
35 | |
36 | # File is opened - make sure it exists |
37 | ok( (-e $tmpnam )); |
38 | |
39 | # Unlink it |
40 | ok( unlink0($fh, $tmpnam) ); |
41 | |
42 | # TMPFILE |
43 | |
44 | $fh = tmpfile(); |
45 | |
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 | |
61 | # Read from it |
62 | my $line = <$fh>; |
63 | |
64 | print "# TMPFILE: Read line: $line"; |
65 | ok( $original, $line); |
66 | |
67 | close($fh); |