blib/lib/File/pfopen.pm

filter: Statementstmt 96.7% 30 / 31 Branchbran 95.0% 19 / 20 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 96.6% 57 / 59 CC 14 SCAR 24.6
CC
14
Cov
96%
pfopen
14.0
BEGIN
1.0
BEGIN
1.0
CRAP 11.7
↑ Summary
1 package File::pfopen;
2  
3 # Author Nigel Horne: njh@bandsman.co.uk
4 # Copyright (C) 2017-2024 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 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 require Exporter;
18 2 our @ISA = ('Exporter');
19 2 our @EXPORT_OK = ('pfopen');
20  
21 =head1 NAME
22  
23 File::pfopen - Try hard to find a file
24  
25 =head1 VERSION
26  
27 Version 0.03
28  
29 =cut
30  
31 2 our $VERSION = '0.03';
32  
33 =head1 SUBROUTINES/METHODS
34  
35 =head2 pfopen
36  
37 Look in a list of directories for a file with an optional list of suffixes.
38  
39 use File::pfopen 'pfopen';
40 ($fh, $filename) = pfopen('/tmp:/var/tmp:/home/njh/tmp', 'foo', 'txt:bin');
41 $fh = pfopen('/tmp:/var/tmp:/home/njh/tmp', 'foo', '<');
42  
43 If mode (argument 4) isn't given, the file is open read/write ('+<')
44  
45 =cut
46  
47 sub pfopen
48 24 ▶{
Subroutine pfopen: called CC=14 SCAR=26.4
Pod documented
49 24 my ($path, $prefix, $suffixes, $mode) = @_;
50 24 ▶ my $candidate = defined($suffixes) ? "$prefix;$path;$suffixes" : "$prefix;$path";
Branch: 1 branch
BranchTrueFalseTotal
defined $suffixes ? : 15 9 24
51 24 our $savedpaths;
52  
53 24 ▶ $mode ||= '+<'; # defaults to opening RW
Condition100%2/2
$mode ||= "+<"
l!l
222
MC/DC100%1/1
$mode ||= "+<"
$mode
Truth table100%2/2
A
A= $mode
Aresult
11
00
54  
55 # Return cached filename if available
56 24 ▶ if(my $rc = $savedpaths->{$candidate}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rc = $File::pfopen::savedpaths->{$candidate}) 4 20 24
57 # $self->_log({ message => "remembered $savedpaths->{$candidate}" });
58 4 ▶ if(open(my $fh, $mode, $rc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, $mode, $rc) 4 0 4
59 4 ▶ return wantarray ? ($fh, $rc) : $fh;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 2 4
60 }
61 0 delete $savedpaths->{$candidate}; # Failed to open cached file
62 }
63  
64 20 foreach my $dir (split /:/, $path) {
65 27 ▶ next unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 3 24 27
66  
67 24 ▶ foreach my $suffix (defined($suffixes) ? split(/:/, $suffixes) : undef) {
Branch: 1 branch
BranchTrueFalseTotal
defined $suffixes ? : 13 11 24
68 28 ▶ my $rc = File::Spec->catfile($dir, defined $suffix ? "$prefix.$suffix" : $prefix);
Branch: 1 branch
BranchTrueFalseTotal
defined $suffix ? : 17 11 28
69 28 ▶ next unless -r $rc;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $rc 15 13 28
70  
71 # $self->_log({ message => "using $rc" });
72  
73 # FIXME: Doesn't play well in taint mode
74 13 ▶ open(my $fh, $mode, $rc) or next;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $mode, $rc or next 12 1 13
75  
76 12 $savedpaths->{$candidate} = $rc;
77 12 ▶ return wantarray ? ($fh, $rc) : $fh;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 7 12
78 }
79 }
80  
81 8 return;
82 }
83  
84  
85 =head1 AUTHOR
86  
87 Nigel Horne, C<< <njh at bandsman.co.uk> >>
88  
89 =head1 BUGS
90  
91 Doesn't play well in taint mode.
92  
93 Using the colon separator can cause confusion on Windows.
94  
95 Would be better if the mode and suffixes options were the other way around, but it's too late to change that now.
96  
97 Please report any bugs or feature requests to C<bug-file-pfopen at rt.cpan.org>,
98 or through the web interface at
99 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-pfopen>.
100 I will be notified, and then you'll
101 automatically be notified of progress on your bug as I make changes.
102  
103 =head1 SUPPORT
104  
105 You can find documentation for this module with the perldoc command.
106  
107 perldoc File::pfopen
108  
109 You can also look for information at:
110  
111 =over 4
112  
113 =item * MetaCPAN
114  
115 L<https://metacpan.org/release/File-pfopen>
116  
117 =item * RT: CPAN's request tracker
118  
119 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=File-pfopen>
120  
121 =item * CPANTS
122  
123 L<http://cpants.cpanauthors.org/dist/File-pfopen>
124  
125 =item * CPAN Testers' Matrix
126  
127 L<http://matrix.cpantesters.org/?dist=File-pfopen>
128  
129 =item * CPAN Testers Dependencies
130  
131 L<http://deps.cpantesters.org/?module=File::pfopen>
132  
133 =back
134  
135 =head1 LICENSE AND COPYRIGHT
136  
137 Copyright 2017-2024 Nigel Horne.
138  
139 Usage is subject to licence terms.
140  
141 The licence terms of this software are as follows:
142  
143 * Personal single user, single computer use: GPL2
144 * All other users (including Commercial, Charity, Educational, Government)
145 must apply in writing for a licence for use from Nigel Horne at the
146 above e-mail.
147  
148 =cut
149  
150 2 1;
↑ Summary