filetest(3pm)
β¨ NAME β¨
okay bestie, you're looking at filetest(3pm) β the Perl pragma that controls the filetest permission operators. literally the permission vibe-check pragma. no cap. π
πΈ SYNOPSIS πΈ
$can_perhaps_read = -r "file"; # use the mode bits
{
use filetest 'access'; # intuit harder
$can_really_read = -r "file";
}
$can_perhaps_read = -r "file"; # use the mode bits again
π DESCRIPTION π
So the way that this works: this pragma tells the compiler to change the behaviour of the filetest permission operators β -r, -w, -x, -R, -W, -X (see perlfunc). iconic little operators, honestly.
By default, file test operators use the simple mode bits as returned by the stat() family of system calls. but bestie, many operating systems have additional features to define more complex access rights β for example ACLs (Access Control Lists). for those environments, use filetest may help the permission operators return results more consistent with other tools. π¦
The use filetest or no filetest statements affect file tests defined in their block, up to the end of the closest enclosing block β they are lexically block-scoped. periodt, the scope stays in its lane.
Currently, only the "access" sub-pragma is implemented. It enables (or disables) the use of access() when available, that is, on most UNIX systems and other POSIX environments. See details below. π
π«Ά Consider this carefully π«Ά
lowkey, the stat() mode bits are probably right for most of the files and directories found on your system, because few people want to use the additional features offered by access(). but highkey you may encounter surprises if your program runs on a system that uses ACLs, since the stat() information won't reflect the actual permissions. π
There may be a slight performance decrease in the filetest operations when the filetest pragma is in effect, because checking bits is very cheap. like, the bits themselves are cheap β the pragma is the extra little detour. π
Also, note that using the file tests for security purposes is a lost cause from the start: there is a window open for race conditions β who is to say that the permissions will not change between the test and the real operation? therefore if you are serious about security, just try the real operation and test for its success β think in terms of atomic operations. fr fr, do the actual thing and check if it worked. π₯
Filetests are more useful for filesystem administrative tasks, when you have no need for the content of the elements on disk. that's the sweet spot, bestie. β¨
π§ The "access" sub-pragma π§
UNIX and POSIX systems provide an abstract access() operating system call, which should be used to query the read, write, and execute rights. This function hides various distinct approaches in additional operating system specific security features, like Access Control Lists (ACLs). it's giving "one call, many OS secrets." π«Ά
The extended filetest functionality is used by Perl only when the argument of the operators is a filename, not when it is a filehandle. so filename in, extended vibes in; filehandle in, that's a different situation. π
π Limitation with regard to "_" π
Because access() does not invoke stat() (at least not in a way visible to Perl), the stat result cache "_" is not set. This means that the outcome of the following two tests is different. The first has the stat bits of /etc/passwd in "_", and in the second case this still contains the bits of /etc. not me finding out the cache is doing its own thing. π
test one:
{ -d '/etc';
-w '/etc/passwd';
print -f _ ? 'Yes' : 'No'; # Yes
}
test two:
{ use filetest 'access';
-d '/etc';
-w '/etc/passwd';
print -f _ ? 'Yes' : 'No'; # No
}
Of course, unless your OS does not implement access(), in which case the pragma is simply ignored. Best not to use "_" at all in a file where the filetest pragma is active! seriously, bestie, don't do it. π
As a side effect, as "_" doesn't work, stacked filetest operators (-f -w $file) won't work either. that's the tea. π
This limitation might be removed in a future version of perl. so, maybe one day. but today? today we respect the limitation. π
π that's the whole filetest(3pm) group-chat recap β brought to you by perl v5.42.3, dated 2026-11-03. now go check those permissions, bestie! ππ
π¦
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms