summaryrefslogtreecommitdiff
path: root/lib/Tie/File/t/06_fixrec.t
blob: 62e55798d721d5e095ba578eb7882d673fa31b12 (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
#!/usr/bin/perl

use POSIX 'SEEK_SET';
my $file = "tf$$.txt";

print "1..5\n";

my $N = 1;
use Tie::File;
print "ok $N\n"; $N++;

my $o = tie @a, 'Tie::File', $file;
print $o ? "ok $N\n" : "not ok $N\n";
$N++;

$a[0] = 'rec0';
check_contents("rec0$/");
$a[1] = "rec1$/";
check_contents("rec0$/rec1$/");
$a[2] = "rec2$/$/";             # should we detect this?
check_contents("rec0$/rec1$/rec2$/$/");

sub check_contents {
  my $x = shift;
  local *FH = $o->{fh};
  seek FH, 0, SEEK_SET;
  my $a;
  { local $/; $a = <FH> }
  $a = "" unless defined $a;
  if ($a eq $x) {
    print "ok $N\n";
  } else {
    s{$/}{\\n}g for $a, $x;
    print "not ok $N\n# expected <$x>, got <$a>\n";
  }
  $N++;
}


END {
  undef $o;
  untie @a;
  1 while unlink $file;
}