File test operators
homencsa resources partnershipsoutreachsoftware_tech
alliance searchinside ncsa adv_computingsciencedivisions
left up right comment

File test operators

So far, we haven't seen a lot of the power that shells incorporate. One feature perl borrows is simple file test operators. Generally, the test operations are used as part of an expression for a conditional. Each operator takes a scalar value as the filename to test. Note that these are operators and not functions, ie no paranthesis.
 
	# procedure to check if the setuid bit is on 
	sub checkSUID { 
		return( -u $_[0] ); 
	} 
Some file test operators
-r readable
-x executable
-e exists
-d is a directory
-t is a tty
-T text file

Example

 
################################################################ 
# Check Security 
################################################################ 
# Users who wish to use this program must 
# put the file ".allowmailform" in their home directory on the  
# machine running this script.  We don't want people creating 
# forms that are abused to send "anonymous" mail. 
$homedir = (getpwnam($recipient))[7]; 
if (! -e "$homedir/.allowmailform") { 
	# this user does not permit the server to send mail to him.  Quit. 
	&error("The recipient of the form has not enabled access for this form");    exit(0); 
} 
Note the interface to the system through getpwnam. One reason perl is so widely used among the system administration community is that it offers builtins to access things like password files (possibly through NIS or similar) just as if it was C.

Note the clever subscripting of the list that getpwnam returns. getpwnam returns all the information in the password file for a user. We take that result in a list context and get the eighth element.


left up right comment



NCSA
The National Center for Supercomputing Applications

University of Illinois at Urbana-Champaign

johnsonb@ncsa.uiuc.edu

Last modified: June 19, 1997



Retrieved by Memoweb from http://www.ncsa.uiuc.edu/General/Training/PerlIntro/filetest.html at 08/02/99