|-f is not the opposite of -d||If youre traversing a tree and want to distinguish files from directories to descend into then -d should be used so device files or pipes can be processed.|
|-f doesnt mean readable/writable/seekable||
Char specials and named pipes are perfectly good for reading and writing,
and char specials can support seeking. Demanding -f is an unnecessary
restriction. You might only ever use ordinary files normally, but theres
no need to prevent someone else running it on a tape drive, /dev/null,
etc. You always have to test each seek() etc for success anyway, and
that will tell you if a file is seekable.
|-e is better than -f||A few inflexible functions or operations may not have good file not found behaviour and may force you to check for a file before invoking. Using -e is better than -f since as described above it doesnt unnecessarily disallow device files.|
|-f before opening is a race condition||
Testing a filename before opening is bad. Any test before opening is
useless because the file can change or be removed in between the test and
If you want to know if the file can be opened then open the file! The error return from open() must be checked, so a test beforehand only duplicates that, and is an opportunity to wrongly presume what the system or the users permissions can or cant do.
If you really do want to enquire into the nature of the file, in order to only accept ordinary files, then open first and then -f on the handle. But thats unusual except for an archiving or backup program.
Incidentally, for error message in $! is normally the best thing to print. It can be slightly technical, but its wording will at least be familiar from other programs and will be translated into the users locale language.
Most uses of -f tend to shell script style code written in Perl. In the shell its usually not possible to do better than such tests (though -d or -e is generally still better than -f), but in Perl it is.
A blanket prohibition like this policy is harsh, but is meant as a building block or at least to make you think carefully whether -f is really right. As always though you can disable ProhibitFiletest_f from your .perlcriticrc in the usual way (see CONFIGURATION in Perl::Critic),
Perl::Critic::Pulp, Perl::Critic, open in perlfunc, ERRNO in POSIX, Errno, errno(3)
Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015 Kevin Ryde
Perl-Critic-Pulp is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.
Perl-Critic-Pulp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses>.
|perl v5.20.3||PERL::CRITIC::POLICY::VALUESANDEXPRESSIONS::PROHIBITFILETEST_F (3)||2015-01-04|