File: 08_ro.t

package info (click to toggle)
perl 5.14.2-21%2Bdeb7u3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 89,728 kB
  • sloc: perl: 421,086; ansic: 195,186; sh: 37,852; pascal: 8,746; cpp: 3,893; makefile: 2,346; xml: 1,972; yacc: 1,602
file content (86 lines) | stat: -rw-r--r-- 1,892 bytes parent folder | download | duplicates (9)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/perl
#
# Make sure it works to open the file in read-only mode
#

my $file = "tf$$.txt";
$: = Tie::File::_default_recsep();

print "1..13\n";

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

my @items = qw(Gold Frankincense Myrrh Ivory Apes Peacocks);
init_file(join $:, @items, '');

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

$#a == $#items ? print "ok $N\n" : print "not ok $N\n";
$N++;

for my $i (0..$#items) {
  ("$items[$i]$:" eq $a[$i]) ? print "ok $N\n" : print "not ok $N\n";
  $N++;
}

sub init_file {
  my $data = shift;
  open F, "> $file" or die $!;
  binmode F;
  print F $data;
  close F;
}

undef $o; untie @a;
my $badrec = "Malformed";
# (10-13) When a record lacks the record seprator, we sneakily try
# to fix it.  How does that work when the file is read-only?
if (setup_badly_terminated_file(4)) {
  my $good = 1;
  my $warn;
  local $SIG{__WARN__} = sub { $good = 0; ctrlfix($warn = shift); };
  local $^W = 1;
  my $o = tie @a, 'Tie::File', $file, mode => O_RDONLY, autochomp => 0
    or die "Couldn't tie $file: $!";

  print $a[0] eq "Malformed$:" ? "ok $N\n" : "not ok $N\n";  $N++;
  print $good ? "ok $N\n" : "not ok $N # $warn\n";  $good = 1; $N++;
  print $a[0] eq "Malformed$:" ? "ok $N\n" : "not ok $N\n";  $N++;
  print $good ? "ok $N\n" : "not ok $N # $warn\n";  $good = 1; $N++;
}

sub setup_badly_terminated_file {
  my $NTESTS = shift;
  open F, "> $file" or die "Couldn't open $file: $!";
  binmode F;
  print F $badrec;
  close F;
  unless (-s $file == length $badrec) {
    for (1 .. $NTESTS) {
      print "ok $N \# skipped - can't create improperly terminated file\n";
      $N++;
    }
    return;
  }
  return 1;
}


sub ctrlfix {
  for (@_) {
    s/\n/\\n/g;
    s/\r/\\r/g;
  }
}

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