PSYC/Tie/File.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 162 CC 13 SCAR 24.2
CC
13
Cov
0%
TIEARRAY
42.0
read_chunk
6.0
FETCH
6.0
CRAP 11.2
1 package Net::PSYC::Tie::File;
2  
3 our $VERSION = '0.1';
4  
5 # this modules ties a file to an array.. not line-wise but in chunks of bytes.
6 # whatever.. fuck my english
7  
8 use bytes;
9 use strict;
10 use Carp;
11 use Fcntl;
12  
13 my %files;
14  
15 sub TIEARRAY {
16 # offset and size are used to specify a range of bytes
17 # in the file
18 my ($class, $file, $chunksize, $offset, $range) = @_;
19 local *FH;
20
21 unless (exists $files{$file}) {
22 sysopen(*FH, $file, O_RDONLY|O_NOFOLLOW) or do {
23 return;
24 };
25 binmode(*FH);
26 $files{$file} = [ *FH, 1 ];
27 } else {
28 *FH = $files{$file}->[0];
29 $files{$file}->[1]++;
30 }
31
32 # a -s seems enough to me..
33 my @stat = stat($file);
34 unless (@stat) {
35 return;
36 }
37 $offset ||= 0;
38 if ($offset >= $stat[7]) {
39 $offset = $stat[7] - 1;
40 }
41 $range ||= $stat[7] - $offset; # 0 means the rest!
42 if ($offset + $range > $stat[7]) {
43 $range = $stat[7] - $offset
44 }
45
46 my $array = [ 0 .. int($range / $chunksize) - (($range % $chunksize) ? 0 : 1)];
47
48 return bless {
49 'FH' => *FH,
50 'BYTES' => $chunksize,
51 'SIZE' => $stat[7],
52 'A' => $array,
53 'C' => 0,
54 'NAME' => $file,
55 'OFFSET'=> $offset,
56 'RANGE' => $range,
57 }, $class;
58 }
59  
60 sub read_chunk {
61 my ($self, $index) = @_;
62  
63 my ($data, $length);
64
65 if (($index + 1) * $self->{'BYTES'} > $self->{'RANGE'}) {
66 $length = $self->{'RANGE'} % $self->{'BYTES'};
67 } else {
68 $length = $self->{'BYTES'};
69 }
70
71 sysseek($self->{'FH'}, $index * $self->{'BYTES'} + $self->{'OFFSET'}, 0);
72 my $flag = sysread($self->{'FH'}, $data, $length);
73 return $data;
74 }
75  
76 sub FETCH {
77 my ($self, $index) = @_;
78
79 if (ref $self->{'A'}->[$index]) {
80 return ${$self->{'A'}->[$index]};
81 }
82 return read_chunk($self, $self->{'A'}->[$index]);
83 }
84  
85 sub FETCHSIZE {
86 my $self = shift;
87 return scalar @{$self->{'A'}};
88 }
89  
90 sub EXISTS {
91 my ($self, $index) = @_;
92 exists $self->{'A'}->[$index];
93 }
94  
95 sub UNTIE {
96 my $self = shift;
97 unless (--$files{$self->{'NAME'}}->[1]) {
98 close $self->{'FH'};
99 delete $files{$self->{'NAME'}};
100 delete $self->{'A'};
101 }
102 }
103  
104 # all methods below change the array
105  
106 sub STORE {
107 my ($self, $index, $value) = @_;
108
109 $self->{'A'}->[$index] = \$value;
110 }
111  
112 sub STORESIZE { }
113 sub EXTEND { }
114  
115 sub DELETE {
116 my ($self, $index) = @_;
117
118 if (ref $self->{'A'}->[$index]) {
119 return ${delete $self->{'A'}->[$index]};
120 }
121 # print STDERR "reading index $index \n";
122 return read_chunk($self, delete $self->{'A'}->[$index]);
123 }
124  
125 sub CLEAR { }
126  
127 sub PUSH {
128 my $self = shift;
129 push(@{$self->{'A'}}, map { \$_ } @_ );
130 }
131  
132 sub POP {
133 my $self = shift;
134 my $last = pop(@{$self->{'A'}});
135 (ref $last) ? $$last : read_chunk($self, $last);
136 }
137  
138 sub SHIFT {
139 my $self = shift;
140 my $first = shift(@{$self->{'A'}});
141 (ref $first) ? $$first : read_chunk($self, $first);
142 }
143  
144 sub UNSHIFT {
145 my $self = shift;
146 unshift(@{$self->{'A'}}, map { \$_ } @_ );
147 }
148  
149 sub SPLICE {
150 my $self = shift;
151 map { (ref $_) ? $$_ : read_chunk($self, $_) } splice(@{$self->{'A'}}, @_);
152 }
153  
154 1;