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 a reference to or the name of a filehandle to be used.
<
filehandle>
In scalar context, reads a single record, usually a line, from the file opened on filehandle. In list context, reads the rest of the file.
<>
<ARGV>
Reads from the input stream formed by the files specified in @ARGV,
or standard input if no arguments were supplied.
,
layers ]Arranges for the file opened on filehandle to be read or written using the specified I/O layers (default: :raw
). For a list of Standard I/O Layers.
Closes the filehandle. Resets $.
if it was an input file. If filehandle is omitted, closes the currently selected filehandle.
Closes the file associated with the hash. Superseded by untie.
%
hash, dbmname, modeOpens a dbm file and associates it with the hash. Superseded by tie.
Returns true if the next read will return EOF (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.
,
function, $
varCalls system-dependent file control functions.
Returns the file descriptor for a given (open) file.
,
operationCalls a system-dependent locking routine on the file. operation is formed by adding one or more values or LOCK_
constants from the table.
Returns the next character from the file, or an empty string on end of file. If filehandle is omitted, reads from STDIN.
,
function, $
varCalls system-dependent I/O control functions.
,
modeandname ],
mode,
name [ ,
. . . ]Opens a file and associates it with filehandle. If filehandle is an uninitialized scalar variable, a new, unique filehandle is automatically created.
modeandname must contain the name of the file, prefixed with the mode with which to open it. If modeandname is not provided, a global (package) variable with the same name as filehandle must provide the mode and name.
See general Open Modes.
In modeandname, -
may be used to designate standard input or output. Whitespace is allowed, as a consequence this form of open cannot easily be used to open files with names that start or end with whitespace.
The form with three or more arguments allows more control over the open mode and file name.
If name is undef, an anonymous temporary file is opened. If name is a reference to a scalar, the contents of the scalar are read from or written to.
mode may have a list of I/O layers appended that will be applied to the handle. For a list of Standard I/O Layers.
,
writehandleCreates a pair of connected pipes. If either handle is an uninitialized scalar variable, a new, unique filehandle is automatically created.
Prints the elements of list, converting them to strings if needed. If filehandle is omitted, prints to the currently selected output handle.
Equivalent to print filehandle sprintf list.
, $
var,
length [ ,
offset]Reads length characters from the file into the variable at offset. Returns the number of characters actually read, 0 on EOF, and undef on failure.
Internal function that implements the <>
operator.
Internal function that implements the qx operator. expr is executed as a system command.
,
position,
whenceArbitrarily positions the file on a byte position. whence can be one of the values or SEEK_
constants from the table.
Sets the current default filehandle for output operations if filehandle is supplied. Returns the currently selected filehandle.
,
wbits,
nbits,
timeoutPerforms a select syscall with the same parameters.
,
listReturns a string resulting from formatting a (possibly empty) list of values. See the section Chapter 27 for a complete list of format conversions. See the section Chapter 28 for an alternative way to obtain formatted output.
,
path,
mode [ ,
perms ]Performs an open syscall. The possible values and flag bits of mode and perms are system-dependent; they are available via the standard module Fcntl.
If filehandle is an uninitialized scalar variable, a new, unique filehandle is automatically created.
mode is formed by adding one or more values or 0_
constants from the table.
, $
var,
length [ ,
offset ]Reads length characters into $
var at offset. Returns the number of characters actually read, 0 on EOF, and undef on failure.
,
position,
whenceArbitrarily positions the file on a byte position, for use with sysread and syswrite. whence can be one of the values or SEEK_
constants from the table.
,
scalar [ ,
length [ ,
offset ] ]Writes length characters from scalar at offset. Returns the number of characters actually written, or undef if there was an error.
Returns the current byte position for the file. If filehandle is omitted, assumes the file last read.
The following modes are valid for all forms of open:
| Input only. This is the default when the mode is empty. |
| Output only. The file is created or truncated if necessary. |
| Open the file in append mode. The file is created if necessary. |
| Read/write update access. |
| Write/read update access. |
| Read/append access. |
These modes may be followed by &
to duplicate an already opened filehandle or, if numeric, file descriptor. Use &=
with a numeric argument to create an alias to the already opened file descriptor.
Modes for the two-argument open include:
| Opens a pipe to read from or write to a command. |
| Forks, with the file connected to the standard input of the child. |
| Forks, with the file connected to the standard output of the child. |
Modes for the three-argument open include:
| Opens a pipe to write to a command. |
| Opens a pipe to read from a command. |
Several input/output related constants can be imported from the standard module Fcntl.
Constants related to open and sysopen are imported by default. For some constants, the widely accepted values are shown in octal.
Description | ||
|
| Read-only access. |
|
| Write-only access. |
|
| Read and write access. |
|
| Create the file if nonexistent. |
|
| Fail if the file already exists. |
|
| Append data to the end of the file. |
|
| Truncate the file. |
| Nonblocking input/output. | |
| Same as | |
| Synchronous input/output. | |
| Lock exclusive. | |
| Lock shared. | |
| File must be a directory. | |
| Do not follow symlinks. | |
| Use binary mode for input/output. | |
| Allow file to be larger than 4 GB. | |
| Terminal will not become the controlling tty. |
Constants related to seek and sysseek must be imported explictly by specifying :seek
in the import list of Fcntl.
Value | Name | Description |
|
| Seek position. |
|
| Seek offset from current position. |
|
| Seek offset from end of file. |
Constants related to flock must be imported explictly by specifying :flock
in the import list of Fcntl.
:bytes
Use 8-bit bytes, as opposed to :utf8.
:crlf
Do CR/LF to newline translation, and vice versa.
:encoding(
enc )
Select a specific encoding.
:perlio
Use Perl’s “PerlIO” implementation.
:raw
Use low-level I/O.
:stdio
Use the system’s “standard I/O” implementation.
:unix
Use Unix-style low-level I/O.
:utf8
Use Perl’s internal encoding of Unicode.
:Via
(module)Use the specified module to handle the I/O.
:win32
Use native I/O (Microsoft Windows platforms only).