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, see page 54.

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, see page 64.

dbmopen %hash, dbmname, mode

Opens a dbm file and associates it with the hash. Superseded by tie, see page 64.

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 pseudofile 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 in page 54.

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 page 52 for general open modes.

In modeandname, - may be used to designate standard input or output. Whitespace is allowed, and 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 (page 54) appended that will be applied to the handle.

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 expr

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

say [ filehandle ] list

Just like print, but implicitly appends a newline.

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 in page 53.

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 Formatted Printing for a complete list of format conversions. See the section Formats 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 in page 53.

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 in page 53.

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

O_RDONLY

Read-only access.

00001

O_WRONLY

Write-only access.

00002

O_RDWR

Read and write access.

00100

O_CREAT

Create the file if nonexistent.

00200

O_EXCL

Fail if the file already exists.

02000

O_APPEND

Append data to the end of the file.

01000

O_TRUNC

Truncate the file.

 

O_NONBLOCK

Nonblocking input/output.

 

O_NDELAY

Same as O_NONBLOCK.

 

O_SYNC

Synchronous input/output.

 

O_EXLOCK

Lock exclusive.

 

O_SHLOCK

Lock shared.

 

O_DIRECTORY

File must be a directory.

 

O_NOFOLLOW

Do not follow symlinks.

 

O_BINARY

Use binary mode for input/output.

 

O_LARGEFILE

Allow file to be larger than 4 GB.

 

O_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

Nonblocking lock.

010

LOCK_UN

Unlock.

Standard I/O Layers

Layer

Description

: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 I/O 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).

PerllO, 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