Webmaster in a Nutshell

Previous Chapter 15
Perl Quick Reference

15.17 Input/Output

In input/output operations, filehandle may be a filehandle as opened by the open operator, a predefined filehandle (e.g., STDOUT), or a scalar variable that evaluates to the name of a filehandle to be used.


In scalar context, reads a single line from the file opened on filehandle. In array context, reads the whole file.


Reads from the input stream formed by the files specified in @ARGV, or standard input if no arguments were supplied.

binmode filehandle

Arranges for the file opened on filehandle to be read or written in binary mode as opposed to text mode (null-operation on UNIX).

close filehandle

Closes the file or pipe associated with the filehandle.

dbmclose %hash

Deprecated, use untie instead.

dbmopen %hash, dbmname, mode

Deprecated, use tie instead.

eof filehandle

Returns true if the next read will return end of file, or if the file is not open.


Returns the EOF status for the last file read.


Indicates EOF on the pseudo file formed of the files listed on the command line.

fcntl filehandle, function, $var

Implements the fcntl(2) function. This function has nonstandard return values.

fileno filehandle

Returns the file descriptor for a given (open) file.

flock filehandle, operation

Calls flock(2) on the file. operation formed by adding 1 (shared), 2 (exclusive), 4 (non-blocking), or 8 (unlock).

getc [ filehandle ]

Yields the next character from the file, or an empty string on end of file. If filehandle is omitted, reads from STDIN.

ioctl filehandle, function, $var

Performs ioctl(2) on the file. This function has nonstandard return values.

open filehandle [ , filename ]

Opens a file and associates it with filehandle. If filename is omitted, the scalar variable of the same name as the filehandle must contain the filename.

The following filename conventions apply when opening a file:


Open file for input. Also "<file".


Open file for output, creating it if necessary.


Open file in append mode.


Open file with read/write access.

"| cmd"

Opens a pipe to command cmd; forks if cmd is -.

"cmd |"

Opens a pipe from command cmd; forks if cmd is -.

file may be &filehnd, in which case the new filehandle is connected to the (previously opened) filehandle filehnd. If it is &=n, file will be connected to the given file descriptor. open returns undef upon failure, true otherwise.

pipe readhandle, writehandle

Returns a pair of connected pipes.

print [ filehandle ] [ list(dagger) ]

Prints the elements of list, converting them to strings if needed. If filehandle is omitted, prints by default to standard output (or to the last selected output channel, see select).

printf [ filehandle ] [ list ]

Equivalent to print filehandle and sprintf list.

read filehandle, $var, length [ , offset ]

Reads length binary bytes from the file into the variable at offset. Returns number of bytes actually read.

seek filehandle, position, whence

Arbitrarily positions the file. Returns true if successful.

select [ filehandle ]

Returns the currently selected filehandle. Sets the current default filehandle for output operations if filehandle is supplied.

select rbits, wbits, nbits, timeout

Performs a select(2) system call with the same parameters.

sprintf format, list

Returns a string formatted by (almost all of) the usual printf(3) conventions.

sysread filehandle, $var, length [ , offset ]

Reads length bytes into $var at offset.

syswrite filehandle, scalar, length [ , offset ]

Writes length bytes from scalar at offset.

tell [ filehandle ]

Returns the current file position for the file. If filehandle is omitted, assumes the file last read.

Previous Home Next
File Operations Book Index Formats

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell
Hosted by uCoz