filetest(3perl)

โœจ filetest โ€” the Perl pragma that controls ur filetest permission operators โœจ

okay bestie, so this is filetest, a <pink>Perl pragma</pink>, and it lives over in the Perl Programmers Reference Guide ๐Ÿ’–. it is so niche and so useful and honestly nobody talks about it. let's get into it ๐Ÿซถ


๐ŸŒธ 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

see how the vibe changes inside the block and then snaps right back?? lexical scoping ate ๐Ÿ’…


๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

so basically, this pragma tells the compiler to change the behaviour of the filetest permission operators, and those operators are -r -w -x -R -W -X (peep perlfunc for the full tea โ˜•).

by default โ€” like, the normal, no-drama default โ€” file test operators just use the simple mode bits, the ones that come back from the stat() family of system calls. that's it, that's the whole personality. cheap, fast, bits.

BUT. many operating systems have extra features that define more complex access rights, for example ACLs (Access Control Lists). and in those environments, use filetest may help the permission operators return results that are 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 โ€” iykyk, they are lexically block-scoped ๐ŸŽ€

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. details down below ๐Ÿ‘‡


๐Ÿ’€ Consider this carefully ๐Ÿ’€

okay bestie, this is the part where i hold ur hand and we look both ways before crossing.

the stat() mode bits are probably right for most of the files and directories on ur system, because honestly few people want to use the additional features that access() offers. but โ€” and this is a big but ๐Ÿ‘ โ€” you may encounter surprises if ur program runs on a system that uses ACLs, because the stat() info won't reflect the actual permissions. not me finding out the hard way ๐Ÿ˜ญ

also there may be a slight performance decrease in the filetest operations while the filetest pragma is in effect, because checking bits is very cheap. like, bits are basically free. access is not free. girl math ๐Ÿงฎ

๐Ÿšจ AND ANOTHER THING โ€” 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?? ๐Ÿ™ˆ nobody, that's who. so if you are serious about security, just try the real operation and test for its success โ€” think in terms of atomic operations. that's the whole move. periodt ๐Ÿ’…

filetests are more useful for filesystem administrative tasks, when you have no need for the content of the elements on disk. admin girlies rise ๐Ÿ‘‘


๐ŸŒˆ 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).

the extended filetest functionality is used by Perl only when the argument of the operators is a filename, not when it is a filehandle. filehandle stans, i'm sorry ๐Ÿซถ


๐ŸŽ€ 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".

{ -d '/etc';
  -w '/etc/passwd';
  print -f _ ? 'Yes' : 'No';   # Yes
}

{ use filetest 'access';
  -d '/etc';
  -w '/etc/passwd';
  print -f _ ? 'Yes' : 'No';   # No
}

like. the same looking code. different answers. the audacity ๐Ÿ˜ญ

of course, unless ur OS does not implement access(), in which case the pragma is simply ignored. so:

โญ best not to use "_" at all in a file where the filetest pragma is active! โญ

as a side effect, because "_" doesn't work, stacked filetest operators (-f -w $file) won't work either. ๐Ÿ’”

this limitation might be removed in a future version of perl. manifesting ๐Ÿ•ฏ๏ธโœจ


and that's the whole page, babes ๐ŸŒธ this was filetest(3perl) from the Perl Programmers Reference Guide, perl v5.44.0, dated 2026-10-17 โ€” go forth and vibe check ur permissions responsibly ๐Ÿ’–๐Ÿฆ‹๐Ÿ’…

๐Ÿ“ฆ where this page comes from
  • arch rolling: perl 5.44.0-1 ยท Artistic-1.0-Perl AND GPL-1.0-or-later
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms