Chapter 26. Input and 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 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.

binmode filehandle [ , 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.

close [ filehandle ]

Closes the filehandle. Resets $. if it was an input file. If filehandle is omitted, closes the currently selected filehandle.

dbmclose %hash

Closes the file associated with the hash. Superseded by untie.

dbmopen %hash, dbmname, mode

Opens a dbm file and associates it with the hash. Superseded by tie.

eof filehandle

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

eof

Returns the EOF status for the last file read.

eof()

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

fcntl filehandle, function, $var

Calls system-dependent file control functions.

fileno filehandle

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

flock filehandle, operation

Calls a system-dependent locking routine on the file. operation is formed by adding one or more values or LOCK_ constants from the table.

getc [ filehandle ]

Returns 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

Calls system-dependent I/O control functions.

open filehandle [ , modeandname ]

open filehandle, 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.

pipe readhandle, writehandle

Creates a pair of connected pipes. If either handle is an uninitialized scalar variable, a new, unique filehandle is automatically created.

print [ filehandle ] list

Prints the elements of list, converting them to strings if needed. If filehandle is omitted, prints to the currently selected output handle.

printf [ filehandle ] list

Equivalent to print filehandle sprintf list.

read filehandle, $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.

readline expr

Internal function that implements the <> operator.

readpipe scalar expr

Internal function that implements the qx operator. expr is executed as a system command.

seek filehandle, position, whence

Arbitrarily positions the file on a byte position. whence can be one of the values or SEEK_ constants from the table.

select [ filehandle ]

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

select rbits, wbits, nbits, timeout

Performs a select syscall with the same parameters.

sprintf format, list

Returns 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.

sysopen filehandle, 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.

sysread filehandle, $var, length [ , offset ]

Reads length characters into $var at offset. Returns the number of characters actually read, 0 on EOF, and undef on failure.

sysseek filehandle, position, whence

Arbitrarily 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.

syswrite filehandle, scalar [ , length [ , offset ] ]

Writes length characters from scalar at offset. Returns the number of characters actually written, or undef if there was an error.

tell [ filehandle ]

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

Open Modes

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.

perlopentut.

Common constants

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.

Value

Name

Description

00000

0_RDONLY

Read-only access.

00001

0_WRONLY

Write-only access.

00002

0_RDWR

Read and write access.

00100

0_CREAT

Create the file if nonexistent.

00200

0_EXCL

Fail if the file already exists.

02000

0_APPEND

Append data to the end of the file.

01000

0_TRUNC

Truncate the file.

 

0_NONBLOCK

Nonblocking input/output.

 

0_NDELAY

Same as o_NONBLOCK.

 

0_SYNC

Synchronous input/output.

 

0_EXLOCK

Lock exclusive.

 

0_SHLOCK

Lock shared.

 

0_DIRECTORY

File must be a directory.

 

0_NOFOLLOW

Do not follow symlinks.

 

0_BINARY

Use binary mode for input/output.

 

0_LARGEFILE

Allow file to be larger than 4 GB.

 

0_NOCTTY

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

00

SEEK_SET

Seek position.

01

SEEK_CUR

Seek offset from current position.

02

SEEK_END

Seek offset from end of file.

Constants related to flock must be imported explictly by specifying :flock in the import list of Fcntl.

Value

Name

Description

001

LOCK_SH

Shared lock.

002

LOCK_EX

Exclusive lock.

004

LOCK_NB

Non-blocking lock.

010

LOCK_UN

Unlock.

Standard I/O Layers

: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).

PerlIO, perlrun (under ENVIRONMENT/PERLIO).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset