This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to Tie::File 0.16.
[perl5.git] / lib / Tie / File / t / 16_handle.t
1 #!/usr/bin/perl
2 #
3 # Basic operation, initializing the object from an already-open handle
4 # instead of from a filename
5
6 my $file = "tf$$.txt";
7
8 print "1..39\n";
9
10 my $N = 1;
11 use Tie::File;
12 print "ok $N\n"; $N++;
13
14 use Fcntl 'O_CREAT', 'O_RDWR';
15 sysopen F, $file, O_CREAT | O_RDWR 
16   or die "Couldn't create temp file $file: $!; aborting";
17
18 my $o = tie @a, 'Tie::File', \*F;
19 print $o ? "ok $N\n" : "not ok $N\n";
20 $N++;
21
22 # 3-4 create
23 $a[0] = 'rec0';
24 check_contents("rec0");
25
26 # 5-8 append
27 $a[1] = 'rec1';
28 check_contents("rec0", "rec1");
29 $a[2] = 'rec2';
30 check_contents("rec0", "rec1", "rec2");
31
32 # 9-14 same-length alterations
33 $a[0] = 'new0';
34 check_contents("new0", "rec1", "rec2");
35 $a[1] = 'new1';
36 check_contents("new0", "new1", "rec2");
37 $a[2] = 'new2';
38 check_contents("new0", "new1", "new2");
39
40 # 15-24 lengthening alterations
41 $a[0] = 'long0';
42 check_contents("long0", "new1", "new2");
43 $a[1] = 'long1';
44 check_contents("long0", "long1", "new2");
45 $a[2] = 'long2';
46 check_contents("long0", "long1", "long2");
47 $a[1] = 'longer1';
48 check_contents("long0", "longer1", "long2");
49 $a[0] = 'longer0';
50 check_contents("longer0", "longer1", "long2");
51
52 # 25-34 shortening alterations, including truncation
53 $a[0] = 'short0';
54 check_contents("short0", "longer1", "long2");
55 $a[1] = 'short1';
56 check_contents("short0", "short1", "long2");
57 $a[2] = 'short2';
58 check_contents("short0", "short1", "short2");
59 $a[1] = 'sh1';
60 check_contents("short0", "sh1", "short2");
61 $a[0] = 'sh0';
62 check_contents("sh0", "sh1", "short2");
63
64 # file with holes
65 $a[4] = 'rec4';
66 check_contents("sh0", "sh1", "short2", "", "rec4");
67 $a[3] = 'rec3';
68 check_contents("sh0", "sh1", "short2", "rec3", "rec4");
69
70 close F;
71 undef $o;
72 untie @a;
73
74 # Does it correctly detect a non-seekable handle?
75 {  eval {pipe *R, *W};
76    close R;
77    if ($@) {
78      print "ok $N # skipped\n";
79      last;
80    }
81    $o = eval {tie @a, 'Tie::File', \*W};
82    if ($@ && $@ =~ /filehandle does not appear to be seekable/) {
83      print "ok $N\n";
84    } else {
85      print "not ok $N\n";
86    }
87    $N++;
88 }
89
90 # try inserting a record into the middle of an empty file
91
92 use POSIX 'SEEK_SET';
93 sub check_contents {
94   my @c = @_;
95   my $x = join $/, @c, '';
96   local *FH = $o->{fh};
97   seek FH, 0, SEEK_SET;
98 #  my $open = open FH, "< $file";
99   my $a;
100   { local $/; $a = <FH> }
101   $a = "" unless defined $a;
102   if ($a eq $x) {
103     print "ok $N\n";
104   } else {
105     s{$/}{\\n}g for $a, $x;
106     print "not ok $N\n# expected <$x>, got <$a>\n";
107   }
108   $N++;
109
110   # now check FETCH:
111   my $good = 1;
112   my $msg;
113   for (0.. $#c) {
114     unless ($a[$_] eq "$c[$_]$/") {
115       $msg = "expected $c[$_]$/, got $a[$_]";
116       $msg =~ s{$/}{\\n}g;
117       $good = 0;
118     }
119   }
120   print $good ? "ok $N\n" : "not ok $N # $msg\n";
121   $N++;
122 }
123
124 END {
125   undef $o;
126   untie @a;
127   1 while unlink $file;
128 }
129
130