1filetest(3pm) Perl Programmers Reference Guide filetest(3pm)
2
3
4
6 filetest - Perl pragma to control the filetest permission operators
7
9 $can_perhaps_read = -r "file"; # use the mode bits
10 {
11 use filetest 'access'; # intuit harder
12 $can_really_read = -r "file";
13 }
14 $can_perhaps_read = -r "file"; # use the mode bits again
15
17 This pragma tells the compiler to change the behaviour of the filetest
18 permission operators, "-r" "-w" "-x" "-R" "-W" "-X" (see perlfunc).
19
20 The default behaviour of file test operators is to use the simple mode
21 bits as returned by the stat() family of system calls. However, many
22 operating systems have additional features to define more complex
23 access rights, for example ACLs (Access Control Lists). For such
24 environments, "use filetest" may help the permission operators to
25 return results more consistent with other tools.
26
27 The "use filetest" or "no filetest" statements affect file tests
28 defined in their block, up to the end of the closest enclosing block
29 (they are lexically block-scoped).
30
31 Currently, only the "access" sub-pragma is implemented. It enables (or
32 disables) the use of access() when available, that is, on most UNIX
33 systems and other POSIX environments. See details below.
34
35 Consider this carefully
36 The stat() mode bits are probably right for most of the files and
37 directories found on your system, because few people want to use the
38 additional features offered by access(). But you may encounter
39 surprises if your program runs on a system that uses ACLs, since the
40 stat() information won't reflect the actual permissions.
41
42 There may be a slight performance decrease in the filetest operations
43 when the filetest pragma is in effect, because checking bits is very
44 cheap.
45
46 Also, note that using the file tests for security purposes is a lost
47 cause from the start: there is a window open for race conditions (who
48 is to say that the permissions will not change between the test and the
49 real operation?). Therefore if you are serious about security, just
50 try the real operation and test for its success - think in terms of
51 atomic operations. Filetests are more useful for filesystem
52 administrative tasks, when you have no need for the content of the
53 elements on disk.
54
55 The "access" sub-pragma
56 UNIX and POSIX systems provide an abstract access() operating system
57 call, which should be used to query the read, write, and execute
58 rights. This function hides various distinct approaches in additional
59 operating system specific security features, like Access Control Lists
60 (ACLs)
61
62 The extended filetest functionality is used by Perl only when the
63 argument of the operators is a filename, not when it is a filehandle.
64
65 Limitation with regard to "_"
66 Because access() does not invoke stat() (at least not in a way visible
67 to Perl), the stat result cache "_" is not set. This means that the
68 outcome of the following two tests is different. The first has the
69 stat bits of /etc/passwd in "_", and in the second case this still
70 contains the bits of "/etc".
71
72 { -d '/etc';
73 -w '/etc/passwd';
74 print -f _ ? 'Yes' : 'No'; # Yes
75 }
76
77 { use filetest 'access';
78 -d '/etc';
79 -w '/etc/passwd';
80 print -f _ ? 'Yes' : 'No'; # No
81 }
82
83 Of course, unless your OS does not implement access(), in which case
84 the pragma is simply ignored. Best not to use "_" at all in a file
85 where the filetest pragma is active!
86
87 As a side effect, as "_" doesn't work, stacked filetest operators ("-f
88 -w $file") won't work either.
89
90 This limitation might be removed in a future version of perl.
91
92
93
94perl v5.28.2 2018-03-01 filetest(3pm)