Opens the file whose filename is given by EXPR, and associates it with FILEHANDLE.
Simple examples to open a file for reading:
open(my $fh, '<', "input.txt") or die $!;
and for writing:
open(my $fh, '>', "output.txt") or die $!;
(The following is a comprehensive reference to open(): for a gentler introduction you may consider perlopentut.)
If FILEHANDLE is an undefined scalar variable (or array or hash element) the variable is assigned a reference to a new anonymous filehandle, otherwise if FILEHANDLE is an expression, its value is used as the name of the real filehandle wanted. (This is considered a symbolic reference, so use strict 'refs'
should not be in effect.)
If EXPR is omitted, the scalar variable of the same name as the FILEHANDLE contains the filename. (Note that lexical variables--those declared with my
--will not work for this purpose; so if you're using my
, specify EXPR in your call to open.)
If three or more arguments are specified then the mode of opening and the filename are separate. If MODE is '<'
or nothing, the file is opened for input. If MODE is '>'
, the file is truncated and opened for output, being created if necessary. If MODE is '>>'
, the file is opened for appending, again being created if necessary.
You can put a '+'
in front of the '>'
or '<'
to indicate that you want both read and write access to the file; thus '+<'
is almost always preferred for read/write updates--the '+>'
mode would clobber the file first. You can't usually use either read-write mode for updating textfiles, since they have variable length records. See the -i switch in perlrun for a better approach. The file is created with permissions of 0666
modified by the process's umask
value.
These various prefixes correspond to the fopen(3) modes of 'r'
, 'r+'
, 'w'
, 'w+'
, 'a'
, and 'a+'
.
In the two-argument (and one-argument) form of the call, the mode and filename should be concatenated (in that order), possibly separated by spaces. You may omit the mode in these forms when that mode is '<'
.
If the filename begins with '|'
, the filename is interpreted as a command to which output is to be piped, and if the filename ends with a '|'
, the filename is interpreted as a command that pipes output to us. See "Using open() for IPC" in perlipc for more examples of this. (You are not allowed to open
to a command that pipes both in and out, but see IPC::Open2, IPC::Open3, and "Bidirectional Communication with Another Process" in perlipc for alternatives.)
For three or more arguments if MODE is '|-'
, the filename is interpreted as a command to which output is to be piped, and if MODE is '-|'
, the filename is interpreted as a command that pipes output to us. In the two-argument (and one-argument) form, one should replace dash ('-'
) with the command. See "Using open() for IPC" in perlipc for more examples of this. (You are not allowed to open
to a command that pipes both in and out, but see IPC::Open2, IPC::Open3, and "Bidirectional Communication" in perlipc for alternatives.)
In the form of pipe opens taking three or more arguments, if LIST is specified (extra arguments after the command name) then LIST becomes arguments to the command invoked if the platform supports it. The meaning of open
with more than three arguments for non-pipe modes is not yet defined, but experimental "layers" may give extra LIST arguments meaning.
In the two-argument (and one-argument) form, opening '<-'
or '-'
opens STDIN and opening '>-'
opens STDOUT.
You may use the three-argument form of open to specify I/O layers (sometimes referred to as "disciplines") to apply to the handle that affect how the input and output are processed (see open and PerlIO for more details). For example:
open(my $fh, "<:encoding(UTF-8)", "filename")
|| die "can't open UTF-8 encoded filename: $!";
opens the UTF-8 encoded file containing Unicode characters; see perluniintro. Note that if layers are specified in the three-argument form, then default layers stored in ${^OPEN} (see perlvar; usually set by the open pragma or the switch -CioD) are ignored.
Open returns nonzero on success, the undefined value otherwise. If the open
involved a pipe, the return value happens to be the pid of the subprocess.
If you're running Perl on a system that distinguishes between text files and binary files, then you should check out