Perl/FileFunctions

File Functions

The following file functions are available in Perl:

* binmode(FILE_HANDLE) This function puts FILE_HANDLE into a binary mode. * chdir( DIR_NAME) Causes your program to use DIR_NAME as the current directory. It will return true if the change was successful, false if not. * chmod(MODE, FILE_LIST) This UNIX-based function changes the permissions for a list of files. A count of the number of files whose permissions was changed is returned. There is no DOS equivalent for this function. * chown(UID, GID, FILE_LIST) This UNIX-based function changes the owner and group for a list of files. A count of the number of files whose ownership was changed is returned. There is no DOS equivalent for this function. * close(FILE_HANDLE) Closes the connection between your program and the file opened with FILE_HANDLE. * closedir( DIR_HANDLE) Closes the connection between your program and the directory opened with DIR_HANDLE. * eof(FILE_HANDLE) Returns true if the next read on FILE_HANDLE will result in hitting the end of the file or if the file is not open. If FILE_HANDLE is not specified the status of the last file read is returned. All input functions return the undefined value when the end of file is reached, so you'll almost never need to use eof. * fcntl(FILE_HANDLE, Implements the fcntl function which lets FUncTION, SCALAR) you perform various file control operations. Its use is beyond the scope of this course. * fileno( FILE_HANDLE) Returns the file descriptor for the specified FILE_HANDLE. * flock(FILEHANDLE, OPERATION) This function will place a lock on a file so that multiple users or programs can't simultaneously use it. The flock function is beyond the scope of this book. * getc(FILE_HANDLE) Reads the next character from FILE_HANDLE. If FILE_HANDLE is not specified, a character will be read from STDIN. glob( EXPRESSION) Returns a list of files that match the specification of EXPRESSION, which can contain wildcards. For instance, glob( "*.pl") will return a list of all Perl program files in the current directory. * ioctl(FILE_HANDLE, Implements the ioctl function which lets FUncTION, SCALAR) you perform various file control operations. Its use is beyond the scope of this book. For more in-depth discussion of this function see Que's Special Edition Using Perl for Web Programming. * link(OLD_FILE_NAME, This UNIX-based function creates a new NEW_FILE_NAME) file name that is linked to the old file name. It returns true for success and false for failure. There is no DOS equivalent for this function. lstat( FILE_HANDLE_OR_Returns file statistics in a 13-element array. FILE_NAME) lstat is identical to stat except that it can also return information about symbolic links. * mkdir(DIR_NAME, MODE) Creates a directory named DIR_NAME. If you try to create a subdirectory, the parent must already exist. This function returns false if the directory can't be created. The special variable $! is assigned the error message. * open(FILE_HANDLE, EXPRESSION) Creates a link between FILE_HANDLE and a file specified by EXPRESSION. * opendir( DIR_HANDLE, DIR_NAME) Creates a link between DIR_HANDLE and the directory specified by DIR_NAME. opendir returns true if successful, false otherwise. * pipe(READ_HANDLE), Opens a pair of connected pipes like the WRITE_HANDLE) corresponding system call. Its use is beyond the scope of this book. For more on this function see Que's Special Edition Using Perl for Web Programming. print FILE_HANDLE (LIST) Sends a list of strings to FILE_HANDLE. If FILE_HANDLE is not specified, then STDOUT is used.   * printf FILE_HANDLESends a list of strings in a format specified by (FORMAT, LIST) FORMAT to FILE_HANDLE. If FILE_HANDLE is not specified, then STDOUT is used.    * read(FILE_HANDLE, BUFFER, Reads bytes from FILE_HANDLE starting at LENGTH,LENGTHOFFSET) OFFSET position in the file into the scalar variable called BUFFER. It returns the number of bytes read or the undefined value.    * readdir(DIR_HANDLE) Returns the next directory entry from DIR_HANDLE when used in a scalar context. If used in an array context, all of the file entries in DIR_HANDLE will be returned in a list. If there are no more entries to return, the undefined value or a null list will be returned depending on the context. * readlink(EXPRESSION) This UNIX-based function returns that value of a symbolic link. If an error occurs, the undefined value is returned and the special variable $! is assigned the error message. The $_ special variable is used if EXPRESSION is not specified. * rename(OLD_FILE_NAME, Changes the name of a file. You can use this NEW_FILE_NAME) function to change the directory where a file resides, but not the disk drive or volume. * rewinddir(DIR_HANDLE) Resets DIR_HANDLE so that the next readdir starts at the beginning of the directory. * rmdir(DIR_NAME) Deletes an empty directory. If the directory can be deleted it returns false and $! is assigned the error message. The $ special variable is used if DIR_NAME is not specified. * seek(FILE_HANDLE, POSITION, Moves to POSITION in the file connected to WHEncE) FILE_HANDLE. The WHEncE parameter determines if POSITION is an offset from the beginning of the file ( WHEncE=0), the current position in the file (WHEncE=1), or the end of the file (WHEncE=2). * seekdir(DIR_HANDLE, POSITION) Sets the current position for readdir. POSITION must be a value returned by the telldir function. * select(FILE_HANDLE) Sets the default FILE_HANDLE for the write and print functions. It returns the currently selected file handle so that you may restore it if needed. * sprintf(FORMAT, LIST) Returns a string whose format is specified by FORMAT. * stat( FILE_HANDLE_OR_Returns file statistics in a 13-element array. FILE_NAME) * symlink(OLD_FILE_NAME, This UNIX-based function creates a new NEW_FILE_NAME) file name symbolically linked to the old file name. It returns false if the NEW_FILE_NAME cannot be created. * sysread(FILE_HANDLE, BUFFER, Reads LENGTH bytes from FILE_HANDLE starting LENGTH,OFFSET) at OFFSET position in the file into the scalar variable called BUFFER. It returns the number of bytes read or the undefined value. * syswrite(FILE_HANDLE, BUFFER, Writes LENGTH bytes from FILE_HANDLE starting LENGTH, OFFSET) at OFFSET position in the file into the scalar variable called BUFFER. It returns the number of bytes written or the undefined value. * tell(FILE_HANDLE) Returns the current file position for FILE_HANDLE. If FILE_HANDLE is not specified, the file position for the last file read is returned. * telldir(DIR_HANDLE) Returns the current position for DIR_HANDLE. The return value may be passed to seekdir to access a particular location in a directory. * truncate(FILE_HANDLE, LENGTH) Truncates the file opened on FILE_HANDLE to be LENGTH bytes long. * unlink(FILE_LIST) Deletes a list of files. If FILE_LIST is not specified, then $ will be used. It returns the number of files successfully deleted. Therefore, it returns false or 0 if no files were deleted. * utime( FILE_LIST) This UNIX-based function changes the access and modification times on each file in FILE_LIST. * write(FILE_HANDLE) Writes a formatted record to FILE_HANDLE.