The Filetest Operators
#!/usr/bin/perl -w
###################### Demo of Filetest Operators #######################
open (F, "foo1") or die "Cannot open foo1.\n";
print "Empty file\n" if -z F; ####### Filetests can use HANDLES or ...
close F;
print "Empty file\n" if -z "foo1"; ####### They can use literal filenames.
print -s "foo2","\n" if ! -z "foo2";
print "Foo1 is a text file!\n" if -T "foo1";
print "Print $ENV{HOME}/perl is a directory!\n" if -d "../perl";
print "Last modified file asg1 ", -M "asg1", " days ago!\n";
print "Last accessed file asg1 ", -A "asg1", " days ago!\n";
print "asg2 was modified more recently than asg1\n"
if (-M "asg1" > -M "asg2");
print "asg1 was modified more recently than asg2\n"
if (-M "asg2" > -M "asg1");
print "File hello is a ", -B "hello" ? "binary file\n" : "text file\n";
print "File foo4 is a symbolic link.\n" if -l "foo4";
system("ls -ldF foo1 foo2 ../perl asg1 asg2 hello foo4");
####################### Execution Results Below #########################
Empty file ###### foo1 created by Unix touch command.
Empty file
6 ###### size of foo2 is 6 bytes (12345\n)
Foo1 is a text file! ###### even though it has no text!
Print /home/jwp2286/perl is a directory!
Last modified file asg1 130.349143518519 days ago!
Last accessed file asg1 3.29377314814815 days ago!
asg1 was modified more recently than asg2
File hello is a binary file
File foo4 is a symbolic link.
#################### ls -ldF of files manipulated above ##################
drwxr-xr-x 4 jwp2286 staff 3584 Feb 1 23:49 ../perl/
-rw------- 1 jwp2286 staff 1018 Sep 24 16:28 asg1
-rw------- 1 jwp2286 staff 242 Sep 23 13:12 asg2
-rw------- 1 jwp2286 staff 0 Feb 1 23:30 foo1
-rw------- 1 jwp2286 staff 6 Feb 1 23:35 foo2
lrwx------ 1 jwp2286 staff 4 Feb 1 23:46 foo4 -> foo3
-rwx------ 1 jwp2286 staff 13020 Feb 1 23:31 hello*
############################# FileTest Summary ###########################
-e HANDLE_OR_NAME #### True if file exists.
-s HANDLE_OR_NAME #### Filesize.
-T HANDLE_OR_NAME #### True if file is a text file.
-d HANDLE_OR_NAME #### True if file is a directory.
-z HANDLE_OR_NAME #### True if file is zero length.
-M HANDLE_OR_NAME #### Age of file in days (floating point fractional!).
-A HANDLE_OR_NAME #### Last access time in days (also floating point!).
-l HANDLE_OR_NAME #### True if file is a soft (symbolic) link.
-B HANDLE_OR_NAME #### True if binary. Directories are binaries!!!
-r -w or -x then H_OR_N #### True if file is readable/writeable/executable.