ArrayHelper.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 81 CC 4 SCAR 13.9
CC
4
Cov
0%
block_shift
6.0
block_move
6.0
tester
6.0
CRAP 4.0
1  
2 package Tie::FlatFile::Array;
3 use strict;
4 use warnings;
5  
6 use Fcntl;
7 use POSIX qw(:stdio_h);
8  
9 sub block_shift {
10 my ($self, $position, $nelements, $distance) = @_;
11 my $fh = $self->fh;
12 my $reclen = $self->reclen;
13  
14 my $block;
15 $self->seek_index($position);
16 unless (read $fh, $block, $nelements * $reclen) {
17 return;
18 }
19  
20 $self->seek_index($position + $distance);
21 print $fh $block;
22 }
23  
24 sub seek_index {
25 my ($self, $index) = @_;
26 seek($self->fh, $index * $self->reclen, SEEK_SET);
27 }
28  
29 sub tell_index {
30 my $self = shift;
31 my $pos = tell($self->fh) / $self->reclen;
32 ceil($pos);
33 }
34  
35  
36  
37 sub block_move {
38 my ($self, $index, $newindex, $blocksize) = @_;
39 my $fh = $self->fh;
40 my $reclen = $self->reclen;
41 return unless $newindex != $index;
42  
43 # Copy the block to be moved.
44 my @block = map $self->FETCH($_), ($index..$index+$blocksize-1);
45  
46 # Store the block in its new location.
47 my $n = $newindex;;
48 $self->STORE($n++, shift @block) while(@block);
49 1;
50 }
51  
52 sub tester {
53 my $testfile = shift;
54 unlink $testfile;
55  
56 unless ($testfile =~ /\.dbf$/) {
57 die ("Filename '$testfile' does not end with .dbf");
58 }
59  
60 my @part = (
61 ['x_lycos.com', 8000],
62 ['x_msn.com',6140],
63 );
64  
65 my $flat;
66 $flat = tie my @dbf, 'Tie::FlatFile::Array', $testfile,
67 O_RDWR | O_CREAT, 0644, { packformat => 'A30N' }
68 or die("Tie failed: $!");
69  
70 push @dbf, ['google.com', 14592];
71 push @dbf, ['yahoo.com', 10126];
72 push @dbf, ['ask.com', 8114];
73 push @dbf, ['tyson.com', 314];
74 push @dbf, ['janesearch.com', 559], ['waits.org', 1029];
75 # $flat->block_move(1,0,2);
76  
77  
78 foreach my $n (0..$#dbf) {
79 print "@{$dbf[$n]}\n";
80 }
81  
82 undef $flat;
83 untie @dbf;
84  
85 }
86  
87  
88 1;
89  
90