1#!/usr/bin/perl
2
3use POSIX 'SEEK_SET';
4my $file = "tf06-$$.txt";
5$: = Tie::File::_default_recsep();
6
7print "1..5\n";
8
9my $N = 1;
10use Tie::File;
11print "ok $N\n"; $N++;
12
13my $o = tie @a, 'Tie::File', $file, autodefer => 0;
14print $o ? "ok $N\n" : "not ok $N\n";
15$N++;
16
17$a[0] = 'rec0';
18check_contents("rec0$:");
19$a[1] = "rec1$:";
20check_contents("rec0$:rec1$:");
21$a[2] = "rec2$:$:";             # should we detect this?
22check_contents("rec0$:rec1$:rec2$:$:");
23
24sub check_contents {
25  my $x = shift;
26  local *FH = $o->{fh};
27  seek FH, 0, SEEK_SET;
28  my $a;
29  { local $/; $a = <FH> }
30  $a = "" unless defined $a;
31  if ($a eq $x) {
32    print "ok $N\n";
33  } else {
34    my $msg = "not ok $N # expected <$x>, got <$a>";
35    ctrlfix($msg);
36    print "$msg\n";
37  }
38  $N++;
39}
40
41sub ctrlfix {
42  for (@_) {
43    s/\n/\\n/g;
44    s/\r/\\r/g;
45  }
46}
47
48END {
49  undef $o;
50  untie @a;
51  1 while unlink $file;
52}
53
54