| 1 | package File::pfopen; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # Author Nigel Horne: njh@nigelhorne.com | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # Copyright (C) 2017-2026 Nigel Horne | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | # Usage is subject to the GPL2 licence terms. | ||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use Config; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 6 | ▶ | use Scalar::Util qw(tainted); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 6 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 6 | our @ISA = ('Exporter'); | |||||||||||||||||||||||||||||||||||||||||
| 17 | 6 | our @EXPORT_OK = ('pfopen'); | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | File::pfopen - Try hard to find a file | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | Version 0.04 | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 6 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | Find a configuration file in the first of several places, and read it: | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | use File::pfopen 'pfopen'; | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | my $fh = pfopen('/etc/myapp:/usr/local/etc/myapp', 'config', 'yml:json', '<'); | ||||||||||||||||||||||||||||||||||||||||||
| 38 | if($fh) { | ||||||||||||||||||||||||||||||||||||||||||
| 39 | my @lines = <$fh>; | ||||||||||||||||||||||||||||||||||||||||||
| 40 | close($fh); | ||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | Find out which file was found, by asking for two return values: | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | use File::pfopen 'pfopen'; | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | my ($fh, $filename) = pfopen('/etc/myapp:/usr/local/etc/myapp', 'config', 'yml:json', '<'); | ||||||||||||||||||||||||||||||||||||||||||
| 48 | print "Using $filename\n" if($fh); | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | Build the list of directories as an array, which works the same on every | ||||||||||||||||||||||||||||||||||||||||||
| 51 | operating system: | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||||
| 54 | use File::pfopen 'pfopen'; | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | my @dirs = (File::Spec->catdir($ENV{'HOME'}, '.myapp'), File::Spec->tmpdir()); | ||||||||||||||||||||||||||||||||||||||||||
| 57 | my $fh = pfopen(\@dirs, 'state', 'dat', '<'); | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | Use a full filename, with no list of suffixes: | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | use File::pfopen 'pfopen'; | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | my $fh = pfopen('/var/tmp:/tmp', 'report.csv', undef, '<'); | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | Use drive letters on Windows, where the directories are separated by ';': | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | use File::pfopen 'pfopen'; | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | my $fh = pfopen('C:\myapp;D:\shared\myapp', 'config', 'ini', '<'); | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | Open a found file to add to it, and read text in UTF-8: | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | use File::pfopen 'pfopen'; | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | my $log = pfopen(['/var/log/myapp', '/tmp'], 'activity', 'log', '>>'); | ||||||||||||||||||||||||||||||||||||||||||
| 76 | my $text = pfopen(['/usr/share/myapp'], 'messages', 'txt', '<:encoding(UTF-8)'); | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | This module has one function, C<pfopen>. | ||||||||||||||||||||||||||||||||||||||||||
| 81 | You give it a list of directories, a filename, and optionally a list of endings | ||||||||||||||||||||||||||||||||||||||||||
| 82 | (called suffixes) for the filename. | ||||||||||||||||||||||||||||||||||||||||||
| 83 | It looks for the file in each directory, in order, | ||||||||||||||||||||||||||||||||||||||||||
| 84 | and opens the first one that it finds. | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | For example, with the directories C</etc> and C</tmp>, | ||||||||||||||||||||||||||||||||||||||||||
| 87 | the filename C<foo> and the suffixes C<txt:bin>, | ||||||||||||||||||||||||||||||||||||||||||
| 88 | it tries these files, in this order, and stops at the first one that it can open: | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | /etc/foo.txt | ||||||||||||||||||||||||||||||||||||||||||
| 91 | /etc/foo.bin | ||||||||||||||||||||||||||||||||||||||||||
| 92 | /tmp/foo.txt | ||||||||||||||||||||||||||||||||||||||||||
| 93 | /tmp/foo.bin | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | It is similar to the way that a shell uses the C<PATH> environment variable to find a program. | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | The function remembers each file that it finds. | ||||||||||||||||||||||||||||||||||||||||||
| 98 | If you ask for the same thing again, it opens the same file again without looking. | ||||||||||||||||||||||||||||||||||||||||||
| 99 | See L</COMMON PITFALLS>. | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | =head2 pfopen($path, $prefix, $suffixes, $mode) | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | my $fh = pfopen($path, $prefix); | ||||||||||||||||||||||||||||||||||||||||||
| 106 | my $fh = pfopen($path, $prefix, $suffixes); | ||||||||||||||||||||||||||||||||||||||||||
| 107 | my $fh = pfopen($path, $prefix, $suffixes, $mode); | ||||||||||||||||||||||||||||||||||||||||||
| 108 | my ($fh, $filename) = pfopen($path, $prefix, $suffixes, $mode); | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | =head3 Purpose | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | Look in a list of directories for a file, optionally trying a list of suffixes, | ||||||||||||||||||||||||||||||||||||||||||
| 113 | and open the first file that it finds. | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | It only finds files. It never returns a directory. | ||||||||||||||||||||||||||||||||||||||||||
| 116 | It looks in the directories in order. | ||||||||||||||||||||||||||||||||||||||||||
| 117 | In each directory, it tries the suffixes in order. | ||||||||||||||||||||||||||||||||||||||||||
| 118 | So a file in an earlier directory is used before a file with an earlier suffix. | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | It remembers each successful search until the program ends. | ||||||||||||||||||||||||||||||||||||||||||
| 121 | When it is called again with the same C<$path>, C<$prefix> and C<$suffixes>, | ||||||||||||||||||||||||||||||||||||||||||
| 122 | it opens the same file again and does not search. | ||||||||||||||||||||||||||||||||||||||||||
| 123 | If that file can no longer be opened, it forgets the file and searches again. | ||||||||||||||||||||||||||||||||||||||||||
| 124 | A string and an array reference that give the same directories count as the same C<$path>, | ||||||||||||||||||||||||||||||||||||||||||
| 125 | so C<['/a', '/b']> and C<'/a:/b'> share one remembered file. | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | =head3 Arguments | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | =item C<$path> (required) | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | The directories to look in. | ||||||||||||||||||||||||||||||||||||||||||
| 134 | This is one of: | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | =item * A string. The directories are separated by the operating system's path separator. | ||||||||||||||||||||||||||||||||||||||||||
| 139 | This is ':' on UNIX and ';' on Windows, the same as in the C<PATH> environment variable. | ||||||||||||||||||||||||||||||||||||||||||
| 140 | Perl stores this character in C<$Config{path_sep}>. | ||||||||||||||||||||||||||||||||||||||||||
| 141 | Because Windows uses ';', you can use drive letters, for example C<'C:\tmp;D:\data'>. | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | =item * A reference to an array of directories, for example C<['/etc', '/tmp']>. | ||||||||||||||||||||||||||||||||||||||||||
| 144 | Use this when a directory name contains the separator character. | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | These directories are skipped without an error: | ||||||||||||||||||||||||||||||||||||||||||
| 149 | directories that do not exist, names that are not directories, | ||||||||||||||||||||||||||||||||||||||||||
| 150 | empty strings, C<undef> values, and names that contain a NUL character. | ||||||||||||||||||||||||||||||||||||||||||
| 151 | A relative directory, such as C<'.'>, is relative to the current directory when C<pfopen> is called. | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | =item C<$prefix> (required) | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | The name of the file to find, without its suffix. For example, C<'config'>. | ||||||||||||||||||||||||||||||||||||||||||
| 156 | It must not be empty. | ||||||||||||||||||||||||||||||||||||||||||
| 157 | The name is used exactly as given. | ||||||||||||||||||||||||||||||||||||||||||
| 158 | Characters such as C<|>, C<E<lt>> and C<E<gt>> have no special meaning. | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | =item C<$suffixes> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | A list of suffixes to try, separated by ':' on all operating systems. | ||||||||||||||||||||||||||||||||||||||||||
| 163 | For example, C<'yml:json'> tries C<config.yml> and then C<config.json>. | ||||||||||||||||||||||||||||||||||||||||||
| 164 | Do not include the dot: C<pfopen> adds it. | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | If this is not given, or is C<undef>, C<$prefix> is used as the whole filename. | ||||||||||||||||||||||||||||||||||||||||||
| 167 | If this is an empty string, nothing can be found. | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | =item C<$mode> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | How to open the file. This is the same as the second argument to Perl's C<open> function. | ||||||||||||||||||||||||||||||||||||||||||
| 172 | If it is not given, or is false (C<undef>, C<''> or C<0>), | ||||||||||||||||||||||||||||||||||||||||||
| 173 | the file is opened to read and write (C<'+E<lt>'>). | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | Only these modes are allowed: | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | < read | ||||||||||||||||||||||||||||||||||||||||||
| 178 | > write (the file is emptied first) | ||||||||||||||||||||||||||||||||||||||||||
| 179 | >> add to the end of the file | ||||||||||||||||||||||||||||||||||||||||||
| 180 | +< read and write | ||||||||||||||||||||||||||||||||||||||||||
| 181 | +> read and write (the file is emptied first) | ||||||||||||||||||||||||||||||||||||||||||
| 182 | +>> read and add to the end of the file | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | After the mode you can add I/O layers, for example C<'E<lt>:raw'> or C<'E<lt>:encoding(UTF-8)'>. | ||||||||||||||||||||||||||||||||||||||||||
| 185 | Other modes, such as the pipe mode C<'-|'>, are not allowed, | ||||||||||||||||||||||||||||||||||||||||||
| 186 | so the file that is found can never be run as a program. | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | =head3 Returns | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | =item * When the file is found and opened, and C<pfopen> is called in scalar context | ||||||||||||||||||||||||||||||||||||||||||
| 195 | (one return value), it returns the open file handle. | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | =item * When the file is found and opened, and C<pfopen> is called in list context | ||||||||||||||||||||||||||||||||||||||||||
| 198 | (two return values), it returns the open file handle and the full filename that was opened. | ||||||||||||||||||||||||||||||||||||||||||
| 199 | The filename is built from the directory, so it is relative if the directory was relative. | ||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | =item * When no file is found, or no file can be opened in C<$mode>, | ||||||||||||||||||||||||||||||||||||||||||
| 202 | it returns C<undef> in scalar context and an empty list in list context. | ||||||||||||||||||||||||||||||||||||||||||
| 203 | This also happens when C<$prefix> or C<$suffixes> contains a NUL character, | ||||||||||||||||||||||||||||||||||||||||||
| 204 | because a filename can never contain one. | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | =head3 Side Effects | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | It remembers the filename of each successful search, as described in L</Purpose>. | ||||||||||||||||||||||||||||||||||||||||||
| 211 | It also forgets a remembered filename when that file can no longer be opened. | ||||||||||||||||||||||||||||||||||||||||||
| 212 | In taint mode, a call with any tainted argument always searches, | ||||||||||||||||||||||||||||||||||||||||||
| 213 | and doesn't remember what it finds, | ||||||||||||||||||||||||||||||||||||||||||
| 214 | so a remembered filename can't hide or add taint. | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | It does not change C<$!>, C<$@>, C<$_>, or any of its arguments. | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | =head3 Errors | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | It croaks (dies with an error message that gives your file and line number) with: | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | =item C<Usage: pfopen($path, $prefix, [$suffixes, [$mode]])> | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | when C<$path> or C<$prefix> is missing or C<undef>, or C<$prefix> is an empty string. | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | =item C<pfopen: path must be a string or an array reference> | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | when C<$path> is a reference to anything except an array. | ||||||||||||||||||||||||||||||||||||||||||
| 231 | This includes an object that is a blessed array. | ||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | =item C<pfopen: invalid mode '$mode'> | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | when C<$mode> is not one of the modes listed in L</Arguments>, for example C<'-|'>. | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | It does not croak or warn when a file can't be found or can't be opened. | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | When Perl runs in taint mode (C<-T>) and C<$path> or C<$prefix> is tainted, | ||||||||||||||||||||||||||||||||||||||||||
| 242 | a mode that writes, including the default C<'+E<lt>'>, | ||||||||||||||||||||||||||||||||||||||||||
| 243 | dies with Perl's own error C<Insecure dependency in open while running with -T switch>. | ||||||||||||||||||||||||||||||||||||||||||
| 244 | Use the mode C<'E<lt>'>, or untaint the arguments first. | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | =head3 Usage Example | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | use File::pfopen 'pfopen'; | ||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | my ($fh, $filename) = pfopen('/tmp:/var/tmp:/home/njh/tmp', 'foo', 'txt:bin'); | ||||||||||||||||||||||||||||||||||||||||||
| 251 | my $fh2 = pfopen('/tmp:/var/tmp:/home/njh/tmp', 'foo', undef, '<'); | ||||||||||||||||||||||||||||||||||||||||||
| 252 | my $fh3 = pfopen(['/tmp', '/var/tmp'], 'foo', 'txt'); | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | The arguments are positional. | ||||||||||||||||||||||||||||||||||||||||||
| 257 | The schemas are in the format of L<Params::Validate::Strict> and L<Return::Set>. | ||||||||||||||||||||||||||||||||||||||||||
| 258 | The output schema describes the list that is returned in list context. | ||||||||||||||||||||||||||||||||||||||||||
| 259 | In scalar context the return value is the first element of that list, or C<undef>. | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | [ | ||||||||||||||||||||||||||||||||||||||||||
| 264 | { name => 'path', type => ['string', 'arrayref'], position => 0 }, | ||||||||||||||||||||||||||||||||||||||||||
| 265 | { name => 'prefix', type => 'string', min => 1, position => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 266 | { name => 'suffixes', type => 'string', optional => 1, position => 2 }, | ||||||||||||||||||||||||||||||||||||||||||
| 267 | { name => 'mode', type => 'string', optional => 1, default => '+<', position => 3, matches => qr/\A\+?(?:<|>>?)(?::.*)?\z/s }, | ||||||||||||||||||||||||||||||||||||||||||
| 268 | ] | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | { | ||||||||||||||||||||||||||||||||||||||||||
| 273 | type => 'arrayref', | ||||||||||||||||||||||||||||||||||||||||||
| 274 | min => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 275 | max => 2, | ||||||||||||||||||||||||||||||||||||||||||
| 276 | callback => sub { (scalar(@{$_[0]}) == 0) || ((scalar(@{$_[0]}) == 2) && defined(Scalar::Util::openhandle($_[0]->[0])) && !ref($_[0]->[1])) }, | ||||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | sub pfopen | ||||||||||||||||||||||||||||||||||||||||||
| 282 | 187 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pfopen: called CC=31 SCAR=34.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | 187 | my ($path, $prefix, $suffixes, $mode) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | 187 | local $!; # Failed file tests set it, and the caller hasn't asked for an error code | |||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | 187 | ▶ | if((!defined($path)) || (!defined($prefix)) || (length($prefix) == 0)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not defined $path && defined $prefix or length $prefix == 0
defined $path && defined $prefix
MC/DC100%3/3
not defined $path && defined $prefix or length $prefix == 0
defined $path
defined $prefix
length $prefix == 0
Truth table100%4/4
not(A && B) or C
A= defined $pathB= defined $prefixC= length $prefix == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | 10 | croak('Usage: pfopen($path, $prefix, [$suffixes, [$mode]])'); | |||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||
| 290 | 177 | ▶ | if(ref($path) && (ref($path) ne 'ARRAY')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $path and ref $path ne "ARRAY"
MC/DC100%2/2
ref $path and ref $path ne "ARRAY"
ref $path
ref $path ne "ARRAY"
Truth table100%3/3
A and B
A= ref $pathB= ref $path ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | 8 | croak('pfopen: path must be a string or an array reference'); | |||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | 169 | ▶ | $mode ||= '+<'; # defaults to opening RW | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$mode ||= "+<"
MC/DC100%1/1
$mode ||= "+<"
$mode
Truth table100%2/2
A
A= $mode
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | # Allowing other modes would let '-|' or '|-' run the found file as a command | ||||||||||||||||||||||||||||||||||||||||||
| 297 | 169 | ▶ | if($mode !~ /\A\+?(?:<|>>?)(?::.*)?\z/s) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 298 | 21 | croak("pfopen: invalid mode '$mode'"); | |||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | # A NUL can never be in a filename, so don't bother looking. | ||||||||||||||||||||||||||||||||||||||||||
| 302 | # It would only give warnings from the file tests | ||||||||||||||||||||||||||||||||||||||||||
| 303 | 148 | ▶ | return if(index($prefix, "\0") >= 0); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 304 | 146 | ▶ | return if(defined($suffixes) && (index($suffixes, "\0") >= 0)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $suffixes and (index($suffixes, "\0") != -1)
MC/DC100%2/2
defined $suffixes and (index($suffixes, "\0") != -1)
defined $suffixes
(index($suffixes, "\0") != -1)
Truth table100%3/3
A and B
A= defined $suffixesB= (index($suffixes, "\0") != -1)
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | 4215 | ▶ | my @dirs = grep { defined($_) && length($_) && (index($_, "\0") < 0) } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $_ && length $_ && (index($_, "\000") == -1)
defined $_ and length $_
MC/DC100%3/3
defined $_ && length $_ && (index($_, "\000") == -1)
defined $_
length $_
(index($_, "\000") == -1)
Truth table100%4/4
A and B && C
A= defined $_B= length $_C= (index($_, "\000") == -1)
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | 144 | (ref($path) ? @{$path} : split(/\Q$Config{path_sep}\E/, $path)); | |||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | # NUL can't appear in a filename, so different arguments can't give the same key | ||||||||||||||||||||||||||||||||||||||||||
| 310 | 144 | ▶ | my $candidate = join("\0", $prefix, defined($suffixes) ? $suffixes : '', @dirs); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 144 | our $savedpaths; | |||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | # A remembered name would hide the taint of these arguments, letting a write | ||||||||||||||||||||||||||||||||||||||||||
| 314 | # succeed that taint mode should stop, or the other way around | ||||||||||||||||||||||||||||||||||||||||||
| 315 | 144 | ▶ | my $remember = !(tainted($candidate) || tainted($mode)); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
&tainted($candidate) || &tainted($mode)
MC/DC50%1/2
&tainted($candidate) || &tainted($mode)
&tainted($candidate)
&tainted($mode)
Truth table66%2/3
A || B
A= &tainted($candidate)B= &tainted($mode)
| |||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | # Return cached filename if available | ||||||||||||||||||||||||||||||||||||||||||
| 318 | 144 | ▶ | if($remember && (my $rc = $savedpaths->{$candidate})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$remember and my $rc = $File::pfopen::savedpaths->{$candidate}
MC/DC100%2/2
$remember and my $rc = $File::pfopen::savedpaths->{$candidate}
$remember
my $rc = $File::pfopen::savedpaths->{$candidate}
Truth table100%3/3
A and B
A= $rememberB= my $rc = $File::pfopen::savedpaths->{$candidate}
| |||||||||||||||||||||||||||||||||||||||||||
| 319 | 26 | ▶ | if((!-d $rc) && open(my $fh, $mode, $rc)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not -d $rc and open my $fh, $mode, $rc
MC/DC100%2/2
not -d $rc and open my $fh, $mode, $rc
not -d $rc
open my $fh, $mode, $rc
Truth table100%3/3
not(A) and B
A= not -d $rcB= open my $fh, $mode, $rc
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | 21 | ▶ | return wantarray ? ($fh, $rc) : $fh; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||||||||||
| 322 | 5 | delete $savedpaths->{$candidate}; # Failed to open cached file | |||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | 123 | foreach my $dir (@dirs) { | |||||||||||||||||||||||||||||||||||||||||
| 326 | 4157 | ▶ | next unless -d $dir; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | 135 | ▶ | foreach my $suffix (defined($suffixes) ? split(/:/, $suffixes) : undef) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | 1142 | ▶ | my $rc = File::Spec->catfile($dir, defined $suffix ? "$prefix.$suffix" : $prefix); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | # Opening a directory with '<' succeeds, but it isn't what was asked for | ||||||||||||||||||||||||||||||||||||||||||
| 332 | 1142 | ▶ | next if(-d $rc); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 333 | 1135 | ▶ | next unless -r _; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | # In taint mode this dies if $rc is tainted and $mode writes, see "Errors" in the POD | ||||||||||||||||||||||||||||||||||||||||||
| 336 | 89 | ▶ | open(my $fh, $mode, $rc) or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | 77 | ▶ | $savedpaths->{$candidate} = $rc if($remember); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | 77 | ▶ | return wantarray ? ($fh, $rc) : $fh; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | 43 | return; | |||||||||||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | =head1 COMMON PITFALLS | ||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | =item * B<The default mode is read and write.> | ||||||||||||||||||||||||||||||||||||||||||
| 352 | If you only want to read, give the mode C<'E<lt>'>. | ||||||||||||||||||||||||||||||||||||||||||
| 353 | Without it, a file that you can read but not write is skipped, | ||||||||||||||||||||||||||||||||||||||||||
| 354 | and C<pfopen> returns nothing even though the file exists. | ||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | =item * B<The mode is the fourth argument, not the third.> | ||||||||||||||||||||||||||||||||||||||||||
| 357 | To give a mode without suffixes, pass C<undef> as the suffixes: | ||||||||||||||||||||||||||||||||||||||||||
| 358 | C<pfopen($path, 'foo.txt', undef, 'E<lt>')>. | ||||||||||||||||||||||||||||||||||||||||||
| 359 | C<pfopen($path, 'foo.txt', 'E<lt>')> looks for a file called C<foo.txt.E<lt>>. | ||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | =item * B<The modes C<'E<gt>'> and C<'+E<gt>'> empty the file.> | ||||||||||||||||||||||||||||||||||||||||||
| 362 | C<pfopen> only finds files that already exist, | ||||||||||||||||||||||||||||||||||||||||||
| 363 | so these modes always delete the contents of an existing file. | ||||||||||||||||||||||||||||||||||||||||||
| 364 | Use C<'E<gt>E<gt>'> to add to the end of a file instead. | ||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||
| 366 | =item * B<Found files are remembered.> | ||||||||||||||||||||||||||||||||||||||||||
| 367 | After a file is found, a later call with the same arguments opens the same file without searching. | ||||||||||||||||||||||||||||||||||||||||||
| 368 | If you then create the file in an earlier directory, or change to a different current directory, | ||||||||||||||||||||||||||||||||||||||||||
| 369 | the remembered file is still used, as long as it can still be opened. | ||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||
| 371 | =item * B<Use ';' between directories on Windows.> | ||||||||||||||||||||||||||||||||||||||||||
| 372 | On Windows the separator is ';', not ':', so C<'C:\a:C:\b'> is one directory, not two. | ||||||||||||||||||||||||||||||||||||||||||
| 373 | To write code that works on all operating systems, pass an array reference. | ||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||
| 375 | =item * B<Suffixes are always separated by ':'.> | ||||||||||||||||||||||||||||||||||||||||||
| 376 | This is true on Windows too. Do not include the dot: C<'.txt'> looks for C<foo..txt>. | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | =item * B<C<undef> and an empty string mean different things.> | ||||||||||||||||||||||||||||||||||||||||||
| 379 | C<undef> suffixes mean "use C<$prefix> as the whole filename". | ||||||||||||||||||||||||||||||||||||||||||
| 380 | An empty string of suffixes means "try no suffixes", so nothing is found. | ||||||||||||||||||||||||||||||||||||||||||
| 381 | C<undef> and empty directories in C<$path> are skipped. | ||||||||||||||||||||||||||||||||||||||||||
| 382 | A missing C<$path> or C<$prefix> is an error. | ||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | =item * B<Check the return value before you use it.> | ||||||||||||||||||||||||||||||||||||||||||
| 385 | Nothing is found when the file is missing, unreadable, a directory, or can't be opened in C<$mode>. | ||||||||||||||||||||||||||||||||||||||||||
| 386 | There is no warning, and C<$!> is not set, so you can't tell these cases apart. | ||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||
| 388 | my ($fh, $filename) = pfopen($path, 'foo', 'txt', '<'); | ||||||||||||||||||||||||||||||||||||||||||
| 389 | die "Can't find foo.txt in $path" unless($fh); | ||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | =item * B<C<$prefix> can contain directories.> | ||||||||||||||||||||||||||||||||||||||||||
| 392 | C<pfopen('/etc', '../tmp/foo')> looks in C</etc/../tmp>. | ||||||||||||||||||||||||||||||||||||||||||
| 393 | If C<$prefix> comes from a user, check it before you call C<pfopen>. | ||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | =head1 ENCODING | ||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | =item * B<Directory names, C<$prefix> and C<$suffixes>> | ||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||
| 403 | These are filenames, and on UNIX a filename is a string of bytes. | ||||||||||||||||||||||||||||||||||||||||||
| 404 | Names in any language, including emojis, work when you pass them as UTF-8 encoded bytes, | ||||||||||||||||||||||||||||||||||||||||||
| 405 | which is how most UNIX systems store filenames: | ||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||
| 407 | use Encode qw(encode); | ||||||||||||||||||||||||||||||||||||||||||
| 408 | my $fh = pfopen($dir, encode('UTF-8', $name), 'txt', '<'); | ||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | If you pass a Perl character string instead, | ||||||||||||||||||||||||||||||||||||||||||
| 411 | the result depends on how Perl stores the string internally. | ||||||||||||||||||||||||||||||||||||||||||
| 412 | For example, C<"caf\x{e9}"> may or may not find C<cafE<eacute>.txt>. | ||||||||||||||||||||||||||||||||||||||||||
| 413 | Encode the name to make the result predictable. | ||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||
| 415 | On Windows, Perl's C<open> function uses the system's "ANSI" code page for filenames, | ||||||||||||||||||||||||||||||||||||||||||
| 416 | so characters that are not in that code page, such as emojis, can't be used in names there. | ||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | A NUL character can't be in any name. See L</Returns>. | ||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||
| 420 | =item * B<C<$mode>> | ||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||
| 422 | The mode itself must be ASCII. | ||||||||||||||||||||||||||||||||||||||||||
| 423 | The layer in the mode can be any layer that Perl supports. | ||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | =item * B<The contents of the file> | ||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||
| 427 | C<pfopen> does not read the file, so the contents can be in any encoding. | ||||||||||||||||||||||||||||||||||||||||||
| 428 | To read or write text as characters, add a layer to the mode, | ||||||||||||||||||||||||||||||||||||||||||
| 429 | for example C<'E<lt>:encoding(UTF-8)'>. | ||||||||||||||||||||||||||||||||||||||||||
| 430 | Then all of Unicode, including emojis, is read and written correctly. | ||||||||||||||||||||||||||||||||||||||||||
| 431 | Without a layer, the file handle reads and writes bytes. | ||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | In taint mode, writing to a file with a tainted name dies. See L</Errors>. | ||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||
| 439 | Would be better if the mode and suffixes options were the other way around, but it's too late to change that now. | ||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||
| 441 | Please report any bugs or feature requests to C<bug-file-pfopen at rt.cpan.org>, | ||||||||||||||||||||||||||||||||||||||||||
| 442 | or through the web interface at | ||||||||||||||||||||||||||||||||||||||||||
| 443 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-pfopen>. | ||||||||||||||||||||||||||||||||||||||||||
| 444 | I will be notified, and then you'll | ||||||||||||||||||||||||||||||||||||||||||
| 445 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | perldoc File::pfopen | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||
| 455 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||
| 457 | =item * MetaCPAN | ||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||
| 459 | L<https://metacpan.org/release/File-pfopen> | ||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||
| 461 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||
| 463 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=File-pfopen> | ||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||
| 465 | =item * CPANTS | ||||||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||||||
| 467 | L<http://cpants.cpanauthors.org/dist/File-pfopen> | ||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | =item * CPAN Testers' Matrix | ||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||
| 471 | L<http://matrix.cpantesters.org/?dist=File-pfopen> | ||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||
| 473 | =item * CPAN Testers Dependencies | ||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||
| 475 | L<http://deps.cpantesters.org/?module=File::pfopen> | ||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||
| 477 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||
| 481 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||
| 483 | =encoding utf-8 | ||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||
| 485 | =head1 FORMAL SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | This section describes C<pfopen> in the Z notation. | ||||||||||||||||||||||||||||||||||||||||||
| 488 | You do not need to read it to use the module. | ||||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||||
| 490 | =head2 pfopen | ||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||
| 492 | [PATH, NAME, HANDLE] | ||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||
| 494 | FILEMODE == { "<", ">", ">>", "+<", "+>", "+>>" } | ||||||||||||||||||||||||||||||||||||||||||
| 495 | MODE == { m : seq CHAR | (∃ f : FILEMODE; l : seq CHAR • m = f ⁀ l ∧ (l = ⟨⟩ ∨ head l = ':')) } | ||||||||||||||||||||||||||||||||||||||||||
| 496 | KEY == NAME × seq NAME × seq PATH | ||||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||
| 498 | isFile, readable : ℙ PATH | ||||||||||||||||||||||||||||||||||||||||||
| 499 | opens : PATH ↔ MODE | ||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||
| 501 | ┌─ PfopenState ─────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||
| 502 | │ cache : KEY ⇸ PATH | ||||||||||||||||||||||||||||||||||||||||||
| 503 | └───────────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||
| 505 | ┌─ InitPfopenState ─────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||
| 506 | │ PfopenState' | ||||||||||||||||||||||||||||||||||||||||||
| 507 | ├───────────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||
| 508 | │ cache' = ∅ | ||||||||||||||||||||||||||||||||||||||||||
| 509 | └───────────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | ┌─ PfopenArgs ──────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||
| 512 | │ dirs? : seq PATH | ||||||||||||||||||||||||||||||||||||||||||
| 513 | │ prefix? : NAME | ||||||||||||||||||||||||||||||||||||||||||
| 514 | │ suffixes? : seq NAME ∪ {⊥} | ||||||||||||||||||||||||||||||||||||||||||
| 515 | │ mode? : MODE | ||||||||||||||||||||||||||||||||||||||||||
| 516 | │ key : KEY | ||||||||||||||||||||||||||||||||||||||||||
| 517 | │ candidates : seq PATH | ||||||||||||||||||||||||||||||||||||||||||
| 518 | │ usable : ℙ PATH | ||||||||||||||||||||||||||||||||||||||||||
| 519 | ├───────────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||
| 520 | │ prefix? ≠ ⟨⟩ | ||||||||||||||||||||||||||||||||||||||||||
| 521 | │ '\0' ∉ ran prefix? | ||||||||||||||||||||||||||||||||||||||||||
| 522 | │ key = (prefix?, suffixes?, dirs?) | ||||||||||||||||||||||||||||||||||||||||||
| 523 | │ suffixes? = ⊥ ⇒ candidates = ⟨ d / prefix? | d ← dirs? ⟩ | ||||||||||||||||||||||||||||||||||||||||||
| 524 | │ suffixes? ≠ ⊥ ⇒ candidates = ⟨ d / (prefix? ⁀ "." ⁀ s) | d ← dirs?, s ← suffixes? ⟩ | ||||||||||||||||||||||||||||||||||||||||||
| 525 | │ usable = { c : PATH | c ∈ isFile ∧ c ∈ readable ∧ (c, mode?) ∈ opens } | ||||||||||||||||||||||||||||||||||||||||||
| 526 | └───────────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||
| 528 | ┌─ PfopenRemembered ────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||
| 529 | │ ΞPfopenState | ||||||||||||||||||||||||||||||||||||||||||
| 530 | │ PfopenArgs | ||||||||||||||||||||||||||||||||||||||||||
| 531 | │ result! : HANDLE × PATH | ||||||||||||||||||||||||||||||||||||||||||
| 532 | ├───────────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||
| 533 | │ key ∈ dom cache ∧ cache(key) ∈ usable | ||||||||||||||||||||||||||||||||||||||||||
| 534 | │ second(result!) = cache(key) | ||||||||||||||||||||||||||||||||||||||||||
| 535 | └───────────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||||
| 537 | ┌─ PfopenFound ─────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||
| 538 | │ ΔPfopenState | ||||||||||||||||||||||||||||||||||||||||||
| 539 | │ PfopenArgs | ||||||||||||||||||||||||||||||||||||||||||
| 540 | │ result! : HANDLE × PATH | ||||||||||||||||||||||||||||||||||||||||||
| 541 | ├───────────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||
| 542 | │ ¬(key ∈ dom cache ∧ cache(key) ∈ usable) | ||||||||||||||||||||||||||||||||||||||||||
| 543 | │ ran candidates ∩ usable ≠ ∅ | ||||||||||||||||||||||||||||||||||||||||||
| 544 | │ second(result!) = head(candidates ↾ usable) | ||||||||||||||||||||||||||||||||||||||||||
| 545 | │ cache' = cache ⊕ { key ↦ second(result!) } | ||||||||||||||||||||||||||||||||||||||||||
| 546 | └───────────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||
| 548 | ┌─ PfopenNotFound ──────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||
| 549 | │ ΔPfopenState | ||||||||||||||||||||||||||||||||||||||||||
| 550 | │ PfopenArgs | ||||||||||||||||||||||||||||||||||||||||||
| 551 | │ result! : {∅} | ||||||||||||||||||||||||||||||||||||||||||
| 552 | ├───────────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||
| 553 | │ ¬(key ∈ dom cache ∧ cache(key) ∈ usable) | ||||||||||||||||||||||||||||||||||||||||||
| 554 | │ ran candidates ∩ usable = ∅ | ||||||||||||||||||||||||||||||||||||||||||
| 555 | │ cache' = {key} ⩤ cache | ||||||||||||||||||||||||||||||||||||||||||
| 556 | └───────────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||||||
| 558 | ┌─ PfopenError ─────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||
| 559 | │ ΞPfopenState | ||||||||||||||||||||||||||||||||||||||||||
| 560 | │ path?, prefix?, mode? : seq CHAR ∪ {⊥} | ||||||||||||||||||||||||||||||||||||||||||
| 561 | │ error! : seq CHAR | ||||||||||||||||||||||||||||||||||||||||||
| 562 | ├───────────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||
| 563 | │ (path? = ⊥ ∨ prefix? = ⊥ ∨ prefix? = ⟨⟩) | ||||||||||||||||||||||||||||||||||||||||||
| 564 | │ ⇒ error! = "Usage: pfopen($path, $prefix, [$suffixes, [$mode]])" | ||||||||||||||||||||||||||||||||||||||||||
| 565 | │ path? is a reference that is not an unblessed array | ||||||||||||||||||||||||||||||||||||||||||
| 566 | │ ⇒ error! = "pfopen: path must be a string or an array reference" | ||||||||||||||||||||||||||||||||||||||||||
| 567 | │ mode? ∉ MODE ∪ {⊥, "", "0"} | ||||||||||||||||||||||||||||||||||||||||||
| 568 | │ ⇒ error! = "pfopen: invalid mode '" ⁀ mode? ⁀ "'" | ||||||||||||||||||||||||||||||||||||||||||
| 569 | └───────────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||||
| 571 | pfopen ≙ PfopenError ∨ PfopenRemembered ∨ PfopenFound ∨ PfopenNotFound | ||||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||
| 573 | The errors are checked in the order shown, and the first one that applies is raised. | ||||||||||||||||||||||||||||||||||||||||||
| 574 | A C<$prefix> or C<$suffixes> containing NUL gives C<PfopenNotFound> without changing C<cache>. | ||||||||||||||||||||||||||||||||||||||||||
| 575 | In taint mode, a call with a tainted argument behaves as if C<key> were not in C<dom cache>, and C<cache' = cache>. | ||||||||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||||||
| 577 | =head1 STATE DIAGRAM | ||||||||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||||||||
| 579 | C<pfopen> keeps one piece of state for each different set of arguments (C<$path>, C<$prefix>, C<$suffixes>): | ||||||||||||||||||||||||||||||||||||||||||
| 580 | either it remembers a filename for them, or it doesn't. | ||||||||||||||||||||||||||||||||||||||||||
| 581 | All lookups start in the FORGOTTEN state when the program starts. | ||||||||||||||||||||||||||||||||||||||||||
| 582 | The labels T1 to T7 are explained in the table below. | ||||||||||||||||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||||||||||||||||
| 584 | program starts | ||||||||||||||||||||||||||||||||||||||||||
| 585 | | | ||||||||||||||||||||||||||||||||||||||||||
| 586 | v | ||||||||||||||||||||||||||||||||||||||||||
| 587 | +-------------+ | ||||||||||||||||||||||||||||||||||||||||||
| 588 | +----->| FORGOTTEN |<-----+ | ||||||||||||||||||||||||||||||||||||||||||
| 589 | | +-------------+ | | ||||||||||||||||||||||||||||||||||||||||||
| 590 | | | | | | | ||||||||||||||||||||||||||||||||||||||||||
| 591 | +--------+ | | | | ||||||||||||||||||||||||||||||||||||||||||
| 592 | T1, T2 | | | | ||||||||||||||||||||||||||||||||||||||||||
| 593 | | | T3 | | ||||||||||||||||||||||||||||||||||||||||||
| 594 | | v | | ||||||||||||||||||||||||||||||||||||||||||
| 595 | +-------------+ | | ||||||||||||||||||||||||||||||||||||||||||
| 596 | +----->| REMEMBERED |------+ | ||||||||||||||||||||||||||||||||||||||||||
| 597 | | +-------------+ T7 | ||||||||||||||||||||||||||||||||||||||||||
| 598 | | | | | ||||||||||||||||||||||||||||||||||||||||||
| 599 | +--------+ | | ||||||||||||||||||||||||||||||||||||||||||
| 600 | T4, T5, T6 | | ||||||||||||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||||||||||||
| 602 | Label From Trigger (a call to pfopen) Action To | ||||||||||||||||||||||||||||||||||||||||||
| 603 | ----- ---------- ------------------------------- --------------------------- ---------- | ||||||||||||||||||||||||||||||||||||||||||
| 604 | T1 FORGOTTEN bad argument croak with an error FORGOTTEN | ||||||||||||||||||||||||||||||||||||||||||
| 605 | T2 FORGOTTEN search finds no usable file return nothing FORGOTTEN | ||||||||||||||||||||||||||||||||||||||||||
| 606 | T3 FORGOTTEN search finds a usable file open it, remember the REMEMBERED | ||||||||||||||||||||||||||||||||||||||||||
| 607 | filename, return the handle | ||||||||||||||||||||||||||||||||||||||||||
| 608 | T4 REMEMBERED bad argument croak with an error REMEMBERED | ||||||||||||||||||||||||||||||||||||||||||
| 609 | T5 REMEMBERED remembered file opens open it, return the handle; REMEMBERED | ||||||||||||||||||||||||||||||||||||||||||
| 610 | don't search | ||||||||||||||||||||||||||||||||||||||||||
| 611 | T6 REMEMBERED remembered file fails, and a open it, remember the new REMEMBERED | ||||||||||||||||||||||||||||||||||||||||||
| 612 | search finds another usable file filename, return the handle | ||||||||||||||||||||||||||||||||||||||||||
| 613 | T7 REMEMBERED remembered file fails, and a forget the filename, FORGOTTEN | ||||||||||||||||||||||||||||||||||||||||||
| 614 | search finds no usable file return nothing | ||||||||||||||||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||||||||||||||||
| 616 | "Return nothing" means C<undef> in scalar context and an empty list in list context. | ||||||||||||||||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||||||||||||||||
| 618 | Notes: | ||||||||||||||||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||||||||||||||||
| 620 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||||||||||||||||
| 622 | =item * "Bad argument" means one of the croaks in L</Errors>. | ||||||||||||||||||||||||||||||||||||||||||
| 623 | It is checked before anything else, so it never changes the state. | ||||||||||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||||||||||
| 625 | =item * A C<$prefix> or C<$suffixes> containing a NUL character returns nothing in either state, and doesn't change the state. | ||||||||||||||||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||||||||||||||||
| 627 | =item * In taint mode, a call with a tainted argument always searches, as if in the FORGOTTEN state, and never changes the state. | ||||||||||||||||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||||||||||||||||
| 629 | =item * "Fails" means the remembered file is gone, is now a directory, or can't be opened in the C<$mode> of this call. | ||||||||||||||||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||||||||||||||||
| 631 | =item * There is no way to forget all the remembered files, except by ending the program. | ||||||||||||||||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||||||||||||||||
| 633 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||||||||||||||||
| 635 | =head1 LICENCE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||||||||||
| 637 | Copyright 2017-2026 Nigel Horne. | ||||||||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||||||||
| 639 | Usage is subject to the GPL2 licence terms. | ||||||||||||||||||||||||||||||||||||||||||
| 640 | If you use it, | ||||||||||||||||||||||||||||||||||||||||||
| 641 | please let me know. | ||||||||||||||||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||||||||||||||||
| 643 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||||||||||||||||
| 645 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||