3 # Basic operation, initializing the object from an already-open handle
4 # instead of from a filename
7 $: = Tie::File::_default_recsep();
18 print "ok $N\n"; $N++;
20 use Fcntl 'O_CREAT', 'O_RDWR';
21 sysopen F, $file, O_CREAT | O_RDWR
22 or die "Couldn't create temp file $file: $!; aborting";
25 my $o = tie @a, 'Tie::File', \*F, autochomp => 0, autodefer => 0;
26 print $o ? "ok $N\n" : "not ok $N\n";
31 check_contents("rec0");
35 check_contents("rec0", "rec1");
37 check_contents("rec0", "rec1", "rec2");
39 # 9-14 same-length alterations
41 check_contents("new0", "rec1", "rec2");
43 check_contents("new0", "new1", "rec2");
45 check_contents("new0", "new1", "new2");
47 # 15-24 lengthening alterations
49 check_contents("long0", "new1", "new2");
51 check_contents("long0", "long1", "new2");
53 check_contents("long0", "long1", "long2");
55 check_contents("long0", "longer1", "long2");
57 check_contents("longer0", "longer1", "long2");
59 # 25-34 shortening alterations, including truncation
61 check_contents("short0", "longer1", "long2");
63 check_contents("short0", "short1", "long2");
65 check_contents("short0", "short1", "short2");
67 check_contents("short0", "sh1", "short2");
69 check_contents("sh0", "sh1", "short2");
73 check_contents("sh0", "sh1", "short2", "", "rec4");
75 check_contents("sh0", "sh1", "short2", "rec3", "rec4");
81 # Does it correctly detect a non-seekable handle?
82 { if ($^O =~ /^(MSWin32|dos)$/) {
83 print "ok $N # skipped ($^O has broken pipe semantics)\n";
86 my $pipe_succeeded = eval {pipe *R, *W};
89 print "ok $N # skipped (no pipes: $@)\n";
91 } elsif (! $pipe_succeeded) {
92 print "ok $N # skipped (pipe call failed: $!)\n";
96 $o = eval {tie @a, 'Tie::File', \*W};
98 if ($@ =~ /filehandle does not appear to be seekable/) {
102 print "not ok $N \# \$\@ is $@\n";
105 print "not ok $N \# passing pipe to TIEARRAY didn't abort program\n";
110 use POSIX 'SEEK_SET';
113 my $x = join $:, @c, '';
114 local *FH = $o->{fh};
115 seek FH, 0, SEEK_SET;
116 # my $open = open FH, "< $file";
118 { local $/; $a = <FH> }
119 $a = "" unless defined $a;
123 ctrlfix(my $msg = "# expected <$x>, got <$a>");
124 print "not ok $N\n$msg\n";
132 unless ($a[$_] eq "$c[$_]$:") {
133 $msg = "expected $c[$_]$:, got $a[$_]";
138 print $good ? "ok $N\n" : "not ok $N # $msg\n";
153 1 while unlink $file;