Previous Page Next Page

10.3. File Testing

Like the shells, Perl provides a number of file test operators (see Table 10.3) to check for the various attributes of a file, such as existence, access permissions, directories, files, and so on. Most of the operators return 1 for true and "" (null) for false.

Table 10.3. File Test Operators[a]
OperatorMeaning
r $fileTrue if $file is a readable file.
w $fileTrue if $file is a writeable file.
x $fileTrue if $file is an executable file.
o $fileTrue if $file is owned by effective uid.
e $fileTrue if file exists.
z $fileTrue if file is zero in size.
s $fileTrue if $file has nonzero size. Returns the size of the file in bytes.
f $fileTrue if $file is a plain file.
d $fileTrue if $file is a directory file.
l $fileTrue if $file is a symbolic link.
p $fileTrue if $file is a named pipe or FIFO.
S $fileTrue if $file is a socket.
b $fileTrue if $file is a block special file.
c $fileTrue if $file is a character special file.
u $fileTrue if $file has a setuid bit set.
g $fileTrue if $file has a setgid bit set.
k $fileTrue if $file has a sticky bit set.
t $fileTrue if filehandle is opened to a tty.
T $fileTrue if $file is a text file.
B $fileTrue if file is a binary file.
M $fileAge of the file in days since modified.
A $fileAge of the file in days since last accessed.
C $fileAge of the file in days since the inode changed.


[a] If a filename is not provided, $_ is the default.

A single underscore can be used to represent the name of the file if the same file is tested more than once. The stat structure of the previous file test is used.

Example 10.34.

(At the Command Line)
1   $ ls -l perl.test
    -rwxr-xr-x  1 ellie         417 Apr 23 13:40 perl.test
2   $ ls -l afile
    -rws--x--x  1 ellie           0 Apr 23 14:07 afile

(In Script)
    #!/usr/bin/perl
    $file=perl.test;

3   print "File is readable\n" if -r  $file;
    print "File is writeable\n" if -w  $file;
    print "File is executable\n" if -x  $file;
    print "File is a regular file\n" if -f  $file;
    print "File is a directory\n" if -d $file;
    print "File is text file\n" if -T $file;
    printf "File was last modified %f days ago.\n", -M $file;
    print "File has been accessed in the last 12 hours.\n" if -M <= 12;
4   print "File has read, write, and execute set.\n"
           if -r $file && -w _ && -x _;
5   stat("afile");  # stat another file
    print "File is a set user id program.\n" if  -u _;
                    # underscore evaluates to last file stat'ed
    print "File is zero size.\n" if -z_;

(Output)
3   File is readable
    File is writeable
    File is executable
    File is a regular file
    *** No print out here because the file is not a directory ***
    File is text file
    File was last modified 0.000035 days ago.
    File has read, write, and execute set.
    File is a set user id program.
    File is zero size.

					  

Explanation

  1. The permissions, ownership, file size, etc., on perl.test are shown.

  2. The permissions, ownership, file size, etc., on afile are shown.

  3. The print statement is executed if the file is readable, writeable, executable, etc.

  4. Since the same file is checked for more than one attribute, an underscore is appended to the file test flag. The underscore references the stat[a] structure, an array that holds information about the file.

    [a] Read more about the stat structure in Chapter 18, "Interfacing with the System."

  5. The stat function returns a 13-element array containing the statistics about a file. As long as the underscore is appended to the file test flag, the statistics for afile are used in the tests that follow.

[a] Read more about the stat structure in Chapter 18, "Interfacing with the System."

[a] Read more about the stat structure in Chapter 18, "Interfacing with the System."

Previous Page Next Page