| 1 | package File::pfopen; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | # Author Nigel Horne: njh@bandsman.co.uk | ||||||||||||||
| 4 | # Copyright (C) 2017 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 File::Spec; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | 3 | require Exporter; | |||||||||||||
| 18 | 3 | our @ISA = ('Exporter'); | |||||||||||||
| 19 | 3 | 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.02 | ||||||||||||||
| 28 | |||||||||||||||
| 29 | =cut | ||||||||||||||
| 30 | |||||||||||||||
| 31 | 3 | our $VERSION = '0.02'; | |||||||||||||
| 32 | |||||||||||||||
| 33 | =head1 SYNOPSIS | ||||||||||||||
| 34 | |||||||||||||||
| 35 | =head2 pfopen | ||||||||||||||
| 36 | |||||||||||||||
| 37 | use File::pfopen 'pfopen'; | ||||||||||||||
| 38 | ($fh, $filename) = pfopen('/tmp:/var/tmp:/home/njh/tmp', 'foo', 'txt:bin')); | ||||||||||||||
| 39 | $fh = pfopen('/tmp:/var/tmp:/home/njh/tmp', 'foo')); | ||||||||||||||
| 40 | |||||||||||||||
| 41 | =cut | ||||||||||||||
| 42 | |||||||||||||||
| 43 | sub pfopen { | ||||||||||||||
| 44 | 13 | ▶ | my $path = shift; | ||||||||||||
|
Subroutine
pfopen: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||
| 45 | 13 | my $prefix = shift; | |||||||||||||
| 46 | 13 | my $suffixes = shift; | |||||||||||||
| 47 | |||||||||||||||
| 48 | 13 | our $savedpaths; | |||||||||||||
| 49 | |||||||||||||||
| 50 | 13 | my $candidate; | |||||||||||||
| 51 | 13 | ▶ | if(defined($suffixes)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 52 | 9 | $candidate = "$prefix;$path;$suffixes"; | |||||||||||||
| 53 | } else { | ||||||||||||||
| 54 | 4 | $candidate = "$prefix;$path"; | |||||||||||||
| 55 | } | ||||||||||||||
| 56 | 13 | ▶ | if($savedpaths->{$candidate}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | # $self->_log({ message => "remembered $savedpaths->{$candidate}" }); | ||||||||||||||
| 58 | 3 | my $rc = $savedpaths->{$candidate}; | |||||||||||||
| 59 | 3 | open(my $fh, '+<', $rc); | |||||||||||||
| 60 | 3 | ▶ | if(wantarray) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 61 | 1 | return ($fh, $rc); | |||||||||||||
| 62 | } | ||||||||||||||
| 63 | 2 | return $fh; | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 10 | foreach my $dir(split(/:/, $path)) { | |||||||||||||
| 67 | 13 | ▶ | next unless(-d $dir); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | 13 | ▶ | if($suffixes) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 69 | 8 | foreach my $suffix(split(/:/, $suffixes)) { | |||||||||||||
| 70 | # $self->_log({ message => "check for file $dir/$prefix.$suffix" }); | ||||||||||||||
| 71 | 11 | my $rc = File::Spec->catfile($dir, "$prefix.$suffix"); | |||||||||||||
| 72 | 11 | ▶ | if(-r $rc) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | 4 | $savedpaths->{$candidate} = $rc; | |||||||||||||
| 74 | 4 | open(my $fh, '+<', $rc); | |||||||||||||
| 75 | 4 | ▶ | if(wantarray) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 76 | 1 | return ($fh, $rc); | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | 3 | return $fh; | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | } | ||||||||||||||
| 81 | } elsif(-r "$dir/$prefix") { | ||||||||||||||
| 82 | 2 | my $rc = File::Spec->catfile($dir, $prefix); | |||||||||||||
| 83 | 2 | $savedpaths->{$candidate} = $rc; | |||||||||||||
| 84 | # $self->_log({ message => "using $rc" }); | ||||||||||||||
| 85 | 2 | open(my $fh, '+<', $rc); | |||||||||||||
| 86 | 2 | ▶ | if(wantarray) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | 1 | return ($fh, $rc); | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | 1 | return $fh; | |||||||||||||
| 90 | } | ||||||||||||||
| 91 | } | ||||||||||||||
| 92 | 4 | return(); | |||||||||||||
| 93 | } | ||||||||||||||
| 94 | |||||||||||||||
| 95 | =head1 AUTHOR | ||||||||||||||
| 96 | |||||||||||||||
| 97 | Nigel Horne, C<< <njh at bandsman.co.uk> >> | ||||||||||||||
| 98 | |||||||||||||||
| 99 | =head1 BUGS | ||||||||||||||
| 100 | |||||||||||||||
| 101 | Please report any bugs or feature requests to C<bug-file-pfopen at rt.cpan.org>, | ||||||||||||||
| 102 | or through the web interface at | ||||||||||||||
| 103 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-pfopen>. | ||||||||||||||
| 104 | I will be notified, and then you'll | ||||||||||||||
| 105 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||
| 106 | |||||||||||||||
| 107 | =head1 SEE ALSO | ||||||||||||||
| 108 | |||||||||||||||
| 109 | =head1 SUPPORT | ||||||||||||||
| 110 | |||||||||||||||
| 111 | You can find documentation for this module with the perldoc command. | ||||||||||||||
| 112 | |||||||||||||||
| 113 | perldoc File::pfopen | ||||||||||||||
| 114 | |||||||||||||||
| 115 | You can also look for information at: | ||||||||||||||
| 116 | |||||||||||||||
| 117 | =over 4 | ||||||||||||||
| 118 | |||||||||||||||
| 119 | =item * RT: CPAN's request tracker | ||||||||||||||
| 120 | |||||||||||||||
| 121 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=File-pfopen> | ||||||||||||||
| 122 | |||||||||||||||
| 123 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||
| 124 | |||||||||||||||
| 125 | L<http://annocpan.org/dist/File-pfopen> | ||||||||||||||
| 126 | |||||||||||||||
| 127 | =item * CPAN Ratings | ||||||||||||||
| 128 | |||||||||||||||
| 129 | L<http://cpanratings.perl.org/d/File-pfopen> | ||||||||||||||
| 130 | |||||||||||||||
| 131 | =item * Search CPAN | ||||||||||||||
| 132 | |||||||||||||||
| 133 | L<http://search.cpan.org/dist/File-pfopen/> | ||||||||||||||
| 134 | |||||||||||||||
| 135 | =back | ||||||||||||||
| 136 | |||||||||||||||
| 137 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||
| 138 | |||||||||||||||
| 139 | Copyright 2017 Nigel Horne. | ||||||||||||||
| 140 | |||||||||||||||
| 141 | Usage is subject to licence terms. | ||||||||||||||
| 142 | |||||||||||||||
| 143 | The licence terms of this software are as follows: | ||||||||||||||
| 144 | |||||||||||||||
| 145 | * Personal single user, single computer use: GPL2 | ||||||||||||||
| 146 | * All other users (including Commercial, Charity, Educational, Government) | ||||||||||||||
| 147 | must apply in writing for a licence for use from Nigel Horne at the | ||||||||||||||
| 148 | above e-mail. | ||||||||||||||
| 149 | |||||||||||||||
| 150 | =cut | ||||||||||||||
| 151 | |||||||||||||||
| 152 | 3 | 1; | |||||||||||||