Reverse.pm

filter: Statementstmt 97.3% 72 / 74 Branchbran 63.6% 14 / 22 Conditioncond 50.0% 4 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 10 / 10 Podpod 33.3% 2 / 6 totaltotal 82.2% 102 / 124 CC 16 SCAR 15.2
CC
16
Cov
87%
new
7.4
next
5.4
dataRead
5.0
CRAP 4.6
↑ Summary reverse.pl »
1  
2 package IO::Reverse;
3  
4 2 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 # the following only used for development
9 #use Data::Dumper;
10 #use lib './lib'; # local Verbose.pm
11 #use Verbose;
12  
13 =head1 NAME
14  
15 IO::Reverse - read a file in reverse
16  
17 =head1 VERSION
18  
19 Version 0.03
20  
21 =cut
22  
23 2 our $VERSION = '0.03';
24  
25 =head1 SYNOPSIS
26  
27 Read a file from the end of file, line by line
28  
29 create a test file from the Command Line
30  
31 $ for i in $( seq 1 10 )
32 do
33 echo "this is $i"
34 done > t.txt
35  
36  
37 Now a small test script
38  
39 use IO::Reverse;
40  
41 my $f = IO::Reverse->new(
42 {
43 FILENAME => './t.txt'
44 }
45 );
46  
47 while ( my $line = $f->next ) {
48 print "$line";
49 }
50  
51  
52 =cut
53  
54 =head1 METHODS
55  
56 There are only 2 methods: new() and next();
57  
58 =head2 new
59  
60 my $f = IO::Reverse->new(
61 FILENAME => './t.txt'
62 );
63  
64 =head2 next
65  
66 Iterate through the file
67  
68 while ( my $line = $f->next ) {
69 print "$line";
70 }
71  
72  
73 =cut
74  
75  
76 =head1 AUTHOR
77  
78 Jared Still, C<< <jkstill at cpan.org> >>
79  
80 =head1 BUGS
81  
82 Please report any bugs or feature requests to C<bug-io-reverse at rt.cpan.org>, or through
83 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=IO-Reverse>. I will be notified, and then you'll
84 automatically be notified of progress on your bug as I make changes.
85  
86 =head1 SUPPORT
87  
88 You can find documentation for this module with the perldoc command.
89  
90 perldoc IO::Reverse
91  
92  
93 You can also look for information at:
94  
95 =over 4
96  
97 =item * RT: CPAN's request tracker (report bugs here)
98  
99 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=IO-Reverse>
100  
101 =item * CPAN Ratings
102  
103 L<https://cpanratings.perl.org/d/IO-Reverse>
104  
105 =item * Search CPAN
106  
107 L<https://metacpan.org/release/IO-Reverse>
108  
109 =back
110  
111 =head1 LICENSE AND COPYRIGHT
112  
113 This software is Copyright (c) 2023 by Jared Still.
114  
115 This is free software, licensed under:
116  
117 The MIT License
118  
119 =cut
120  
121  
122 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
123 #our @EXPORT = qw();
124 2 our @ISA=qw(Exporter);
125  
126 1 ▶sub new {
Subroutine new: called CC=7 SCAR=20.0
Pod documented
127 1 my ($class, $args) = @_;
128  
129 1 ▶ open(my $fh,'<', $args->{FILENAME}) || die "Reverse: could not open file: $args->{FILENAME} - $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $args->{"FILENAME"} or die qq[Reverse: could not open file: $args->{"FILENAME"} - $!\n] 1 0 1
130 1 $args->{FH}=$fh;
131 1 $args->{F_SIZE} = -s $args->{FILENAME};
132 # offset starts at penultimate character in file
133  
134 # uncomment if Verbose.pm needed again
135 #$args->{verbose} = Verbose->new(
136 #{
137 #VERBOSITY=>$args->{VERBOSITY},
138 #LABELS=>1,
139 #TIMESTAMP=>0,
140 ##HANDLE=>*STDERR
141 #HANDLE=>*STDOUT
142 #}
143 #);
144  
145 1 ▶ $args->{DEBUG} ||= 0;
Condition50%1/2
$args->{"DEBUG"} ||= 0
l!l
01
MC/DC0%0/1
$args->{"DEBUG"} ||= 0
$args->{"DEBUG"}
Truth table50%1/2
A
A= $args->{"DEBUG"}
Aresult
11
00
146 1 ▶ $args->{CHUNKSIZE} ||= 2**20;
Condition50%1/2
$args->{"CHUNKSIZE"} ||= 1048576
l!l
01
MC/DC0%0/1
$args->{"CHUNKSIZE"} ||= 1048576
$args->{"CHUNKSIZE"}
Truth table50%1/2
A
A= $args->{"CHUNKSIZE"}
Aresult
11
00
147  
148 # extra initial offset to avoid reading EOF
149 1 $args->{F_OFFSET} = ($args->{CHUNKSIZE}+1) * -1; # offset continually decrements to allow reverse seek
150  
151 1 ▶ if ( $args->{CHUNKSIZE} >= abs($args->{F_SIZE}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"CHUNKSIZE"} >= abs $args->{"F_SIZE"}) 1 0 1
152 1 $args->{CHUNKSIZE} = $args->{F_SIZE} ;
153 1 $args->{F_OFFSET} = ($args->{F_SIZE} * -1) +1 ;
154 1 $args->{F_OFFSET} = $args->{CHUNKSIZE} * -1;
155 }
156  
157 1 ▶ $args->{BOF} ||= 0; # true/false - have we reached beginning of file - control used in loadBuffer()
Condition50%1/2
$args->{"BOF"} ||= 0
l!l
01
MC/DC0%0/1
$args->{"BOF"} ||= 0
$args->{"BOF"}
Truth table50%1/2
A
A= $args->{"BOF"}
Aresult
11
00
158  
159 1 seek $args->{FH}, $args->{F_OFFSET} , 2;
160  
161 # do not use getpos - described as 'opaque' in the docs
162 # only useful for passing to setpos
163 #$args->{F_POS} = $args->{FH}->getpos;
164 1 $args->{F_POS} = tell($args->{FH});
165 1 ▶ $args->{DEBUG} ||= 0;
Condition50%1/2
$args->{"DEBUG"} ||= 0
l!l
01
MC/DC0%0/1
$args->{"DEBUG"} ||= 0
$args->{"DEBUG"}
Truth table50%1/2
A
A= $args->{"DEBUG"}
Aresult
11
00
166  
167 1 my $self = bless $args, $class;
168  
169 1 $self->showReadParameters;
170  
171 1 return $self;
172 }
173  
174  
175 # closure to preserve buffer across calls
176 {
177  
178 2 my ($accumulator,@bufLines) = ('',());
179 2 my ($firstChar) = ('');
180 2 my %readHash = ();
181  
182 1 ▶sub showReadParameters {
Subroutine showReadParameters: called CC=1 SCAR=0.0
Pod undocumented
183 1 my ($self) = @_;
184 # uncomment if Verbose.pm needed again
185 #$self->{verbose}->print(3," fsize: $self->{F_SIZE}",[]);
186 #$self->{verbose}->print(3," chunkSize: $self->{CHUNKSIZE}",[]);
187 #$self->{verbose}->print(3," offset: $self->{F_OFFSET}",[]);
188 }
189  
190 1 ▶sub setReadParameters {
Subroutine setReadParameters: called CC=2 SCAR=7.2
Pod undocumented
191 1 my ($self) = @_;
192  
193 1 ▶ if ( abs($self->{F_OFFSET}) + $self->{CHUNKSIZE} > $self->{F_SIZE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs($self->{'F_OFFSET'}) + $self->{'CHUNKSIZE'} > $self->{'F_SIZE'}) { } 1 0 1
194 1 $self->{CHUNKSIZE} = $self->{F_SIZE} - abs($self->{F_OFFSET}) ;#-1;
195 1 $self->{F_OFFSET} = ($self->{F_SIZE} * -1) ; #+1;
196 } else {
197 0 $self->{F_OFFSET} += ($self->{CHUNKSIZE} * -1);
198 }
199  
200 1 return;
201  
202 }
203  
204  
205 2 ▶sub dataRead {
Subroutine dataRead: called CC=5 SCAR=16.1
Pod undocumented
206 2 my ($self) = @_;
207 2 my $buffer='';
208 2 my $iter=0;
209  
210 2 while(1) {
211  
212 2 my $rsz = 0;
213 2 ▶ if ($self->{CHUNKSIZE} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"CHUNKSIZE"} > 0) 1 1 2
214 1 seek $self->{FH}, $self->{F_OFFSET} , 2;
215 1 $rsz = read($self->{FH}, $buffer, $self->{CHUNKSIZE} );
216 }
217  
218 2 ▶ if ($rsz < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rsz < 1) 1 1 2
219 1 @bufLines = split(/\n/, $accumulator);
220 1 $self->{BOF} = 1;
221 1 return 1;
222 }
223
224 1 $accumulator = $buffer . $accumulator;
225
226 1 $self->setReadParameters();
227  
228 1 ▶ last if $buffer =~ /\n/;
Branch: 1 branch
BranchTrueFalseTotal
if $buffer =~ /\n/u 1 0 1
229  
230 }
231  
232 1 @bufLines = split(/\n/, $accumulator);
233  
234 1 $accumulator = shift @bufLines; # possibly partial line
235 1 ▶ if (@bufLines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bufLines) 1 0 1
236 1 @bufLines = reverse @bufLines; # needs to be in reverse order if more than 1 element
237 }
238  
239 1 return 1;
240 }
241  
242 2 ▶sub loadBuffer {
Subroutine loadBuffer: called CC=1 SCAR=0.0
Pod undocumented
243 2 my ($self) = @_;
244  
245 2 my $r = $self->dataRead();
246  
247 2 return $r;
248  
249 }
250  
251 11 ▶sub next {
Subroutine next: called CC=5 SCAR=16.8
Pod documented
252 11 my ($self) = @_;
253  
254 11 ▶ return undef if $self->{BOF};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"BOF"} 1 10 11
255
256 10 ▶ if (! @bufLines ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@bufLines) 2 8 10
257 2 $self->loadBuffer() ;
258 }
259
260 10 ▶ if (@bufLines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bufLines) { } 10 0 10
261 10 my $f = shift @bufLines;
262 10 return $f . "\n";
263 } else {
264 0 ▶ push @bufLines, $accumulator if $accumulator;
Branch: 1 branch
BranchTrueFalseTotal
if $accumulator 0 0 0
265 }
266  
267 }
268  
269 } # end of closure
270  
271  
272 2 1;
273  
274  
↑ Summary reverse.pl »