| 1 | 3 | ▶ | use 5.10.0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | package Tie::FileSection; | ||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | $Tie::FileSection::VERSION = '0.171950'; | |||||||||||||||||||||||||||||||||||||||||
| 5 | # ABSTRACT: restrict files sequential access using array like boundaries | ||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | require Tie::Handle; | |||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | our @ISA = qw( Tie::StdHandle ); | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 28 | ▶ | sub new{ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=9 SCAR=22.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 28 | ▶ | my $pkg = $_[0] eq __PACKAGE__ ? shift : __PACKAGE__ ; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 28 | my %opts = @_; | |||||||||||||||||||||||||||||||||||||||||
| 12 | 28 | ▶ | $opts{filename} || $opts{file} or die "filename|file parameter is mandatory!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opts{"filename"} or $opts{"file"}
MC/DC0%0/2
$opts{"filename"} or $opts{"file"}
$opts{"filename"}
$opts{"file"}
Truth table66%2/3
A or B
A= $opts{"filename"}B= $opts{"file"}
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 28 | ▶ | my $first_line = $opts{first_line} // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"first_line"} // 0
MC/DC100%1/1
$opts{"first_line"} // 0
$opts{"first_line"}
Truth table100%2/2
A
A= $opts{"first_line"}
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 28 | ▶ | my $last_line = $opts{last_line} // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"last_line"} // 0
MC/DC100%1/1
$opts{"last_line"} // 0
$opts{"last_line"}
Truth table100%2/2
A
A= $opts{"last_line"}
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 28 | my $use_real_line_nr = $opts{use_real_line_nr}; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 28 | my $FH = $opts{file}; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 28 | ▶ | if(!$FH && defined $opts{filename}){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $FH and defined $opts{"filename"}
MC/DC50%1/2
not $FH and defined $opts{"filename"}
not $FH
defined $opts{"filename"}
Truth table66%2/3
not(A) and B
A= not $FHB= defined $opts{"filename"}
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 12 | ▶ | open $FH, '<', $opts{filename} or die "** could not open file $opts{filename} : $!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 28 | tie *F, $pkg, $FH, $first_line, $last_line, $use_real_line_nr; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 28 | return \*F; | |||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 28 | ▶ | sub TIEHANDLE{ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
TIEHANDLE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 28 | my ($pkg, $FH, $first_line, $last_line, $use_real_line_nr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 28 | my $self = bless { | |||||||||||||||||||||||||||||||||||||||||
| 27 | handle => $FH, | ||||||||||||||||||||||||||||||||||||||||||
| 28 | first_line => $first_line, | ||||||||||||||||||||||||||||||||||||||||||
| 29 | last_line => $last_line, | ||||||||||||||||||||||||||||||||||||||||||
| 30 | init => 0, #lazy read | ||||||||||||||||||||||||||||||||||||||||||
| 31 | curr_line => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | use_real_line_nr => $use_real_line_nr, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | line_buffer => [], | ||||||||||||||||||||||||||||||||||||||||||
| 34 | tell_buffer => [], | ||||||||||||||||||||||||||||||||||||||||||
| 35 | }, $pkg; | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 28 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub UNTIE{ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
UNTIE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | my $fh = $_[0]->{handle}; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | undef $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | close( $fh ); | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 111 | ▶ | sub EOF{ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
EOF: called CC=16 SCAR=27.9
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 111 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 111 | my $f = $self->{first_line}; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 111 | my $l = $self->{last_line}; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 111 | ▶ | if($f>=0 && $l>0 && $f > $l){ #static EOF | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$f >= 0 and $l > 0 and $f > $l
$f >= 0 and $l > 0
MC/DC0%0/3
$f >= 0 and $l > 0 and $f > $l
$f >= 0
$l > 0
$f > $l
Truth table75%3/4
A and B and C
A= $f >= 0B= $l > 0C= $f > $l
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 111 | ▶ | if($f<0 && $l<0 && $l < $f ){ #static EOF | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$f < 0 and $l < 0 and $l < $f
$f < 0 and $l < 0
MC/DC0%0/3
$f < 0 and $l < 0 and $l < $f
$f < 0
$l < 0
$l < $f
Truth table75%3/4
A and B and C
A= $f < 0B= $l < 0C= $l < $f
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 111 | ▶ | if($f<0 && $l>0){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$f < 0 and $l > 0
MC/DC100%2/2
$f < 0 and $l > 0
$f < 0
$l > 0
Truth table100%3/3
A and B
A= $f < 0B= $l > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 16 | return abs($f) + $self->{curr_line} >= $l; | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 95 | ▶ | if($self->{init} && 0 <= $l && $l >= $self->{curr_line}){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$self->{"init"} and 0 <= $l and $l >= $self->{"curr_line"}
$self->{"init"} and 0 <= $l
MC/DC100%3/3
$self->{"init"} and 0 <= $l and $l >= $self->{"curr_line"}
$self->{"init"}
0 <= $l
$l >= $self->{"curr_line"}
Truth table100%4/4
A and B and C
A= $self->{"init"}B= 0 <= $lC= $l >= $self->{"curr_line"}
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 6 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 89 | ▶ | if(eof( $self->{handle} )){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | #take in account buffer here | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 33 | ▶ | if($l < 0 && scalar(@{$self->{line_buffer}})<abs($l)){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$l < 0 and scalar @{$self->{"line_buffer"};} < abs $l
MC/DC100%2/2
$l < 0 and scalar @{$self->{"line_buffer"};} < abs $l
$l < 0
scalar @{$self->{"line_buffer"};} < abs $l
Truth table100%3/3
A and B
A= $l < 0B= scalar @{$self->{"line_buffer"};} < abs $l
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 12 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | else{ | ||||||||||||||||||||||||||||||||||||||||||
| 70 | #buffer not empty | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 21 | ▶ | return if @{$self->{line_buffer}}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 12 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 56 | return; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 28 | ▶ | sub TELL { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
TELL: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 28 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 28 | $. = $self->{curr_line}; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 28 | ▶ | return tell($self->{handle}) unless $self->{use_buffer}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 16 | return $self->{tell_buffer}[0]; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 63 | ▶ | sub _readline{ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_readline: called CC=20 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 63 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 63 | my $fh = $self->{handle}; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 63 | my $l = $self->{last_line}; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 63 | my $tellbuff = $self->{tell_buffer}; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 63 | my $linebuff = $self->{line_buffer}; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 63 | ▶ | unless($self->{init}++){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 28 | my $f = $self->{first_line}; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 28 | ▶ | if($f > 0){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 10 | my $i = $f; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 10 | ▶ | while(--$i && defined scalar <$fh>){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
--$i and defined scalar readline $fh
MC/DC50%1/2
--$i and defined scalar readline $fh
--$i
defined scalar readline $fh
Truth table66%2/3
A and B
A= --$iB= defined scalar readline $fh
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | elsif($f < 0){ | ||||||||||||||||||||||||||||||||||||||||||
| 100 | #need to read until eof for abs($f) records | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 14 | for(1..abs $f){ | |||||||||||||||||||||||||||||||||||||||||
| 102 | 31 | push @$tellbuff, tell($fh); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 31 | push @$linebuff, scalar <$fh>; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 14 | $self->{use_buffer}++; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 14 | ▶ | while(!eof $fh){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 41 | shift @$tellbuff; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 41 | shift @$linebuff; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 41 | push @$tellbuff, tell($fh); | |||||||||||||||||||||||||||||||||||||||||
| 110 | 41 | push @$linebuff, scalar <$fh>; | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 28 | ▶ | if($f > 0 && $l < 0){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$f > 0 and $l < 0
MC/DC100%2/2
$f > 0 and $l < 0
$f > 0
$l < 0
Truth table100%3/3
A and B
A= $f > 0B= $l < 0
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 4 | for(1..abs $l){ | |||||||||||||||||||||||||||||||||||||||||
| 115 | 8 | push @$tellbuff, tell($fh); | |||||||||||||||||||||||||||||||||||||||||
| 116 | 8 | push @$linebuff, scalar <$fh>; | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 4 | $self->{use_buffer}++; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 28 | ▶ | if(eof($fh)){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | #add the final pos if requested aftere EOF. | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 14 | push @$tellbuff, tell($fh); | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 28 | ▶ | if($self->{use_real_line_nr}){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 3 | ▶ | $. -= @$linebuff if $self->{use_buffer}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 3 | $self->{curr_line} = $.; | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 25 | $. = undef; | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | #read one line and return it, take in accound first_line/last_line and buffer | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 63 | my $eof = eof($fh); | |||||||||||||||||||||||||||||||||||||||||
| 134 | 63 | my $pos = tell($fh); | |||||||||||||||||||||||||||||||||||||||||
| 135 | 63 | ▶ | my $line = $eof ? undef : <$fh>; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 63 | ▶ | if($self->{use_buffer}){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 35 | ▶ | unless($eof){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 8 | push @$linebuff, $line; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 8 | push @$tellbuff, $pos; | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | elsif($l < 0 && scalar(@$linebuff)<abs($l)){ | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$l < 0 && scalar @$linebuff < abs $l
MC/DC0%0/2
$l < 0 && scalar @$linebuff < abs $l
$l < 0
scalar @$linebuff < abs $l
Truth table66%2/3
A && B
A= $l < 0B= scalar @$linebuff < abs $l
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 35 | $line = shift @$linebuff; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 35 | ▶ | shift @$tellbuff unless @$tellbuff == 1; #always keep last pos | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | 63 | $self->{curr_line}++; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 63 | $. = $self->{curr_line}; | |||||||||||||||||||||||||||||||||||||||||
| 149 | 63 | return $line; | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 99 | ▶ | sub READLINE { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
READLINE: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 99 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 154 | 99 | ▶ | return if $self->EOF; #test basics boundaries | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 75 | ▶ | unless(wantarray){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 63 | return $self->_readline; | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | #ARRAY | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 12 | my @rows; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 12 | ▶ | while(defined($_=$self->READLINE)){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 28 | push @rows, $_; | |||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 12 | @rows; | |||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 12 | ▶ | sub CLOSE { close($_[0]->{handle}) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
CLOSE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | sub FILENO { fileno($_[0]->{handle}) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
FILENO: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | __END__ | ||||||||||||||||||||||||||||||||||||||||||