blib/lib/Tie/FileSection.pm

filter: Statementstmt 92.2% 95 / 103 Branchbran 87.9% 51 / 58 Conditioncond 86.0% 37 / 43 MC/DCmcdc 52.0% 13 / 25 Subroutinesub 81.8% 9 / 11 Podpod 100% 1 / 1 totaltotal 85.4% 206 / 241 CC 47 SCAR 26.1
CC
47
Cov
90%
_readline
20.2
EOF
16.3
new
9.4
CRAP 13.6
↑ Summary
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
BranchTrueFalseTotal
$_[0] eq __PACKAGE__ ? : 28 0 28
11 28 my %opts = @_;
12 28 ▶ $opts{filename} || $opts{file} or die "filename|file parameter is mandatory!";
Branch: 1 branch
BranchTrueFalseTotal
$opts{"filename"} or $opts{"file"} or die "filename|file parameter is mandatory!" 28 0 28
Condition66%2/3
$opts{"filename"} or $opts{"file"}
l!l&&r!l&&!r
12160
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"}
ABresult
1X1
011
000
13 28 ▶ my $first_line = $opts{first_line} // 0;
Condition100%2/2
$opts{"first_line"} // 0
l!l
244
MC/DC100%1/1
$opts{"first_line"} // 0
$opts{"first_line"}
Truth table100%2/2
A
A= $opts{"first_line"}
Aresult
11
00
14 28 ▶ my $last_line = $opts{last_line} // 0;
Condition100%2/2
$opts{"last_line"} // 0
l!l
1612
MC/DC100%1/1
$opts{"last_line"} // 0
$opts{"last_line"}
Truth table100%2/2
A
A= $opts{"last_line"}
Aresult
11
00
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
BranchTrueFalseTotal
if (not $FH and defined $opts{"filename"}) 12 16 28
Condition66%2/3
not $FH and defined $opts{"filename"}
!ll&&!rl&&r
16012
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"}
ABresult
0X0
100
111
18 12 ▶ open $FH, '<', $opts{filename} or die "** could not open file $opts{filename} : $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $FH, "<", $opts{"filename"} or die qq[** could not open file $opts{"filename"} : $!\n] 12 0 12
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
BranchTrueFalseTotal
if ($f >= 0 and $l > 0 and $f > $l) 0 111 111
Condition83%5/6
$f >= 0 and $l > 0 and $f > $l
!ll&&!rl&&r
99120
$f >= 0 and $l > 0
!ll&&!rl&&r
475212
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
ABCresult
0XX0
10X0
1100
1111
50 0 return 1;
51 }
52 111 ▶ if($f<0 && $l<0 && $l < $f ){ #static EOF
Branch: 1 branch
BranchTrueFalseTotal
if ($f < 0 and $l < 0 and $l < $f) 0 111 111
Condition83%5/6
$f < 0 and $l < 0 and $l < $f
!ll&&!rl&&r
95160
$f < 0 and $l < 0
!ll&&!rl&&r
643116
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
ABCresult
0XX0
10X0
1100
1111
53 0 return 1;
54 }
55
56 111 ▶ if($f<0 && $l>0){
Branch: 1 branch
BranchTrueFalseTotal
if ($f < 0 and $l > 0) 16 95 111
Condition100%3/3
$f < 0 and $l > 0
!ll&&!rl&&r
643116
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($self->{"init"} and 0 <= $l and $l >= $self->{"curr_line"}) 6 89 95
Condition100%6/6
$self->{"init"} and 0 <= $l and $l >= $self->{"curr_line"}
!ll&&!rl&&r
58316
$self->{"init"} and 0 <= $l
!ll&&!rl&&r
342437
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"}
ABCresult
0XX0
10X0
1100
1111
61 6 return 1;
62 }
63
64 89 ▶ if(eof( $self->{handle} )){
Branch: 1 branch
BranchTrueFalseTotal
if (eof $self->{"handle"}) 33 56 89
65 #take in account buffer here
66 33 ▶ if($l < 0 && scalar(@{$self->{line_buffer}})<abs($l)){
Branch: 1 branch
BranchTrueFalseTotal
if ($l < 0 and scalar @{$self->{'line_buffer'};} < abs $l) { } 12 21 33
Condition100%3/3
$l < 0 and scalar @{$self->{"line_buffer"};} < abs $l
!ll&&!rl&&r
13812
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
ABresult
0X0
100
111
67 12 return 1;
68 }
69 else{
70 #buffer not empty
71 21 ▶ return if @{$self->{line_buffer}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"line_buffer"};} 9 12 21
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
BranchTrueFalseTotal
unless $self->{"use_buffer"} 12 16 28
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
BranchTrueFalseTotal
unless ($self->{"init"}++) 28 35 63
93 28 my $f = $self->{first_line};
94 28 ▶ if($f > 0){
Branch: 2 branches
BranchTrueFalseTotal
if ($f > 0) { } 10 18 28
elsif ($f < 0) { } 14 4 18
95 10 my $i = $f;
96 10 ▶ while(--$i && defined scalar <$fh>){
Branch: 1 branch
BranchTrueFalseTotal
if (--$i and defined scalar readline $fh) 6 10 16
Condition66%2/3
--$i and defined scalar readline $fh
!ll&&!rl&&r
1006
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless (eof $fh) 41 14 55
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
BranchTrueFalseTotal
if ($f > 0 and $l < 0) 4 24 28
Condition100%3/3
$f > 0 and $l < 0
!ll&&!rl&&r
1864
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (eof $fh) 14 14 28
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
BranchTrueFalseTotal
if ($self->{'use_real_line_nr'}) { } 3 25 28
125 3 ▶ $. -= @$linebuff if $self->{use_buffer};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"use_buffer"} 2 1 3
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
BranchTrueFalseTotal
$eof ? : 27 36 63
136 63 ▶ if($self->{use_buffer}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_buffer"}) 35 28 63
137 35 ▶ unless($eof){
Branch: 2 branches
BranchTrueFalseTotal
if ($eof) { } 27 8 35
if (do { $l < 0 and scalar @$linebuff < abs $l }) 0 42 42
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
!ll&&!rl&&r
15120
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
ABresult
0X0
100
111
142 0 return;
143 }
144 35 $line = shift @$linebuff;
145 35 ▶ shift @$tellbuff unless @$tellbuff == 1; #always keep last pos
Branch: 1 branch
BranchTrueFalseTotal
unless @$tellbuff == 1 35 0 35
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
BranchTrueFalseTotal
if $self->EOF 24 75 99
155 75 ▶ unless(wantarray){
Branch: 1 branch
BranchTrueFalseTotal
unless (wantarray) 63 12 75
156 63 return $self->_readline;
157 }
158 #ARRAY
159 12 my @rows;
160 12 ▶ while(defined($_=$self->READLINE)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $self->READLINE)) 28 12 40
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__
↑ Summary