| 1 | ##-*- Mode: CPerl -*- | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | ## | ||||||||||||||||||||||||||||||||||||||||||||||
| 3 | ## File: Tie/File/Indexed.pm | ||||||||||||||||||||||||||||||||||||||||||||||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||
| 5 | ## Description: tied array access to indexed data files | ||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | package Tie::File::Indexed; | ||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use 5.10.0; ##-- for // operator | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Tie::Array; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use JSON qw(); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Fcntl qw(:DEFAULT :seek :flock); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use File::Copy qw(); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use IO::File; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use Carp qw(confess); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | ##====================================================================== | ||||||||||||||||||||||||||||||||||||||||||||||
| 18 | ## Globals | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 5 | our @ISA = qw(Tie::Array); | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | 5 | our $VERSION = '0.09'; | |||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | ##====================================================================== | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | ## Constructors etc. | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | ## $tied = CLASS->new(%opts) | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | ## $tied = CLASS->new($file,%opts) | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | ## + %opts, object structure: | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | ## ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | ## file => $file, ##-- file basename; uses files "${file}", "${file}.idx", "${file}.hdr" | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | ## mode => $mode, ##-- open mode (fcntl flags or perl-style; default='rwa') | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | ## perms => $perms, ##-- default: 0666 & ~umask | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | ## pack_o => $pack_o, ##-- file offset pack template (default='J') | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | ## pack_l => $pack_l, ##-- string-length pack template (default='J') | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | ## bsize => $bsize, ##-- block-size in bytes for index batch-operations (default=2**21 = 2MB) | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | ## temp => $bool, ##-- if true, call unlink() on object destruction (default=false) | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | ## ## | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | ## ##-- pack lengths (after open()) | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | ## len_o => $len_o, ##-- packsize($pack_o) | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | ## len_l => $len_l, ##-- packsize($pack_l) | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | ## len_ix => $len_ix, ##-- packsize($pack_ix) == $len_o + $len_l | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | ## pack_ix=> $pack_ix, ##-- "${pack_o}${pack_l}" | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | ## ## | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | ## ##-- guts (after open()) | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | ## idxfh => $idxfh, ##-- $file.idx : [$i] => pack("${pack_o}${pack_l}", $offset_in_datfh_of_item_i, $len_in_datfh_of_item_i) | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | ## datfh => $datfh, ##-- $file : raw data (concatenated) | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | ## #size => $nrecords, ##-- cached number of records for faster FETCHSIZE() ##-- potentially UNSAFE for concurrent access: DON'T USE | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | ## ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 17 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=15.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 17 | my $that = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | 17 | ▶ | my $file = (@_ % 2)==0 ? undef : shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 17 | my %opts = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | 17 | ▶ | my $tied = bless({ | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
ref $that || $that
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table66%2/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | $that->defaults(), | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | file => $file, | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | @_, | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | }, ref($that)||$that); | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 17 | ▶ | return $tied->open() if (defined($tied->{file})); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | return $tied; | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | ## %defaults = CLASS_OR_OBJECT->defaults() | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | ## + default attributes for constructor | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 17 | ▶ | sub defaults { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | #file => $file, | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 17 | perms => (0666 & ~umask), | |||||||||||||||||||||||||||||||||||||||||||||
| 68 | mode => 'rwa', | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | pack_o => 'J', | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | pack_l => 'J', | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | block => 2**21, | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | ## undef = $tied->DESTROY() | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | ## + implicitly calls unlink() if 'temp' attribute is set to a true value | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | ## + implicitly calls close() | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 16 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 16 | ▶ | $_[0]->unlink() if ($_[0]{temp}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 16 | $_[0]->close(); | |||||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | ##====================================================================== | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | ## Utilities | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||
| 86 | ##-------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | ## Utilities: fcntl | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | ## $flags = CLASS_OR_OBJECT->fcflags($mode) | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | ## + returns Fcntl flags for symbolic string $mode | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 474 | ▶ | sub fcflags { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fcflags: called CC=10 SCAR=23.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 474 | ▶ | shift if (UNIVERSAL::isa($_[0],__PACKAGE__)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 474 | my $mode = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 94 | 474 | ▶ | $mode //= 'r'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$mode //= "r"
MC/DC0%0/1
$mode //= "r"
$mode
Truth table50%1/2
A
A= $mode
| |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 474 | ▶ | return $mode if ($mode =~ /^[0-9]+$/); ##-- numeric mode is interpreted as Fcntl bitmask | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 17 | my $fread = $mode =~ /[r<]/; | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | 17 | my $fwrite = $mode =~ /[wa>\+]/; | |||||||||||||||||||||||||||||||||||||||||||||
| 98 | 17 | ▶ | my $fappend = ($mode =~ /[a]/ || $mode =~ />>/); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$mode =~ /[a]/ || $mode =~ />>/
MC/DC50%1/2
$mode =~ /[a]/ || $mode =~ />>/
$mode =~ /[a]/
$mode =~ />>/
Truth table66%2/3
A || B
A= $mode =~ /[a]/B= $mode =~ />>/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 17 | ▶ | my $flags = ($fread | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | ? ($fwrite ? (O_RDWR|O_CREAT) : O_RDONLY) | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | : ($fwrite ? (O_WRONLY|O_CREAT) : 0) | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 17 | ▶ | $flags |= O_TRUNC if ($fwrite && !$fappend); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$fwrite and not $fappend
MC/DC100%2/2
$fwrite and not $fappend
$fwrite
not $fappend
Truth table100%3/3
A and not(B)
A= $fwriteB= not $fappend
| |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 17 | return $flags; | |||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | ## $fcflags = fcgetfl($fh) | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | ## + returns Fcntl flags for filehandle $fh | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub fcgetfl { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fcgetfl: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | shift if (UNIVERSAL::isa($_[0],__PACKAGE__)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | my $fh = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | return CORE::fcntl($fh,F_GETFL,0); | |||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||
| 115 | ## $bool = CLASS_OR_OBJECT->fcread($mode) | ||||||||||||||||||||||||||||||||||||||||||||||
| 116 | ## + returns true if any read-bits are set for $mode | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 80 | ▶ | sub fcread { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fcread: called CC=3 SCAR=12.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 80 | ▶ | shift if (UNIVERSAL::isa($_[0],__PACKAGE__)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 80 | my $flags = fcflags(shift); | |||||||||||||||||||||||||||||||||||||||||||||
| 120 | 80 | ▶ | return ($flags&O_RDONLY)==O_RDONLY || ($flags&O_RDWR)==O_RDWR; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
($flags & 0) == 0 || ($flags & 2) == 2
MC/DC0%0/2
($flags & 0) == 0 || ($flags & 2) == 2
($flags & 0) == 0
($flags & 2) == 2
Truth table33%1/3
A || B
A= ($flags & 0) == 0B= ($flags & 2) == 2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||
| 123 | ## $bool = CLASS_OR_OBJECT->fcwrite($mode) | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | ## + returns true if any write-bits are set for $mode | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 135 | ▶ | sub fcwrite { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fcwrite: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 135 | ▶ | shift if (UNIVERSAL::isa($_[0],__PACKAGE__)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 135 | my $flags = fcflags(shift); | |||||||||||||||||||||||||||||||||||||||||||||
| 128 | 135 | ▶ | return ($flags&O_WRONLY)==O_WRONLY || ($flags&O_RDWR)==O_RDWR; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
($flags & 1) == 1 || ($flags & 2) == 2
MC/DC50%1/2
($flags & 1) == 1 || ($flags & 2) == 2
($flags & 1) == 1
($flags & 2) == 2
Truth table66%2/3
A || B
A= ($flags & 1) == 1B= ($flags & 2) == 2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||
| 131 | ## $bool = CLASS_OR_OBJECT->fctrunc($mode) | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | ## + returns true if truncate-bits are set for $mode | ||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 122 | ▶ | sub fctrunc { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fctrunc: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 122 | ▶ | shift if (UNIVERSAL::isa($_[0],__PACKAGE__)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 122 | my $flags = fcflags(shift); | |||||||||||||||||||||||||||||||||||||||||||||
| 136 | 122 | return ($flags&O_TRUNC)==O_TRUNC; | |||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||
| 139 | ## $bool = CLASS_OR_OBJECT->fccreat($mode) | ||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | sub fccreat { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fccreat: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | shift if (UNIVERSAL::isa($_[0],__PACKAGE__)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | my $flags = fcflags(shift); | |||||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | return ($flags&O_CREAT)==O_CREAT; | |||||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||
| 146 | ## $str = CLASS_OR_OBJECT->fcperl($mode) | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | ## + return perl mode-string for $mode | ||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 54 | ▶ | sub fcperl { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fcperl: called CC=7 SCAR=23.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 54 | ▶ | shift if (UNIVERSAL::isa($_[0],__PACKAGE__)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 54 | my $flags = fcflags(shift); | |||||||||||||||||||||||||||||||||||||||||||||
| 151 | 54 | ▶ | return (fcread($flags) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | ? (fcwrite($flags) ##-- +read | ||||||||||||||||||||||||||||||||||||||||||||||
| 153 | ? (fctrunc($flags) ##-- +read,+write | ||||||||||||||||||||||||||||||||||||||||||||||
| 154 | ? '+>' : '+<') ##-- +read,+write,+/-trunc | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | : '<') | ||||||||||||||||||||||||||||||||||||||||||||||
| 156 | : (fcwrite($flags) ##-- -read | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | ? (fctrunc($flags) ##-- -read,+write | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | ? '>' : '>>') ##-- -read,+write,+/-trunc | ||||||||||||||||||||||||||||||||||||||||||||||
| 159 | : '<') ##-- -read,-write : default | ||||||||||||||||||||||||||||||||||||||||||||||
| 160 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||
| 163 | ## $fh_or_undef = CLASS_OR_OBJECT->fcopen($file,$mode) | ||||||||||||||||||||||||||||||||||||||||||||||
| 164 | ## $fh_or_undef = CLASS_OR_OBJECT->fcopen($file,$mode,$perms) | ||||||||||||||||||||||||||||||||||||||||||||||
| 165 | ## + opens $file with fcntl- or perl-style mode $mode | ||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 54 | ▶ | sub fcopen { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fcopen: called CC=9 SCAR=23.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 54 | ▶ | shift if (UNIVERSAL::isa($_[0],__PACKAGE__)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 54 | my ($file,$flags,$perms) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 169 | 54 | $flags = fcflags($flags); | |||||||||||||||||||||||||||||||||||||||||||||
| 170 | 54 | ▶ | $perms //= (0666 & ~umask); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$perms //= 438 & ~(umask)
MC/DC0%0/1
$perms //= 438 & ~(umask)
$perms
Truth table50%1/2
A
A= $perms
| |||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 54 | my $mode = fcperl($flags); | |||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 54 | my ($sysfh); | |||||||||||||||||||||||||||||||||||||||||||||
| 174 | 54 | ▶ | if (ref($file)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 175 | ##-- dup an existing filehandle | ||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | $sysfh = $file; | |||||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 178 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 179 | ##-- use sysopen() to honor O_CREAT and O_TRUNC | ||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 54 | ▶ | sysopen($sysfh, $file, $flags, $perms) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||
| 183 | ##-- now open perl-fh from system fh | ||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 54 | ▶ | open(my $fh, "${mode}&=", fileno($sysfh)) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 54 | ▶ | if (fcwrite($flags) && !fctrunc($flags)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
fcwrite($flags) and not fctrunc($flags)
MC/DC100%2/2
fcwrite($flags) and not fctrunc($flags)
fcwrite($flags)
not fctrunc($flags)
Truth table100%3/3
A and not(B)
A= fcwrite($flags)B= not fctrunc($flags)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 186 | ##-- append mode: seek to end of file | ||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 21 | ▶ | seek($fh, 0, SEEK_END) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 54 | return $fh; | |||||||||||||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||||||
| 192 | ##-------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 193 | ## Utilities: pack sizes | ||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||
| 195 | ## $len = CLASS->packsize($packfmt) | ||||||||||||||||||||||||||||||||||||||||||||||
| 196 | ## $len = CLASS->packsize($packfmt,@args) | ||||||||||||||||||||||||||||||||||||||||||||||
| 197 | ## + get pack-size for $packfmt with args @args | ||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 52 | ▶ | sub packsize { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
packsize: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 5 | ▶ | use bytes; ##-- deprecated in perl v5.18.2 | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 5 | ▶ | no warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 52 | return bytes::length(pack($_[0],@_[1..$#_])); | |||||||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||
| 205 | ##-------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 206 | ## Utilities: JSON | ||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||
| 208 | ## $data = CLASS->loadJsonString( $string,%opts) | ||||||||||||||||||||||||||||||||||||||||||||||
| 209 | ## $data = CLASS->loadJsonString(\$string,%opts) | ||||||||||||||||||||||||||||||||||||||||||||||
| 210 | ## + %opts passed to JSON::from_json(), e.g. (relaxed=>0) | ||||||||||||||||||||||||||||||||||||||||||||||
| 211 | ## + supports $opts{json} = $json_obj | ||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 13 | ▶ | sub loadJsonString { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
loadJsonString: called CC=4 SCAR=14.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 13 | ▶ | my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 13 | ▶ | my $bufr = ref($_[0]) ? $_[0] : \$_[0]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 13 | my %opts = @_[1..$#_]; | |||||||||||||||||||||||||||||||||||||||||||||
| 216 | 13 | ▶ | return $opts{json}->decode($$bufr) if ($opts{json}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 13 | return JSON::from_json($$bufr, {utf8=>!utf8::is_utf8($$bufr), relaxed=>1, allow_nonref=>1, %opts}); | |||||||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||
| 220 | ## $data = CLASS->loadJsonFile($filename_or_handle,%opts) | ||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 13 | ▶ | sub loadJsonFile { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
loadJsonFile: called CC=5 SCAR=16.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 13 | ▶ | my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 13 | my $file = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 224 | 13 | ▶ | my $fh = ref($file) ? $file : IO::File->new("<$file"); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 13 | ▶ | return undef if (!$fh); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 13 | binmode($fh,':raw'); | |||||||||||||||||||||||||||||||||||||||||||||
| 227 | 13 | local $/=undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 228 | 13 | my $buf = <$fh>; | |||||||||||||||||||||||||||||||||||||||||||||
| 229 | 13 | ▶ | close($fh) if (!ref($file)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 13 | return $that->loadJsonString(\$buf,@_); | |||||||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||
| 233 | ## $str = CLASS->saveJsonString($data) | ||||||||||||||||||||||||||||||||||||||||||||||
| 234 | ## $str = CLASS->saveJsonString($data,%opts) | ||||||||||||||||||||||||||||||||||||||||||||||
| 235 | ## + %opts passed to JSON::to_json(), e.g. (pretty=>0, canonical=>0)' | ||||||||||||||||||||||||||||||||||||||||||||||
| 236 | ## + supports $opts{json} = $json_obj | ||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 35 | ▶ | sub saveJsonString { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
saveJsonString: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 35 | ▶ | my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 35 | my $data = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 240 | 35 | my %opts = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 241 | 35 | ▶ | return $opts{json}->encode($data) if ($opts{json}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 35 | return JSON::to_json($data, {utf8=>1, allow_nonref=>1, allow_unknown=>1, allow_blessed=>1, convert_blessed=>1, pretty=>1, canonical=>1, %opts}); | |||||||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||||||
| 245 | ## $bool = CLASS->saveJsonFile($data,$filename_or_handle,%opts) | ||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 23 | ▶ | sub saveJsonFile { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
saveJsonFile: called CC=8 SCAR=24.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 23 | ▶ | my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 23 | my $data = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 249 | 23 | my $file = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 250 | 23 | ▶ | my $fh = ref($file) ? $file : IO::File->new(">$file"); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 23 | ▶ | logconfess((ref($that)||$that)."::saveJsonFile() failed to open file '$file': $!") if (!$fh); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $that || $that
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table0%0/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 23 | binmode($fh,':raw'); | |||||||||||||||||||||||||||||||||||||||||||||
| 253 | 23 | ▶ | $fh->print($that->saveJsonString($data,@_)) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 23 | ▶ | if (!ref($file)) { close($fh) || return undef; } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 23 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||
| 258 | ##-------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 259 | ## Utilities: debugging | ||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||
| 261 | ## $idxbuf = $tied->slurpIndex() | ||||||||||||||||||||||||||||||||||||||||||||||
| 262 | ## + slurps whole raw index-file into a string-buffer (for debugging) | ||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | ▶ | sub slurpIndex { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
slurpIndex: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | my $tied = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | ▶ | return undef if (!$tied->opened); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | my $fh = $tied->{idxfh}; | |||||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | ▶ | CORE::seek($fh, 0, SEEK_SET) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | local $/ = undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | return <$fh>; | |||||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||||
| 272 | ## $idxtxt = $tied->indexText() | ||||||||||||||||||||||||||||||||||||||||||||||
| 273 | ## @idxtxt = $tied->indexText() | ||||||||||||||||||||||||||||||||||||||||||||||
| 274 | ## + slurps whole index file and returns it as a text-buffer (for debugging) | ||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | ▶ | sub indexText { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
indexText: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | my $tied = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | ▶ | my @idx = map {join(' ',unpack($tied->{pack_ix},$_))} unpack("(A[$tied->{len_ix}])*", $tied->slurpIndex//''); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$tied->slurpIndex // ""
MC/DC0%0/1
$tied->slurpIndex // ""
$tied->slurpIndex
Truth table0%0/2
A
A= $tied->slurpIndex
| |||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | ▶ | return wantarray ? @idx : join("\n",@idx)."\n"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||
| 281 | ## $datbuf = $tied->slurpData() | ||||||||||||||||||||||||||||||||||||||||||||||
| 282 | ## + slurps whole raw data-file into a string-buffer (for debugging) | ||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | ▶ | sub slurpData { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
slurpData: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | my $tied = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | ▶ | return undef if (!$tied->opened); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | my $fh = $tied->{datfh}; | |||||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | ▶ | CORE::seek($fh, 0, SEEK_SET) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | local $/ = undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | return <$fh>; | |||||||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||
| 293 | ##====================================================================== | ||||||||||||||||||||||||||||||||||||||||||||||
| 294 | ## Subclass API: Data I/O | ||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||
| 296 | ## $bool = $tied->writeData($data) | ||||||||||||||||||||||||||||||||||||||||||||||
| 297 | ## + write item $data to $tied->{datfh} at its current position | ||||||||||||||||||||||||||||||||||||||||||||||
| 298 | ## + after writing, $tied->{datfh} should be positioned to the first byte following the written item | ||||||||||||||||||||||||||||||||||||||||||||||
| 299 | ## + $tied is assumed to be opened in write-mode | ||||||||||||||||||||||||||||||||||||||||||||||
| 300 | ## + default implementation just writes $data as a byte-string (undef is written as the empty string) | ||||||||||||||||||||||||||||||||||||||||||||||
| 301 | ## + can be overridden by subclasses to perform transparent encoding of complex data | ||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 24 | ▶ | sub writeData { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
writeData: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 24 | ▶ | return $_[0]{datfh}->print($_[1]//''); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_[1] // ""
MC/DC0%0/1
$_[1] // ""
$_[1]
Truth table50%1/2
A
A= $_[1]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||||||
| 306 | ## $data_or_undef = $tied->readData($length) | ||||||||||||||||||||||||||||||||||||||||||||||
| 307 | ## + read item data record of length $length from $tied->{datfh} at its current position | ||||||||||||||||||||||||||||||||||||||||||||||
| 308 | ## + default implementation just reads a byte-string of length $length | ||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 106 | ▶ | sub readData { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
readData: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 106 | ▶ | CORE::read($_[0]{datfh}, my $buf, $_[1])==$_[1] or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 106 | return $buf; | |||||||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||
| 314 | ##====================================================================== | ||||||||||||||||||||||||||||||||||||||||||||||
| 315 | ## Subclass API: Index I/O | ||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||
| 317 | ## ($off,$len) = $tied->readIndex($index) | ||||||||||||||||||||||||||||||||||||||||||||||
| 318 | ## ($off,$len) = $tied->readIndex(undef) | ||||||||||||||||||||||||||||||||||||||||||||||
| 319 | ## + gets index-record for item at logical index $index from $tied->{idxfh} | ||||||||||||||||||||||||||||||||||||||||||||||
| 320 | ## + if $index is undef, read from the current position of $tied->{idxfh} | ||||||||||||||||||||||||||||||||||||||||||||||
| 321 | ## + $index is assumed to exist in the array | ||||||||||||||||||||||||||||||||||||||||||||||
| 322 | ## + returns the empty list on error | ||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 133 | ▶ | sub readIndex { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
readIndex: called CC=4 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 133 | ▶ | !defined($_[1]) or CORE::seek($_[0]{idxfh}, $_[1]*$_[0]{len_ix}, SEEK_SET) or return qw(); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $_[1] or seek $_[0]{"idxfh"}, $_[1] * $_[0]{"len_ix"}, 0
MC/DC0%0/2
not defined $_[1] or seek $_[0]{"idxfh"}, $_[1] * $_[0]{"len_ix"}, 0
not defined $_[1]
seek $_[0]{"idxfh"}, $_[1] * $_[0]{"len_ix"}, 0
Truth table33%1/3
not(A) or B
A= not defined $_[1]B= seek $_[0]{"idxfh"}, $_[1] * $_[0]{"len_ix"}, 0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 133 | ▶ | CORE::read($_[0]{idxfh}, my $buf, $_[0]{len_ix})==$_[0]{len_ix} or return qw(); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 133 | return unpack($_[0]{pack_ix}, $buf); | |||||||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||
| 329 | ## $tied_or_undef = $tied->writeIndex($index,$off,$len) | ||||||||||||||||||||||||||||||||||||||||||||||
| 330 | ## $tied_or_undef = $tied->writeIndex(undef,$off,$len) | ||||||||||||||||||||||||||||||||||||||||||||||
| 331 | ## + writes index-record for item at logical index $index to $tied->{idxfh} | ||||||||||||||||||||||||||||||||||||||||||||||
| 332 | ## + if $index is undef, write at the current position of $tied->{idxfh} | ||||||||||||||||||||||||||||||||||||||||||||||
| 333 | ## + returns undef list on error | ||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 75 | ▶ | sub writeIndex { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
writeIndex: called CC=4 SCAR=14.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 75 | ▶ | !defined($_[1]) or CORE::seek($_[0]{idxfh}, $_[1]*$_[0]{len_ix}, SEEK_SET) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $_[1] or seek $_[0]{"idxfh"}, $_[1] * $_[0]{"len_ix"}, 0
MC/DC0%0/2
not defined $_[1] or seek $_[0]{"idxfh"}, $_[1] * $_[0]{"len_ix"}, 0
not defined $_[1]
seek $_[0]{"idxfh"}, $_[1] * $_[0]{"len_ix"}, 0
Truth table66%2/3
not(A) or B
A= not defined $_[1]B= seek $_[0]{"idxfh"}, $_[1] * $_[0]{"len_ix"}, 0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 75 | ▶ | $_[0]{idxfh}->print(pack($_[0]{pack_ix}, $_[2], $_[3])) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 75 | return $_[0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||||||
| 340 | ## $tied_or_undef = $tied->shiftIndex($start,$n,$shift) | ||||||||||||||||||||||||||||||||||||||||||||||
| 341 | ## + moves $n index records starting from $start by $shift positions (may be negative) | ||||||||||||||||||||||||||||||||||||||||||||||
| 342 | ## + operates directly on $tied->{idxfh} | ||||||||||||||||||||||||||||||||||||||||||||||
| 343 | ## + doesn't change old values unless they are overwritten | ||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 4 | ▶ | sub shiftIndex { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
shiftIndex: called CC=18 SCAR=30.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 4 | my ($tied,$start,$n,$shift) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||||||
| 347 | ##-- common variables | ||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 4 | my $idxfh = $tied->{idxfh}; | |||||||||||||||||||||||||||||||||||||||||||||
| 349 | 4 | my $len_ix = $tied->{len_ix}; | |||||||||||||||||||||||||||||||||||||||||||||
| 350 | 4 | ▶ | my $bsize = $tied->{bsize} // 2**21; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$tied->{"bsize"} // 2097152
MC/DC0%0/1
$tied->{"bsize"} // 2097152
$tied->{"bsize"}
Truth table50%1/2
A
A= $tied->{"bsize"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 4 | my $bstart = $len_ix * $start; | |||||||||||||||||||||||||||||||||||||||||||||
| 352 | 4 | my $bn = $len_ix * $n; | |||||||||||||||||||||||||||||||||||||||||||||
| 353 | 4 | my $bshift = $len_ix * $shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 354 | 4 | my ($buf,$boff,$blen); | |||||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||||||
| 356 | ##-- dispatch by shift direction | ||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 4 | ▶ | if ($shift > 0) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 358 | ##-- shift right (copy right-to-left) | ||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 2 | ▶ | CORE::seek($tied->{idxfh}, $bstart+$bn, SEEK_SET) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 2 | ▶ | while ($bn > 0) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 2 | ▶ | $blen = $bn > $bsize ? $bsize : $bn; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 2 | ▶ | CORE::seek($idxfh, -$blen, SEEK_CUR) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 2 | ▶ | CORE::read($idxfh, $buf, $blen)==$blen or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 2 | ▶ | CORE::seek($idxfh, $bshift-$blen, SEEK_CUR) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 2 | ▶ | $idxfh->print($buf) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 2 | ▶ | CORE::seek($idxfh, -$bshift, SEEK_CUR) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 2 | $bn -= $blen; | |||||||||||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 369 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 370 | ##-- shift left (copy left-to-right) | ||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 2 | ▶ | CORE::seek($tied->{idxfh}, $bstart, SEEK_SET) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 2 | ▶ | while ($bn > 0) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 2 | ▶ | $blen = $bn > $bsize ? $bsize : $bn; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 2 | ▶ | CORE::read($idxfh, $buf, $blen)==$blen or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 2 | ▶ | CORE::seek($idxfh, $bshift-$blen, SEEK_CUR) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 2 | ▶ | $idxfh->print($buf) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 2 | ▶ | CORE::seek($idxfh, -$bshift, SEEK_CUR) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 2 | $bn -= $blen; | |||||||||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 4 | return $tied; | |||||||||||||||||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||||||
| 387 | ##====================================================================== | ||||||||||||||||||||||||||||||||||||||||||||||
| 388 | ## Object API | ||||||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||||||
| 390 | ##-------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 391 | ## Object API: header | ||||||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||||||
| 393 | ## @keys = $tied->headerKeys() | ||||||||||||||||||||||||||||||||||||||||||||||
| 394 | ## + keys to save as header | ||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 23 | ▶ | sub headerKeys { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
headerKeys: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 23 | ▶ | return grep {!ref($_[0]{$_}) && $_ !~ m{^(?:file|mode|perms)$}} keys %{$_[0]}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
ref $_[0]{$_} || $_ =~ /^(?:file|mode|perms)$/
MC/DC100%2/2
ref $_[0]{$_} || $_ =~ /^(?:file|mode|perms)$/
ref $_[0]{$_}
$_ =~ /^(?:file|mode|perms)$/
Truth table100%3/3
A || B
A= ref $_[0]{$_}B= $_ =~ /^(?:file|mode|perms)$/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||||||
| 399 | ## \%header = $tied->headerData() | ||||||||||||||||||||||||||||||||||||||||||||||
| 400 | ## + data to save as header | ||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 23 | ▶ | sub headerData { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
headerData: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 23 | my $tied = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 403 | 23 | return {(map {($_=>$tied->{$_})} $tied->headerKeys), class=>ref($tied)}; | |||||||||||||||||||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||||||
| 406 | ## $tied_or_undef = $tied->loadHeader() | ||||||||||||||||||||||||||||||||||||||||||||||
| 407 | ## $tied_or_undef = $tied->loadHeader($headerFile,%opts) | ||||||||||||||||||||||||||||||||||||||||||||||
| 408 | ## + loads header from "$tied->{file}.hdr" | ||||||||||||||||||||||||||||||||||||||||||||||
| 409 | ## + %opts are passed to loadJsonFile() | ||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 13 | ▶ | sub loadHeader { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
loadHeader: called CC=5 SCAR=17.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 13 | my ($tied,$hfile,%opts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 412 | 13 | ▶ | $hfile //= $tied->{file}.".hdr" if (defined($tied->{file})); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$hfile //= $tied->{"file"} . '.hdr'
MC/DC0%0/1
$hfile //= $tied->{"file"} . '.hdr'
$hfile
Truth table50%1/2
A
A= $hfile
| |||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 13 | ▶ | confess(ref($tied)."::loadHeader(): no header-file specified and no 'file' attribute defined") if (!defined($hfile)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 13 | ▶ | my $hdata = $tied->loadJsonFile($hfile,%opts) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 415 | or confess(ref($tied)."::loadHeader(): failed to load header data from '$hfile'"); | ||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 13 | @$tied{keys %$hdata} = values %$hdata; | |||||||||||||||||||||||||||||||||||||||||||||
| 417 | 13 | return $tied; | |||||||||||||||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||||||
| 420 | ## $tied_or_undef = $tied->saveHeader() | ||||||||||||||||||||||||||||||||||||||||||||||
| 421 | ## $tied_or_undef = $tied->saveHeader($headerFile) | ||||||||||||||||||||||||||||||||||||||||||||||
| 422 | ## + saves header data to $headerFile | ||||||||||||||||||||||||||||||||||||||||||||||
| 423 | ## + %opts are passed to saveJsonFile() | ||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 23 | ▶ | sub saveHeader { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
saveHeader: called CC=4 SCAR=14.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 23 | my ($tied,$hfile,%opts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 426 | 23 | ▶ | $hfile //= $tied->{file}.".hdr" if (defined($tied->{file})); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$hfile //= $tied->{"file"} . '.hdr'
MC/DC0%0/1
$hfile //= $tied->{"file"} . '.hdr'
$hfile
Truth table50%1/2
A
A= $hfile
| |||||||||||||||||||||||||||||||||||||||||||||||
| 427 | 23 | ▶ | confess(ref($tied)."::saveHeader(): no header-file specified and no 'file' attribute defined") if (!defined($hfile)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 428 | 23 | return $tied->saveJsonFile($tied->headerData(), $hfile, %opts); | |||||||||||||||||||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||||||
| 431 | ##-------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 432 | ## Object API: open/close | ||||||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||||||
| 434 | ## $tied_or_undef = $tied->open($file,$mode) | ||||||||||||||||||||||||||||||||||||||||||||||
| 435 | ## $tied_or_undef = $tied->open($file) | ||||||||||||||||||||||||||||||||||||||||||||||
| 436 | ## $tied_or_undef = $tied->open() | ||||||||||||||||||||||||||||||||||||||||||||||
| 437 | ## + opens file(s) | ||||||||||||||||||||||||||||||||||||||||||||||
| 438 | 26 | ▶ | sub open { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
open: called CC=12 SCAR=26.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 26 | my ($tied,$file,$mode) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 440 | 26 | ▶ | $file //= $tied->{file}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$file //= $tied->{"file"}
MC/DC0%0/2
$file //= $tied->{"file"}
$file
$tied->{"file"}
Truth table66%2/3
A //= B
A= $fileB= $tied->{"file"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 26 | ▶ | $mode //= $tied->{mode}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$mode //= $tied->{"mode"}
MC/DC0%0/2
$mode //= $tied->{"mode"}
$mode
$tied->{"mode"}
Truth table66%2/3
A //= B
A= $modeB= $tied->{"mode"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 26 | ▶ | $tied->close() if ($tied->opened); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 26 | $tied->{file} = $file; | |||||||||||||||||||||||||||||||||||||||||||||
| 444 | 26 | $tied->{mode} = $mode = fcflags($mode); | |||||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 26 | ▶ | if (fcread($mode) && !fctrunc($mode)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
fcread($mode) and not fctrunc($mode)
MC/DC50%1/2
fcread($mode) and not fctrunc($mode)
fcread($mode)
not fctrunc($mode)
Truth table66%2/3
A and not(B)
A= fcread($mode)B= not fctrunc($mode)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 13 | ▶ | (!-e "$file.hdr" && fccreat($mode)) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not -e "$file.hdr" and fccreat($mode) or $tied->loadHeader
not -e "$file.hdr" and fccreat($mode)
MC/DC0%0/3
not -e "$file.hdr" and fccreat($mode) or $tied->loadHeader
not -e "$file.hdr"
fccreat($mode)
$tied->loadHeader
Truth table20%1/5
not(A) and B or C
A= not -e "$file.hdr"B= fccreat($mode)C= $tied->loadHeader
| |||||||||||||||||||||||||||||||||||||||||||||||
| 448 | or $tied->loadHeader() | ||||||||||||||||||||||||||||||||||||||||||||||
| 449 | or confess(ref($tied)."::failed to load header from '$tied->{file}.hdr': $!"); | ||||||||||||||||||||||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||||||
| 452 | $tied->{idxfh} = fcopen("$file.idx", $mode, $tied->{perms}) | ||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 26 | ▶ | or confess(ref($tied)."::open failed for index-file $file.idx: $!"); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 454 | $tied->{datfh} = fcopen("$file", $mode, $tied->{perms}) | ||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 26 | ▶ | or confess(ref($tied)."::open failed for data-file $file: $!"); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 26 | binmode($_) foreach (@$tied{qw(idxfh datfh)}); | |||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||
| 458 | ##-- pack lengths | ||||||||||||||||||||||||||||||||||||||||||||||
| 459 | #use bytes; ##-- deprecated in perl v5.18.2 | ||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 26 | $tied->{len_o} = packsize($tied->{pack_o}); | |||||||||||||||||||||||||||||||||||||||||||||
| 461 | 26 | $tied->{len_l} = packsize($tied->{pack_l}); | |||||||||||||||||||||||||||||||||||||||||||||
| 462 | 26 | $tied->{len_ix} = $tied->{len_o} + $tied->{len_l}; | |||||||||||||||||||||||||||||||||||||||||||||
| 463 | 26 | $tied->{pack_ix} = $tied->{pack_o}.$tied->{pack_l}; | |||||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||||||
| 465 | 26 | return $tied; | |||||||||||||||||||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||||
| 468 | ## $tied_or_undef = $tied->close() | ||||||||||||||||||||||||||||||||||||||||||||||
| 469 | ## + close any opened file, writes header if opened in write mode | ||||||||||||||||||||||||||||||||||||||||||||||
| 470 | 36 | ▶ | sub close { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
close: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 471 | 36 | my $tied = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 472 | 36 | ▶ | return $tied if (!$tied->opened); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 473 | 25 | ▶ | if ($tied->opened && fcwrite($tied->{mode})) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$tied->opened and fcwrite($tied->{"mode"})
MC/DC50%1/2
$tied->opened and fcwrite($tied->{"mode"})
$tied->opened
fcwrite($tied->{"mode"})
Truth table66%2/3
A and B
A= $tied->openedB= fcwrite($tied->{"mode"})
| |||||||||||||||||||||||||||||||||||||||||||||||
| 474 | 23 | ▶ | $tied->saveHeader() or | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 475 | confess(ref($tied)."::close(): failed to save header file"); | ||||||||||||||||||||||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 477 | 25 | delete @$tied{qw(idxfh datfh)}; ##-- should auto-close if not shared | |||||||||||||||||||||||||||||||||||||||||||||
| 478 | 25 | undef $tied->{file}; | |||||||||||||||||||||||||||||||||||||||||||||
| 479 | 25 | return $tied; | |||||||||||||||||||||||||||||||||||||||||||||
| 480 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||||||
| 482 | ## $bool = $tied->reopen() | ||||||||||||||||||||||||||||||||||||||||||||||
| 483 | ## + closes and re-opens underlying filehandles | ||||||||||||||||||||||||||||||||||||||||||||||
| 484 | ## + should cause a "real" flush even on systems without a working IO::Handle::flush | ||||||||||||||||||||||||||||||||||||||||||||||
| 485 | 7 | ▶ | sub reopen { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reopen: called CC=3 SCAR=13.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 486 | 7 | my $tied = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 487 | 7 | my ($file,$mode) = @$tied{qw(file mode)}; | |||||||||||||||||||||||||||||||||||||||||||||
| 488 | 7 | ▶ | return $tied->opened() && $tied->close() && $tied->open($file, $mode & ~O_TRUNC); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
$tied->opened && $tied->close && $tied->open($file, $mode & 18446744073709551103)
$tied->opened and $tied->close
MC/DC0%0/3
$tied->opened && $tied->close && $tied->open($file, $mode & 18446744073709551103)
$tied->opened
$tied->close
$tied->open($file, $mode & 18446744073709551103)
Truth table25%1/4
A and B && C
A= $tied->openedB= $tied->closeC= $tied->open($file, $mode & 18446744073709551103)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 489 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||||||
| 491 | ## $bool = $tied->opened() | ||||||||||||||||||||||||||||||||||||||||||||||
| 492 | ## + returns true iff object is opened | ||||||||||||||||||||||||||||||||||||||||||||||
| 493 | 105 | ▶ | sub opened { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
opened: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 494 | 105 | my $tied = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 495 | return (ref($tied) | ||||||||||||||||||||||||||||||||||||||||||||||
| 496 | && defined($tied->{idxfh}) | ||||||||||||||||||||||||||||||||||||||||||||||
| 497 | && defined($tied->{datfh}) | ||||||||||||||||||||||||||||||||||||||||||||||
| 498 | 105 | ▶ | ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
ref $tied && defined $tied->{'idxfh'} && defined $tied->{'datfh'}
ref $tied and defined $tied->{"idxfh"}
MC/DC33%1/3
ref $tied && defined $tied->{'idxfh'} && defined $tied->{'datfh'}
ref $tied
defined $tied->{"idxfh"}
defined $tied->{'datfh'}
Truth table50%2/4
A and B && C
A= ref $tiedB= defined $tied->{"idxfh"}C= defined $tied->{'datfh'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||||||
| 501 | ## $tied_or_undef = $tied->flush() | ||||||||||||||||||||||||||||||||||||||||||||||
| 502 | ## $tied_or_undef = $tied->flush($flushHeader) | ||||||||||||||||||||||||||||||||||||||||||||||
| 503 | ## + attempts to flush underlying filehandles using underlying filehandles' flush() method | ||||||||||||||||||||||||||||||||||||||||||||||
| 504 | ## (ususally IO::Handle::flush) | ||||||||||||||||||||||||||||||||||||||||||||||
| 505 | ## + also writes header file | ||||||||||||||||||||||||||||||||||||||||||||||
| 506 | ## + calls reopen() if underlying filehandles don't support a flush() method | ||||||||||||||||||||||||||||||||||||||||||||||
| 507 | 5 | ▶ | sub flush { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
flush: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 508 | 5 | my ($tied,$flushHeader) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 509 | 5 | my $rc = $tied->opened; | |||||||||||||||||||||||||||||||||||||||||||||
| 510 | 5 | if (0 && $rc && UNIVERSAL::can($tied->{idxfh},'flush') && UNIVERSAL::can($tied->{datfh},'flush')) { | |||||||||||||||||||||||||||||||||||||||||||||
| 511 | ##-- use fh flush() method | ||||||||||||||||||||||||||||||||||||||||||||||
| 512 | $rc = $tied->{idxfh}->flush() && $tied->{datfh}->flush() && (!$flushHeader || $tied->saveHeaderFile()); | ||||||||||||||||||||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 514 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 515 | ##-- use reopen() | ||||||||||||||||||||||||||||||||||||||||||||||
| 516 | 5 | $rc = $tied->reopen(); | |||||||||||||||||||||||||||||||||||||||||||||
| 517 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 518 | 5 | ▶ | return $rc ? $tied : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 519 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||||||
| 521 | ##-------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 522 | ## Object API: file operations | ||||||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||||||
| 524 | ## $tied_or_undef = $tied->unlink() | ||||||||||||||||||||||||||||||||||||||||||||||
| 525 | ## $tied_or_undef = $tied->unlink($file) | ||||||||||||||||||||||||||||||||||||||||||||||
| 526 | ## + attempts to unlink underlying files | ||||||||||||||||||||||||||||||||||||||||||||||
| 527 | ## + implicitly calls close() | ||||||||||||||||||||||||||||||||||||||||||||||
| 528 | 11 | ▶ | sub unlink { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unlink: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 529 | 11 | my ($tied,$file) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 530 | 11 | ▶ | $file //= $tied->{file}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$file //= $tied->{"file"}
MC/DC50%1/2
$file //= $tied->{"file"}
$file
$tied->{"file"}
Truth table66%2/3
A //= B
A= $fileB= $tied->{"file"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 531 | 11 | $tied->close(); | |||||||||||||||||||||||||||||||||||||||||||||
| 532 | 11 | ▶ | return undef if (!defined($file)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 533 | 10 | foreach ('','.idx','.hdr') { | |||||||||||||||||||||||||||||||||||||||||||||
| 534 | 30 | ▶ | CORE::unlink("${file}$_") or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 535 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 536 | 10 | return $tied; | |||||||||||||||||||||||||||||||||||||||||||||
| 537 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||||||
| 539 | ## $tied_or_undef = $tied->rename($newname) | ||||||||||||||||||||||||||||||||||||||||||||||
| 540 | ## + renames underlying file(s) using CORE::rename() | ||||||||||||||||||||||||||||||||||||||||||||||
| 541 | ## + implicitly close()s and re-open()s $tied | ||||||||||||||||||||||||||||||||||||||||||||||
| 542 | ## + object must be opened in write-mode | ||||||||||||||||||||||||||||||||||||||||||||||
| 543 | 1 | ▶ | sub rename { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rename: called CC=6 SCAR=20.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 544 | 1 | my ($tied,$newfile) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 545 | 1 | my $flags = fcflags($tied->{mode}); | |||||||||||||||||||||||||||||||||||||||||||||
| 546 | 1 | my $oldfile = $tied->{file}; | |||||||||||||||||||||||||||||||||||||||||||||
| 547 | 1 | ▶ | return undef if (!$tied->opened() || !fcwrite($flags) || !$tied->close); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$tied->opened and fcwrite($flags) and $tied->close
$tied->opened and fcwrite($flags)
MC/DC0%0/3
$tied->opened and fcwrite($flags) and $tied->close
$tied->opened
fcwrite($flags)
$tied->close
Truth table25%1/4
A and B and C
A= $tied->openedB= fcwrite($flags)C= $tied->close
| |||||||||||||||||||||||||||||||||||||||||||||||
| 548 | 1 | foreach ('','.idx','.hdr') { | |||||||||||||||||||||||||||||||||||||||||||||
| 549 | 3 | ▶ | CORE::rename("${oldfile}$_","${newfile}$_") or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 550 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 551 | 1 | return $tied->open($newfile, ($flags & ~O_TRUNC)); | |||||||||||||||||||||||||||||||||||||||||||||
| 552 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||||||||||
| 554 | ## $dst_object_or_undef = $tied_src->copy($dst_filename, %dst_opts) | ||||||||||||||||||||||||||||||||||||||||||||||
| 555 | ## $dst_object_or_undef = $tied_src->copy($dst_object) | ||||||||||||||||||||||||||||||||||||||||||||||
| 556 | ## + copies underlying file(s) using File::Copy::copy() | ||||||||||||||||||||||||||||||||||||||||||||||
| 557 | ## + source object must be opened | ||||||||||||||||||||||||||||||||||||||||||||||
| 558 | ## + implicitly calls flush() on both source and destination objects | ||||||||||||||||||||||||||||||||||||||||||||||
| 559 | ## + if a destination object is specified, it must be opened in write-mode | ||||||||||||||||||||||||||||||||||||||||||||||
| 560 | 2 | ▶ | sub copy { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
copy: called CC=10 SCAR=26.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 561 | 2 | my ($src,$dst,%opts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 562 | 2 | ▶ | return undef if (!$src->opened || !$src->flush); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$src->opened and $src->flush
MC/DC0%0/2
$src->opened and $src->flush
$src->opened
$src->flush
Truth table33%1/3
A and B
A= $src->openedB= $src->flush
| |||||||||||||||||||||||||||||||||||||||||||||||
| 563 | 2 | ▶ | $dst = $src->new($dst, %opts, mode=>'rw') if (!ref($dst)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 564 | 2 | ▶ | return undef if (!$dst->opened && !$dst->open($opts{file}, 'rw')); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$dst->opened or $dst->open($opts{"file"}, "rw")
MC/DC0%0/2
$dst->opened or $dst->open($opts{"file"}, "rw")
$dst->opened
$dst->open($opts{"file"}, "rw")
Truth table33%1/3
A or B
A= $dst->openedB= $dst->open($opts{"file"}, "rw")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||||||||||||||
| 566 | 2 | foreach (qw(idxfh datfh)) { | |||||||||||||||||||||||||||||||||||||||||||||
| 567 | 4 | ▶ | CORE::seek($src->{$_}, 0, SEEK_SET) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 568 | 4 | ▶ | CORE::seek($dst->{$_}, 0, SEEK_SET) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 569 | 4 | ▶ | File::Copy::copy($src->{$_}, $dst->{$_}) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 570 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 571 | 2 | return $dst->flush(); | |||||||||||||||||||||||||||||||||||||||||||||
| 572 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||||||||||||||||
| 574 | ## $tied_or_undef = $tied->move($newname) | ||||||||||||||||||||||||||||||||||||||||||||||
| 575 | ## + renames underlying file(s) using File::Copy::move() | ||||||||||||||||||||||||||||||||||||||||||||||
| 576 | ## + implicitly close()s and re-open()s $tied | ||||||||||||||||||||||||||||||||||||||||||||||
| 577 | ## + object must be opened in write-mode | ||||||||||||||||||||||||||||||||||||||||||||||
| 578 | 1 | ▶ | sub move { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
move: called CC=6 SCAR=20.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 579 | 1 | my ($tied,$newfile) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 580 | 1 | my $flags = fcflags($tied->{mode}); | |||||||||||||||||||||||||||||||||||||||||||||
| 581 | 1 | my $oldfile = $tied->{file}; | |||||||||||||||||||||||||||||||||||||||||||||
| 582 | 1 | ▶ | return undef if (!$tied->opened() || !fcwrite($flags) || !$tied->close); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$tied->opened and fcwrite($flags) and $tied->close
$tied->opened and fcwrite($flags)
MC/DC0%0/3
$tied->opened and fcwrite($flags) and $tied->close
$tied->opened
fcwrite($flags)
$tied->close
Truth table25%1/4
A and B and C
A= $tied->openedB= fcwrite($flags)C= $tied->close
| |||||||||||||||||||||||||||||||||||||||||||||||
| 583 | 1 | foreach ('','.idx','.hdr') { | |||||||||||||||||||||||||||||||||||||||||||||
| 584 | 3 | ▶ | File::Copy::move("${oldfile}$_","${newfile}$_") or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 585 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 586 | 1 | return $tied->open($newfile, ($flags & ~O_TRUNC)); | |||||||||||||||||||||||||||||||||||||||||||||
| 587 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||||||||||||
| 591 | ##-------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 592 | ## Object API: consolidate | ||||||||||||||||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||||||||||||||||
| 594 | ## $tied_or_undef = $tied->consolidate() | ||||||||||||||||||||||||||||||||||||||||||||||
| 595 | ## $tied_or_undef = $tied->consolidate($tmpfile) | ||||||||||||||||||||||||||||||||||||||||||||||
| 596 | ## + consolidates file data: ensures data in $tied->{datfh} are in index-order and contain no gaps or unused blocks | ||||||||||||||||||||||||||||||||||||||||||||||
| 597 | ## + object must be opened in write-mode | ||||||||||||||||||||||||||||||||||||||||||||||
| 598 | ## + uses $tmpfile as a temporary file for consolidation (default="$tied->{file}.tmp") | ||||||||||||||||||||||||||||||||||||||||||||||
| 599 | 1 | ▶ | sub consolidate { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
consolidate: called CC=18 SCAR=31.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 600 | 1 | my ($tied,$tmpfile) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||||||||||||||||
| 602 | ##-- open tempfile | ||||||||||||||||||||||||||||||||||||||||||||||
| 603 | 1 | ▶ | $tmpfile //= "$tied->{file}.tmp"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$tmpfile //= qq[$tied->{"file"}.tmp]
MC/DC0%0/1
$tmpfile //= qq[$tied->{"file"}.tmp]
$tmpfile
Truth table50%1/2
A
A= $tmpfile
| |||||||||||||||||||||||||||||||||||||||||||||||
| 604 | my $tmpfh = fcopen($tmpfile, $tied->{mode}, $tied->{perms}) | ||||||||||||||||||||||||||||||||||||||||||||||
| 605 | 1 | ▶ | or confess(ref($tied)."::open failed for temporary data-file $tmpfile: $!"); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 606 | 1 | binmode($tmpfh); | |||||||||||||||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||||||||||||||||
| 608 | ##-- copy data | ||||||||||||||||||||||||||||||||||||||||||||||
| 609 | 1 | my ($file,$idxfh,$datfh,$len_ix,$pack_ix) = @$tied{qw(file idxfh datfh len_ix pack_ix)}; | |||||||||||||||||||||||||||||||||||||||||||||
| 610 | 1 | my ($buf,$off,$len); | |||||||||||||||||||||||||||||||||||||||||||||
| 611 | 1 | my $size = $tied->size; | |||||||||||||||||||||||||||||||||||||||||||||
| 612 | 1 | ▶ | CORE::seek($idxfh, 0, SEEK_SET) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 613 | 1 | ▶ | CORE::seek($tmpfh, 0, SEEK_SET) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 614 | 1 | ▶ | for (my $i=0; $i < $size; ++$i) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 615 | 3 | ▶ | CORE::read($idxfh, $buf, $len_ix)==$len_ix or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 616 | 3 | ($off,$len) = unpack($pack_ix, $buf); | |||||||||||||||||||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||||||||||||||||||||
| 618 | ##-- update index record (in-place) | ||||||||||||||||||||||||||||||||||||||||||||||
| 619 | 3 | ▶ | CORE::seek($idxfh, $i*$len_ix, SEEK_SET) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 620 | 3 | ▶ | $idxfh->print(pack($pack_ix, CORE::tell($tmpfh),$len)) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||||||||||||||||||||
| 622 | ##-- copy data record | ||||||||||||||||||||||||||||||||||||||||||||||
| 623 | 3 | ▶ | next if ($len == 0); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 624 | 3 | ▶ | CORE::seek($datfh, $off, SEEK_SET) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 625 | 3 | ▶ | CORE::read($datfh, $buf, $len)==$len or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 626 | 3 | ▶ | $tmpfh->print($buf) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 627 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||||||||||||||||||||
| 629 | ##-- close data-filehandles | ||||||||||||||||||||||||||||||||||||||||||||||
| 630 | 1 | ▶ | CORE::close($tmpfh) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 631 | or confess(ref($tied)."::consolidate(): failed to close temp-file '$tmpfile': $!"); | ||||||||||||||||||||||||||||||||||||||||||||||
| 632 | 1 | ▶ | CORE::close($datfh) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 633 | or confess(ref($tied)."::consolidate(): failed to close old data-file '$file': $!"); | ||||||||||||||||||||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||||||||||||||||||||
| 635 | ##-- replace old datafile | ||||||||||||||||||||||||||||||||||||||||||||||
| 636 | 1 | undef $tmpfh; | |||||||||||||||||||||||||||||||||||||||||||||
| 637 | 1 | undef $datfh; | |||||||||||||||||||||||||||||||||||||||||||||
| 638 | 1 | delete $tied->{datfh}; | |||||||||||||||||||||||||||||||||||||||||||||
| 639 | 1 | ▶ | CORE::unlink($file) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 640 | or confess(ref($tied)."::consolidate(): failed to unlink old data-file '$tied->{file}': $!"); | ||||||||||||||||||||||||||||||||||||||||||||||
| 641 | #CORE::rename($tmpfile, $file) ##-- win32 chokes here with "Permission denied" | ||||||||||||||||||||||||||||||||||||||||||||||
| 642 | 1 | ▶ | File::Copy::move($tmpfile, $file) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 643 | or confess(ref($tied)."::consolidate(): failed to move temp-file '$tmpfile' to '$file': $!"); | ||||||||||||||||||||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||||||||||||||||||||
| 645 | ##-- re-open | ||||||||||||||||||||||||||||||||||||||||||||||
| 646 | $tied->{datfh} = fcopen("$file", (fcflags($tied->{mode}) & ~O_TRUNC), $tied->{perms}) | ||||||||||||||||||||||||||||||||||||||||||||||
| 647 | 1 | ▶ | or confess(ref($tied)."::consolidate(): failed to re-open data-file $file: $!"); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||||||||||||||||||||
| 649 | 1 | return $tied; | |||||||||||||||||||||||||||||||||||||||||||||
| 650 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||||||||||||||||||||
| 652 | ##-------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 653 | ## Object API: advisory locking | ||||||||||||||||||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||||||||||||||||
| 655 | ## $bool = $tied->flock() | ||||||||||||||||||||||||||||||||||||||||||||||
| 656 | ## $bool = $tied->flock($lock) | ||||||||||||||||||||||||||||||||||||||||||||||
| 657 | ## + get an advisory lock of type $lock (default=LOCK_EX) on $tied->{datfh}, using perl's flock() function | ||||||||||||||||||||||||||||||||||||||||||||||
| 658 | ## + implicitly calls flush() prior to locking | ||||||||||||||||||||||||||||||||||||||||||||||
| 659 | 0 | ▶ | sub flock { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
flock: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 660 | 0 | my ($tied,$op) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 661 | 0 | ▶ | return undef if (!$tied->opened); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 662 | 0 | $tied->flush(); | |||||||||||||||||||||||||||||||||||||||||||||
| 663 | 0 | ▶ | return CORE::flock($tied->{datfh}, ($op // LOCK_EX)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$op // 2
MC/DC0%0/1
$op // 2
$op
Truth table0%0/2
A
A= $op
| |||||||||||||||||||||||||||||||||||||||||||||||
| 664 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||||||||||||||||||||
| 666 | ## $bool = $tied->funlock() | ||||||||||||||||||||||||||||||||||||||||||||||
| 667 | ## $bool = $tied->funlock($lock) | ||||||||||||||||||||||||||||||||||||||||||||||
| 668 | ## + unlock $tied->{datfh} using perl's flock() function; $lock defaults to LOCK_UN | ||||||||||||||||||||||||||||||||||||||||||||||
| 669 | 0 | ▶ | sub funlock { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
funlock: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 670 | 0 | ▶ | return $_[0]->flock( LOCK_UN | ($_[1]//0) ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$_[1] // 0
MC/DC0%0/1
$_[1] // 0
$_[1]
Truth table0%0/2
A
A= $_[1]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 671 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||||||||||||||||||||
| 674 | ##====================================================================== | ||||||||||||||||||||||||||||||||||||||||||||||
| 675 | ## API: Tied Array | ||||||||||||||||||||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||||||||||||||||||||
| 677 | ##-------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 678 | ## API: Tied Array: mandatory methods | ||||||||||||||||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||||||||||||||||
| 680 | ## $tied = tie(@array, $tieClass, $file,%opts) | ||||||||||||||||||||||||||||||||||||||||||||||
| 681 | ## $tied = TIEARRAY($tieClass, $file,%opts) | ||||||||||||||||||||||||||||||||||||||||||||||
| 682 | 5 | ▶ | BEGIN { *TIEARRAY = \&new; } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||||||||||||||||||||
| 684 | ## $count = $tied->FETCHSIZE() | ||||||||||||||||||||||||||||||||||||||||||||||
| 685 | ## + like scalar(@array) | ||||||||||||||||||||||||||||||||||||||||||||||
| 686 | ## + re-positions $tied->{idxfh} to eof | ||||||||||||||||||||||||||||||||||||||||||||||
| 687 | 5 | ▶ | BEGIN { *size = \&FETCHSIZE; } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 688 | 288 | ▶ | sub FETCHSIZE { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
FETCHSIZE: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||||||||||||||||||||
| 689 | 288 | ▶ | return undef if (!$_[0]{idxfh}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 690 | #return ((-s $_[0]{idxfh}) / $_[0]{len_ix}); ##-- doesn't handle recent writes correctly (probably due to perl i/o buffering) | ||||||||||||||||||||||||||||||||||||||||||||||
| 691 | ## | ||||||||||||||||||||||||||||||||||||||||||||||
| 692 | 288 | ▶ | CORE::seek($_[0]{idxfh},0,SEEK_END) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 693 | 288 | return CORE::tell($_[0]{idxfh}) / $_[0]{len_ix}; | |||||||||||||||||||||||||||||||||||||||||||||
| 694 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||||||||||||||||
| 696 | ## $val = $tied->FETCH($index) | ||||||||||||||||||||||||||||||||||||||||||||||
| 697 | ## $val = $tied->FETCH($index) | ||||||||||||||||||||||||||||||||||||||||||||||
| 698 | 131 | ▶ | sub FETCH { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=4 SCAR=14.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 699 | ##-- sanity check | ||||||||||||||||||||||||||||||||||||||||||||||
| 700 | 131 | ▶ | return undef if ($_[1] >= $_[0]->size); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 701 | |||||||||||||||||||||||||||||||||||||||||||||||
| 702 | ##-- get index record from $idxfh | ||||||||||||||||||||||||||||||||||||||||||||||
| 703 | 131 | ▶ | my ($off,$len) = $_[0]->readIndex($_[1]) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||||||||||||||||||||
| 705 | ##-- get data record from $datfh | ||||||||||||||||||||||||||||||||||||||||||||||
| 706 | 131 | ▶ | CORE::seek($_[0]{datfh}, $off, SEEK_SET) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 707 | 131 | return $_[0]->readData($len); | |||||||||||||||||||||||||||||||||||||||||||||
| 708 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||||||||||||||||||||
| 710 | ## $val = $tied->STORE($index,$val) | ||||||||||||||||||||||||||||||||||||||||||||||
| 711 | ## + no consistency checking or optimization; just appends a new record to the end of $datfh and updates $idxfh | ||||||||||||||||||||||||||||||||||||||||||||||
| 712 | 74 | ▶ | sub STORE { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 713 | ##-- append encoded record to $datfh | ||||||||||||||||||||||||||||||||||||||||||||||
| 714 | 74 | ▶ | CORE::seek($_[0]{datfh}, 0, SEEK_END) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 715 | 74 | my $off0 = CORE::tell($_[0]{datfh}); | |||||||||||||||||||||||||||||||||||||||||||||
| 716 | 74 | ▶ | $_[0]->writeData($_[2]) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 717 | 74 | my $off1 = CORE::tell($_[0]{datfh}); | |||||||||||||||||||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||||||||||||||||||||
| 719 | ##-- update index record in $idxfh | ||||||||||||||||||||||||||||||||||||||||||||||
| 720 | 74 | ▶ | $_[0]->writeIndex($_[1], $off0, ($off1-$off0)) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||||||||||||||||||||
| 722 | ##-- return | ||||||||||||||||||||||||||||||||||||||||||||||
| 723 | 74 | return $_[2]; | |||||||||||||||||||||||||||||||||||||||||||||
| 724 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||||||||||||||||||||
| 726 | ## $count = $tied->STORESIZE($count) | ||||||||||||||||||||||||||||||||||||||||||||||
| 727 | ## $count = $tied->STORESIZE($count) ##-- local extension | ||||||||||||||||||||||||||||||||||||||||||||||
| 728 | ## + modifies only $idxfh | ||||||||||||||||||||||||||||||||||||||||||||||
| 729 | 0 | ▶ | sub STORESIZE { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
STORESIZE: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 730 | 0 | my $oldsize = $_[0]->size; | |||||||||||||||||||||||||||||||||||||||||||||
| 731 | 0 | ▶ | if ($_[1] < $oldsize) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 732 | ##-- shrink | ||||||||||||||||||||||||||||||||||||||||||||||
| 733 | 0 | ▶ | CORE::truncate($_[0]{idxfh}, $_[1]*$_[0]{len_ix}) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 734 | } elsif ($_[1] > $oldsize) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 735 | ##-- grow (idxfh only) | ||||||||||||||||||||||||||||||||||||||||||||||
| 736 | 0 | ▶ | CORE::seek($_[0]{idxfh}, $_[1]*$_[0]{len_ix}-1, SEEK_SET) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 737 | 0 | $_[0]{idxfh}->print("\0"); | |||||||||||||||||||||||||||||||||||||||||||||
| 738 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 739 | 0 | return $_[1]; | |||||||||||||||||||||||||||||||||||||||||||||
| 740 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 741 | |||||||||||||||||||||||||||||||||||||||||||||||
| 742 | ## $bool = $tied->EXISTS($index) | ||||||||||||||||||||||||||||||||||||||||||||||
| 743 | 0 | ▶ | sub EXISTS { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
EXISTS: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 744 | 0 | return $_[1] < $_[0]->size; | |||||||||||||||||||||||||||||||||||||||||||||
| 745 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||||||||||||||||||||
| 747 | ## undef = $tied->DELETE($index) | ||||||||||||||||||||||||||||||||||||||||||||||
| 748 | ## + really just wraps $tied->STORE($index,undef) | ||||||||||||||||||||||||||||||||||||||||||||||
| 749 | 0 | ▶ | sub DELETE { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DELETE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 750 | 0 | return $_[0]->STORE($_[1],undef); | |||||||||||||||||||||||||||||||||||||||||||||
| 751 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||||||||||||||||||||
| 753 | ##-------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 754 | ## API: Tied Array: optional methods | ||||||||||||||||||||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||||||||||||||||||||
| 756 | ## undef = $tied->CLEAR() | ||||||||||||||||||||||||||||||||||||||||||||||
| 757 | 10 | ▶ | sub CLEAR { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
CLEAR: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||||||||||||||||||||
| 758 | 10 | ▶ | CORE::truncate($_[0]{idxfh}, 0) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 759 | 10 | ▶ | CORE::truncate($_[0]{datfh}, 0) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 760 | 10 | return $_[0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 761 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 762 | |||||||||||||||||||||||||||||||||||||||||||||||
| 763 | ## $newsize = $tied->PUSH(@vals) | ||||||||||||||||||||||||||||||||||||||||||||||
| 764 | 1 | ▶ | sub PUSH { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
PUSH: called CC=7 SCAR=20.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 765 | 1 | my $tied = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||||||||||||||||||||
| 767 | 1 | ▶ | CORE::seek($tied->{datfh}, 0, SEEK_END) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 768 | 1 | ▶ | CORE::seek($tied->{idxfh}, 0, SEEK_END) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 769 | 1 | my ($off0,$off1); | |||||||||||||||||||||||||||||||||||||||||||||
| 770 | 1 | foreach (@_) { | |||||||||||||||||||||||||||||||||||||||||||||
| 771 | 1 | my $off0 = CORE::tell($tied->{datfh}); | |||||||||||||||||||||||||||||||||||||||||||||
| 772 | 1 | ▶ | $tied->writeData($_) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 773 | 1 | my $off1 = CORE::tell($tied->{datfh}); | |||||||||||||||||||||||||||||||||||||||||||||
| 774 | |||||||||||||||||||||||||||||||||||||||||||||||
| 775 | ##-- update index record in $idxfh | ||||||||||||||||||||||||||||||||||||||||||||||
| 776 | 1 | ▶ | $tied->writeIndex(undef, $off0, ($off1-$off0)) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 777 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||||||||||||||||||||
| 779 | 1 | ▶ | return $tied->size if (defined(wantarray)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 780 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 781 | |||||||||||||||||||||||||||||||||||||||||||||||
| 782 | ## $val = $tied->POP() | ||||||||||||||||||||||||||||||||||||||||||||||
| 783 | ## + truncates data-file if we're popping the final data-record | ||||||||||||||||||||||||||||||||||||||||||||||
| 784 | 1 | ▶ | sub POP { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
POP: called CC=6 SCAR=19.3
| |||||||||||||||||||||||||||||||||||||||||||||||
| 785 | 1 | ▶ | return undef if (!(my $size=$_[0]->size)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||||||||||||||||||||
| 787 | ##-- get final index record (& truncate it) | ||||||||||||||||||||||||||||||||||||||||||||||
| 788 | 1 | ▶ | my ($off,$len) = $_[0]->readIndex($size-1) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 789 | 1 | ▶ | CORE::truncate($_[0]{idxfh}, ($size-1)*$_[0]{len_ix}) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 790 | |||||||||||||||||||||||||||||||||||||||||||||||
| 791 | ##-- get corresponding data-record | ||||||||||||||||||||||||||||||||||||||||||||||
| 792 | 1 | ▶ | CORE::seek($_[0]{datfh}, $off, SEEK_SET) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 793 | 1 | my $val = $_[0]->readData($len); | |||||||||||||||||||||||||||||||||||||||||||||
| 794 | |||||||||||||||||||||||||||||||||||||||||||||||
| 795 | ##-- maybe trim data-file | ||||||||||||||||||||||||||||||||||||||||||||||
| 796 | 1 | ▶ | CORE::truncate($_[0]{datfh}, $off) if (($off+$len) == (-s $_[0]{datfh})); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 797 | 1 | return $val; | |||||||||||||||||||||||||||||||||||||||||||||
| 798 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 799 | |||||||||||||||||||||||||||||||||||||||||||||||
| 800 | ## $val = $tied->SHIFT() | ||||||||||||||||||||||||||||||||||||||||||||||
| 801 | ## + truncates data-file if we're shifting the final data-record | ||||||||||||||||||||||||||||||||||||||||||||||
| 802 | 1 | ▶ | sub SHIFT { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
SHIFT: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 803 | ##-- get first index record | ||||||||||||||||||||||||||||||||||||||||||||||
| 804 | 1 | ▶ | my ($off,$len) = $_[0]->readIndex(0) or return undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 805 | |||||||||||||||||||||||||||||||||||||||||||||||
| 806 | ##-- defer to SPLICE | ||||||||||||||||||||||||||||||||||||||||||||||
| 807 | 1 | my $val = $_[0]->SPLICE(0,1); | |||||||||||||||||||||||||||||||||||||||||||||
| 808 | |||||||||||||||||||||||||||||||||||||||||||||||
| 809 | ##-- maybe trim data-file | ||||||||||||||||||||||||||||||||||||||||||||||
| 810 | 1 | ▶ | CORE::truncate($_[0]{datfh}, $off) if (($off+$len) == (-s $_[0]{datfh})); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 811 | 1 | return $val; | |||||||||||||||||||||||||||||||||||||||||||||
| 812 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 813 | |||||||||||||||||||||||||||||||||||||||||||||||
| 814 | ## @removed = $tied->SPLICE($offset, $length, @newvals) | ||||||||||||||||||||||||||||||||||||||||||||||
| 815 | ## $last_removed = $tied->SPLICE($offset, $length, @newvals) | ||||||||||||||||||||||||||||||||||||||||||||||
| 816 | 4 | ▶ | sub SPLICE { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
SPLICE: called CC=13 SCAR=26.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 817 | 4 | my $tied = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 818 | 4 | my $size = $tied->size(); | |||||||||||||||||||||||||||||||||||||||||||||
| 819 | 4 | ▶ | my $off = (@_) ? shift : 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 820 | 4 | ▶ | $off += $size if ($off < 0); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 821 | 4 | ▶ | my $len = (@_) ? shift : ($size-$off); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 822 | 4 | ▶ | $len += $size-$off if ($len < 0); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 823 | |||||||||||||||||||||||||||||||||||||||||||||||
| 824 | ##-- get result-list | ||||||||||||||||||||||||||||||||||||||||||||||
| 825 | 4 | my ($i,@result); | |||||||||||||||||||||||||||||||||||||||||||||
| 826 | 4 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 827 | 3 | ▶ | for ($i=$off; $i <= $len; ++$i) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 828 | 4 | push(@result, $tied->FETCH($i)); | |||||||||||||||||||||||||||||||||||||||||||||
| 829 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 830 | } elsif ($len > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 831 | 1 | @result = ($tied->FETCH($off+$len-1)); | |||||||||||||||||||||||||||||||||||||||||||||
| 832 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 833 | |||||||||||||||||||||||||||||||||||||||||||||||
| 834 | ##-- shift post-splice index records (expensive, but generally not so bad as default Tie::Array iterated FETCH()+STORE()) | ||||||||||||||||||||||||||||||||||||||||||||||
| 835 | 4 | my $shift = scalar(@_) - $len; | |||||||||||||||||||||||||||||||||||||||||||||
| 836 | 4 | ▶ | $tied->shiftIndex($off+$len, $size-($off+$len), $shift) if ($shift != 0); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 837 | |||||||||||||||||||||||||||||||||||||||||||||||
| 838 | ##-- store new values | ||||||||||||||||||||||||||||||||||||||||||||||
| 839 | 4 | ▶ | for ($i=0; $i < @_; ++$i) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 840 | 4 | $tied->STORE($off+$i, $_[$i]); | |||||||||||||||||||||||||||||||||||||||||||||
| 841 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 842 | |||||||||||||||||||||||||||||||||||||||||||||||
| 843 | ##-- maybe shrink array | ||||||||||||||||||||||||||||||||||||||||||||||
| 844 | 4 | ▶ | CORE::truncate($tied->{idxfh}, ($size+$shift)*$tied->{len_ix}) or return undef if ($shift < 0); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
$shift < 0 && (truncate($tied->{'idxfh'}, ($size + $shift) * $tied->{'len_ix'}) || (return undef))
truncate $tied->{"idxfh"}, ($size + $shift) * $tied->{"len_ix"} or return undef
MC/DC0%0/2
$shift < 0 && (truncate($tied->{'idxfh'}, ($size + $shift) * $tied->{'len_ix'}) || (return undef))
$shift < 0
truncate $tied->{"idxfh"}, ($size + $shift) * $tied->{"len_ix"}
Truth table33%1/3
A && B
A= $shift < 0B= truncate $tied->{"idxfh"}, ($size + $shift) * $tied->{"len_ix"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 845 | |||||||||||||||||||||||||||||||||||||||||||||||
| 846 | ##-- return | ||||||||||||||||||||||||||||||||||||||||||||||
| 847 | 4 | ▶ | return wantarray ? @result : $result[0]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 848 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 849 | |||||||||||||||||||||||||||||||||||||||||||||||
| 850 | ## @vals = $tied->UNSHIFT(@vals) | ||||||||||||||||||||||||||||||||||||||||||||||
| 851 | ## + just defers to SPLICE | ||||||||||||||||||||||||||||||||||||||||||||||
| 852 | 0 | ▶ | sub UNSHIFT { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
UNSHIFT: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 853 | 0 | return scalar shift->SPLICE(0,0,@_); | |||||||||||||||||||||||||||||||||||||||||||||
| 854 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 855 | |||||||||||||||||||||||||||||||||||||||||||||||
| 856 | ## ? = $tied->EXTEND($newcount) | ||||||||||||||||||||||||||||||||||||||||||||||
| 857 | |||||||||||||||||||||||||||||||||||||||||||||||
| 858 | 5 | 1; ##-- be happpy | |||||||||||||||||||||||||||||||||||||||||||||