| 1 | |||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | package Tie::FlatFile::Array; | ||||||||||
| 6 | 1 | ▶ | use base 'Class::Accessor'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Fcntl; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use POSIX qw(:stdio_h ceil); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use FileHandle; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use English qw(-no_match_vars); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use File::Spec::Functions qw(catfile splitpath); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 1 | my @fields; | |||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | our $VERSION = 0.05; | |||||||||
| 18 | 1 | $VERSION = eval $VERSION; | |||||||||
| 19 | 1 | @fields = qw(filename flags mode packformat handle | |||||||||
| 20 | reclen nulls nulla); | ||||||||||
| 21 | 1 | __PACKAGE__->mk_accessors(@fields); | |||||||||
| 22 | 1 | *fh = \&handle; | |||||||||
| 23 | # require Tie::FlatFile::ArrayHelper; | ||||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | 0 | ▶ | sub TIEARRAY { | ||||||||
|
Subroutine
TIEARRAY: not called CC=4 SCAR=27.2
| |||||||||||
| 27 | 0 | my $class = shift; | |||||||||
| 28 | 0 | my $self = bless({}, $class); | |||||||||
| 29 | 0 | my ($filename, $flags, $mode, $opts) = @_; | |||||||||
| 30 | 0 | my ($packformat); | |||||||||
| 31 | 0 | local $Carp::CarpLevel = 1; # Set the stack frame for croak(). | |||||||||
| 32 | |||||||||||
| 33 | 0 | ▶ | if ('HASH' ne ref($opts)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 0 | croak('Options hash missing'); | |||||||||
| 35 | } else { | ||||||||||
| 36 | 0 | $packformat = $opts->{packformat}; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | # Check for missing parameters. | ||||||||||
| 40 | 0 | foreach my $nm (qw(filename flags mode packformat)) { | |||||||||
| 41 | 0 | my $value = eval "\$$nm"; | |||||||||
| 42 | 0 | ▶ | unless (defined ($value)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 0 | croak("Missing $nm"); | |||||||||
| 44 | } | ||||||||||
| 45 | 0 | $self->$nm($value); | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | # Open the file and save the file handle. | ||||||||||
| 49 | 0 | my $fh = new FileHandle $filename, $flags; | |||||||||
| 50 | 0 | $self->handle($fh); | |||||||||
| 51 | |||||||||||
| 52 | # Store the record length; | ||||||||||
| 53 | 0 | my $len = $self->reclen(length(pack $packformat, (1) x 30)); | |||||||||
| 54 | |||||||||||
| 55 | { | ||||||||||
| 56 | 0 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||
| 57 | 0 | $self->nulls(pack $packformat, (undef) x 30); | |||||||||
| 58 | 0 | $self->nulla([(undef) x 30]); | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 0 | $self; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 0 | ▶ | sub UNTIE { | ||||||||
|
Subroutine
UNTIE: not called CC=2 SCAR=17.9
| |||||||||||
| 65 | 0 | my $self = shift; | |||||||||
| 66 | 0 | ▶ | return unless $self->handle; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 0 | close($self->handle); | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 0 | ▶ | sub FETCH { | ||||||||
|
Subroutine
FETCH: not called CC=3 SCAR=24.8
| |||||||||||
| 71 | 0 | my ($self, $index) = @_; | |||||||||
| 72 | 0 | ▶ | return undef if $index < 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | |||||||||||
| 74 | 0 | my $len = $self->reclen; | |||||||||
| 75 | 0 | my $fh = $self->fh; | |||||||||
| 76 | 0 | local $Carp::CarpLevel = 1; # Set the stack frame for croak(). | |||||||||
| 77 | |||||||||||
| 78 | 0 | local $RS = \$len; # Set the record length. | |||||||||
| 79 | 0 | seek($fh, $index * $len, SEEK_SET); | |||||||||
| 80 | 0 | my $data = <$fh>; # Get a record. | |||||||||
| 81 | 0 | ▶ | return undef unless $data; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | |||||||||||
| 83 | # Unpack and return the data as an array reference. | ||||||||||
| 84 | 0 | [ unpack $self->packformat, $data ]; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 0 | ▶ | sub STORE { | ||||||||
|
Subroutine
STORE: not called CC=1 SCAR=6.9
| |||||||||||
| 88 | 0 | my ($self, $index, $value) = @_; | |||||||||
| 89 | 0 | my $len = $self->reclen; | |||||||||
| 90 | 0 | my $fh = $self->fh; | |||||||||
| 91 | |||||||||||
| 92 | 0 | seek($fh, $index * $len, SEEK_SET); | |||||||||
| 93 | 0 | print $fh (pack $self->packformat, @$value); | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | 0 | ▶ | sub FETCHSIZE { | ||||||||
|
Subroutine
FETCHSIZE: not called CC=1 SCAR=6.9
| |||||||||||
| 97 | 0 | my $self = shift; | |||||||||
| 98 | 0 | my $pos = tell($self->fh); | |||||||||
| 99 | |||||||||||
| 100 | # Go to the end of the file and find out the | ||||||||||
| 101 | # size in bytes [using tell()] and divide that | ||||||||||
| 102 | # by the size of a record. | ||||||||||
| 103 | 0 | seek($self->fh, 0, SEEK_END); | |||||||||
| 104 | 0 | my $size = tell($self->fh) / $self->reclen; | |||||||||
| 105 | 0 | $size = ceil($size); | |||||||||
| 106 | |||||||||||
| 107 | # Go back to the original position in the file. | ||||||||||
| 108 | 0 | seek($self->fh, $pos, SEEK_SET); | |||||||||
| 109 | 0 | $size; | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | |||||||||||
| 113 | 0 | ▶ | sub EXTEND { | ||||||||
|
Subroutine
EXTEND: not called CC=1 SCAR=0.0
| |||||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | |||||||||||
| 117 | 0 | ▶ | sub EXISTS { | ||||||||
|
Subroutine
EXISTS: not called CC=2 SCAR=17.9
| |||||||||||
| 118 | 0 | my ($self, $index) = @_; | |||||||||
| 119 | 0 | ▶ | $index >= 0 && $index < $self->FETCHSIZE; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | 0 | ▶ | sub DELETE { | ||||||||
|
Subroutine
DELETE: not called CC=1 SCAR=6.9
| |||||||||||
| 123 | 0 | my ($self, $index) = @_; | |||||||||
| 124 | 0 | $self->STORE($index, $self->nulla); | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | 0 | ▶ | sub CLEAR { | ||||||||
|
Subroutine
CLEAR: not called CC=1 SCAR=6.9
| |||||||||||
| 128 | 0 | my $self = shift; | |||||||||
| 129 | 0 | truncate($self->fh, 0); | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | 0 | ▶ | sub PUSH { | ||||||||
|
Subroutine
PUSH: not called CC=2 SCAR=17.9
| |||||||||||
| 133 | 0 | my $self = shift; | |||||||||
| 134 | 0 | my $size = $self->FETCHSIZE; | |||||||||
| 135 | 0 | ▶ | $self->STORE($size++, +shift) while (@_); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | 0 | ▶ | sub POP { | ||||||||
|
Subroutine
POP: not called CC=1 SCAR=6.9
| |||||||||||
| 139 | 0 | my $self = shift; | |||||||||
| 140 | 0 | my $size = $self->FETCHSIZE; | |||||||||
| 141 | 0 | my $data = $self->FETCH($size-1); | |||||||||
| 142 | 0 | truncate($self->fh, ($size-1) * $self->reclen); | |||||||||
| 143 | 0 | $data; | |||||||||
| 144 | } | ||||||||||
| 145 | |||||||||||
| 146 | 0 | ▶ | sub SHIFT { | ||||||||
|
Subroutine
SHIFT: not called CC=3 SCAR=24.8
| |||||||||||
| 147 | 0 | my $self = shift; | |||||||||
| 148 | 0 | my $size = $self->FETCHSIZE; | |||||||||
| 149 | 0 | ▶ | return undef unless $size; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 150 | |||||||||||
| 151 | 0 | my $data = $self->FETCH(0); | |||||||||
| 152 | 0 | my $reclen = $self->reclen; | |||||||||
| 153 | 0 | my $fh = $self->fh; | |||||||||
| 154 | 0 | local $RS = \$reclen; | |||||||||
| 155 | |||||||||||
| 156 | 0 | foreach my $n (0..$size-2) { | |||||||||
| 157 | 0 | seek($fh, ($n+1) * $reclen, SEEK_SET); | |||||||||
| 158 | 0 | my $temp = <$fh>; | |||||||||
| 159 | 0 | seek($fh, -2*$reclen, SEEK_CUR); | |||||||||
| 160 | 0 | print $fh $temp; | |||||||||
| 161 | } | ||||||||||
| 162 | |||||||||||
| 163 | 0 | truncate($fh, ($size-1)*$reclen ); | |||||||||
| 164 | 0 | $data; | |||||||||
| 165 | } | ||||||||||
| 166 | |||||||||||
| 167 | 0 | ▶ | sub UNSHIFT { | ||||||||
|
Subroutine
UNSHIFT: not called CC=3 SCAR=24.8
| |||||||||||
| 168 | 0 | my $self = shift; | |||||||||
| 169 | |||||||||||
| 170 | 0 | ▶ | for (my $n = $self->FETCHSIZE-1; $n >= 0; --$n) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 171 | 0 | my $ele = $self->FETCH($n); | |||||||||
| 172 | 0 | $self->STORE($n + @_, $ele); | |||||||||
| 173 | } | ||||||||||
| 174 | |||||||||||
| 175 | 0 | foreach my $n (0..$#_) { | |||||||||
| 176 | 0 | $self->STORE($n, $_[$n]); | |||||||||
| 177 | } | ||||||||||
| 178 | 0 | $self->FETCHSIZE; | |||||||||
| 179 | } | ||||||||||
| 180 | |||||||||||
| 181 | |||||||||||
| 182 | 1 | 1; | |||||||||
| 183 | |||||||||||