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