3 # Basic operation, initializing the object from an already-open handle
4 # instead of from a filename
17 print "ok $N\n"; $N++;
19 use Fcntl 'O_CREAT', 'O_RDWR';
20 sysopen F, $file, O_CREAT | O_RDWR
21 or die "Couldn't create temp file $file: $!; aborting";
24 my $o = tie @a, 'Tie::File', \*F;
25 print $o ? "ok $N\n" : "not ok $N\n";
30 check_contents("rec0");
34 check_contents("rec0", "rec1");
36 check_contents("rec0", "rec1", "rec2");
38 # 9-14 same-length alterations
40 check_contents("new0", "rec1", "rec2");
42 check_contents("new0", "new1", "rec2");
44 check_contents("new0", "new1", "new2");
46 # 15-24 lengthening alterations
48 check_contents("long0", "new1", "new2");
50 check_contents("long0", "long1", "new2");
52 check_contents("long0", "long1", "long2");
54 check_contents("long0", "longer1", "long2");
56 check_contents("longer0", "longer1", "long2");
58 # 25-34 shortening alterations, including truncation
60 check_contents("short0", "longer1", "long2");
62 check_contents("short0", "short1", "long2");
64 check_contents("short0", "short1", "short2");
66 check_contents("short0", "sh1", "short2");
68 check_contents("sh0", "sh1", "short2");
72 check_contents("sh0", "sh1", "short2", "", "rec4");
74 check_contents("sh0", "sh1", "short2", "rec3", "rec4");
80 # Does it correctly detect a non-seekable handle?
83 if ($^O =~ /^(MSWin32|dos)$/) {
84 print "ok $N \# skipped ($^O has broken pipe semantics)\n";
87 my $pipe_succeeded = eval {pipe *R, *W};
90 print "ok $N \# skipped (no pipes: $@)\n";
92 } elsif (! $pipe_succeeded) {
93 print "ok $N \# skipped (pipe call failed: $!)\n";
97 $o = eval {tie @a, 'Tie::File', \*W};
99 if ($@ =~ /filehandle does not appear to be seekable/) {
103 print "not ok $N \# \$\@ is $@\n";
106 print "not ok $N \# passing pipe to TIEARRAY didn't abort program\n";
111 # try inserting a record into the middle of an empty file
113 use POSIX 'SEEK_SET';
116 my $x = join $/, @c, '';
117 local *FH = $o->{fh};
118 seek FH, 0, SEEK_SET;
119 # my $open = open FH, "< $file";
121 { local $/; $a = <FH> }
122 $a = "" unless defined $a;
126 s{$/}{\\n}g for $a, $x;
127 print "not ok $N\n# expected <$x>, got <$a>\n";
135 unless ($a[$_] eq "$c[$_]$/") {
136 $msg = "expected $c[$_]$/, got $a[$_]";
141 print $good ? "ok $N\n" : "not ok $N # $msg\n";
148 1 while unlink $file;