Chapter 24. File Test Operators

These unary operators take one argument, either a filename or a filehandle, and test the associated file to see if something is true about it. If the argument is omitted, they test $_ (except for -t, which tests STDIN). If the special argument _ (underscore) is passed, they use the information from the preceding test or stat call.

See also the filetest pragma.

-r -w -x

File is readable/writable/executable by effective uid/gid.

-R -W -X

File is readable/writable/executable by real uid/gid.

-o -0

File is owned by effective/real uid.

-e -z

File exists/has zero size.

-s

File exists and has non-zero size. Returns the size.

-f -d

File is a plain file/a directory.

-l -S -p

File is a symbolic link/a socket/a named pipe (FIFO).

-b -c

File is a block/character special file.

-u -g -k

File has setuid/setgid/sticky bit set.

-t

Filehandle (default STDIN) is opened to a tty.

-T -B

File is a text/non-text (binary) file. These tests return true on an empty file, or a file at EOF when testing a filehandle.

-M -A -C

Returns the modification/access/inode-change time of the file. The value is relative to the time the program started and expressed in fractional days. See also $^T.

Get Perl Pocket Reference, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.