summaryrefslogtreecommitdiff
path: root/t/lib/ftmp-posix.t
blob: 2e455861e947cf2c37c01d4b832b1b101ccd95e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/perl -w
# Test for File::Temp - POSIX functions

BEGIN {
	chdir 't' if -d 't';
	@INC = '../lib';
	require Test; import Test;
	plan(tests => 7);
}

use strict;

use File::Temp qw/ :POSIX unlink0 /;
ok(1);

# TMPNAM - scalar

print "# TMPNAM: in a scalar context: \n";
my $tmpnam = tmpnam();

# simply check that the file does not exist
# Not a 100% water tight test though if another program 
# has managed to create one in the meantime.
ok( !(-e $tmpnam ));

print "# TMPNAM file name: $tmpnam\n";

# TMPNAM list context
# Not strict posix behaviour
(my $fh, $tmpnam) = tmpnam();

print "# TMPNAM: in list context: $fh $tmpnam\n";

# File is opened - make sure it exists
ok( (-e $tmpnam ));

# Unlink it 
ok( unlink0($fh, $tmpnam) );

# TMPFILE

$fh = tmpfile();

ok( $fh );
print "# TMPFILE: tmpfile got FH $fh\n";

$fh->autoflush(1) if $] >= 5.006;

# print something to it
my $original = "Hello a test\n";
print "# TMPFILE: Wrote line: $original";
print $fh $original
  or die "Error printing to tempfile\n";

# rewind it
ok( seek($fh,0,0) );


# Read from it
my $line = <$fh>;

print "# TMPFILE: Read line: $line";
ok( $original, $line);

close($fh);