blib/lib/File/Open/NoCache/ReadOnly.pm

filter: Statementstmt 87.5% 35 / 40 Branchbran 61.1% 11 / 18 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 70.7% 58 / 82 SCAR 27.9
CC
13
Cov
73%
new
13.2
DESTROY
5.8
BEGIN
1.0
CRAP 16.3
↑ Summary
1 package File::Open::NoCache::ReadOnly;
2  
3 # Author Nigel Horne: njh@bandsman.co.uk
4 # Copyright (C) 2019 Nigel Horne
5  
6 # Usage is subject to licence terms.
7 # The licence terms of this software are as follows:
8 # Personal single user, single computer use: GPL2
9 # All other users (including Commercial, Charity, Educational, Government)
10 # must apply in writing for a licence for use from Nigel Horne at the
11 # above e-mail.
12  
13 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use IO::AIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 NAME
19  
20 File::Open::NoCache::ReadOnly - Open a file and clear the cache afterward
21  
22 =head1 VERSION
23  
24 Version 0.01
25  
26 =cut
27  
28 3 our $VERSION = '0.01';
29  
30 =head1 SUBROUTINES/METHODS
31  
32 =head2 new
33  
34 Open a file and flush the cache afterwards.
35 One use case is building a large database from smaller files that are
36 only read in once.
37 Once the file has been used it's a waste of RAM to keep it in cache.
38  
39 use File::Open::NoCache::ReadOnly;
40 my $fh = File::Open::NoCache::ReadOnly->new('/etc/passwd');
41  
42 =cut
43  
44 sub new {
45 2 ▶ my $proto = shift;
Subroutine new: called CC=9 SCAR=25.8
Pod documented
46 2 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
47  
48 2 ▶ return unless(defined($class));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 2 2
49  
50 2 my %params;
51 2 ▶ if(ref($_[0]) eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 2 2
elsif (ref $_[0] or not defined $_[0]) { } 0 2 2
elsif (scalar(@_) % 2 == 0) { } 1 1 2
52 0 %params = %{$_[0]};
53 ▶ } elsif(ref($_[0]) || !defined($_[0])) {
Condition33%1/3
ref $_[0] or not defined $_[0]
l!l&&r!l&&!r
002
MC/DC0%0/2
ref $_[0] or not defined $_[0]
ref $_[0] not defined $_[0]
Truth table33%1/3
A or not(B)
A= ref $_[0]B= not defined $_[0]
ABresult
1X1
011
000
54 0 Carp::croak('Usage: ', __PACKAGE__, '->new(%args)');
55 } elsif(scalar(@_) % 2 == 0) {
56 1 %params = @_;
57 } else {
58 1 $params{'filename'} = shift;
59 }
60  
61 2 ▶ if(my $filename = $params{'filename'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $filename = $params{"filename"}) 2 0 2
62 2 ▶ if(open(my $fd, '<', $filename)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fd, "<", $filename) 1 1 2
63 1 return bless { fd => $fd }, $class
64 }
65 1 Carp::carp("$filename: $!");
66 1 return;
67 }
68 0 Carp::carp('Usage: ', __PACKAGE__, '->new(filename => $filename)');
69 0 return;
70 }
71  
72 =head2 fd
73  
74 Returns the file descriptor of the file
75  
76 my $fd = $fh->fd();
77 my $line = <$fd>;
78  
79 =cut
80  
81 sub fd {
82 1 ▶ my $self = shift;
Subroutine fd: called CC=1 SCAR=0.0
Pod documented
83  
84 1 return $self->{'fd'};
85 }
86  
87 sub DESTROY {
88 1 ▶ if(defined($^V) && ($^V ge 'v5.14.0')) {
Subroutine DESTROY: called CC=5 SCAR=17.5
Branch: 1 branch
BranchTrueFalseTotal
if (defined $^V and $^V ge "v5.14.0") 1 0 1
Condition33%1/3
defined $^V and $^V ge "v5.14.0"
!ll&&!rl&&r
001
MC/DC0%0/2
defined $^V and $^V ge "v5.14.0"
defined $^V $^V ge "v5.14.0"
Truth table33%1/3
A and B
A= defined $^VB= $^V ge "v5.14.0"
ABresult
0X0
100
111
89 1 ▶ return if ${^GLOBAL_PHASE} eq 'DESTRUCT'; # >= 5.14.0 only
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 1 1
90 }
91 1 my $self = shift;
92  
93 1 ▶ if(my $fd = $self->{'fd'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fd = $self->{"fd"}) 1 0 1
94 # my @statb = stat($fd);
95 # IO::AIO::fadvise($fd, 0, $statb[7] - 1, IO::AIO::FADV_DONTNEED);
96 1 IO::AIO::fadvise($fd, 0, 0, IO::AIO::FADV_DONTNEED);
97  
98 1 close $self->{'fd'};
99  
100 1 delete $self->{'fd'};
101 }
102 }
103  
104 =head1 AUTHOR
105  
106 Nigel Horne, C<< <njh at bandsman.co.uk> >>
107  
108 =head1 BUGS
109  
110 Please report any bugs or feature requests to
111 C<bug-file-Open-NoCache-ReadOnly at rt.cpan.org>,
112 or through the web interface at
113 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Open-NoCache-ReadOnly>.
114 I will be notified, and then you'll
115 automatically be notified of progress on your bug as I make changes.
116  
117 =head1 SUPPORT
118  
119 You can find documentation for this module with the perldoc command.
120  
121 perldoc File::Open::NoCache::ReadOnly
122  
123 You can also look for information at:
124  
125 =over 4
126  
127 =item * RT: CPAN's request tracker
128  
129 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Open-NoCache-ReadOnly>
130  
131 =item * AnnoCPAN: Annotated CPAN documentation
132  
133 L<http://annocpan.org/dist/File-Open-NoCache-ReadOnly>
134  
135 =item * CPAN Ratings
136  
137 L<http://cpanratings.perl.org/d/File-Open-NoCache-ReadOnly>
138  
139 =item * Search CPAN
140  
141 L<http://search.cpan.org/dist/File-Open-NoCache-ReadOnly/>
142  
143 =back
144  
145 =head1 LICENSE AND COPYRIGHT
146  
147 Copyright 2019 Nigel Horne.
148  
149 Usage is subject to licence terms.
150  
151 The licence terms of this software are as follows:
152  
153 * Personal single user, single computer use: GPL2
154 * All other users (including Commercial, Charity, Educational, Government)
155 must apply in writing for a licence for use from Nigel Horne at the
156 above e-mail.
157  
158 =cut
159  
160 3 1;
↑ Summary