<?xml version="1.0" encoding="windows-1252"?>
<node id="315" title="perlfunc:open" created="1999-08-24 18:43:12" updated="2005-08-12 22:48:29">
<type id="119">
perlfunc</type>
<author id="114">
gods</author>
<data>
<field name="doctext">
</field>
<field name="name">

&lt;P&gt;
open - open a file, pipe, or descriptor

&lt;P&gt;
&lt;HR&gt;
</field>
<field name="synopsis">

&lt;P&gt;
open 
&lt;FONT SIZE=-1&gt;FILEHANDLE,EXPR&lt;/FONT&gt;

&lt;P&gt;
open 
&lt;FONT SIZE=-1&gt;FILEHANDLE&lt;/FONT&gt;

&lt;P&gt;
&lt;HR&gt;
</field>
<field name="description">

&lt;P&gt;
Opens the file whose filename is given by 
&lt;FONT SIZE=-1&gt;EXPR,&lt;/FONT&gt; and associates it with 
&lt;FONT SIZE=-1&gt;FILEHANDLE.&lt;/FONT&gt; If 
&lt;FONT SIZE=-1&gt;FILEHANDLE&lt;/FONT&gt; is an expression, its value is used as the name of the real filehandle wanted. If 
&lt;FONT SIZE=-1&gt;EXPR&lt;/FONT&gt; is omitted, the scalar variable of the same name as the 
&lt;FONT SIZE=-1&gt;FILEHANDLE&lt;/FONT&gt; contains the filename. (Note that lexical variables--those declared with
 [perlfunc:my|my()]--will not work for this purpose; so if you're using [perlfunc:my|my()], specify 
&lt;FONT SIZE=-1&gt;EXPR&lt;/FONT&gt; in your call to open.)

&lt;P&gt;
If the filename begins with &lt;CODE&gt;'&amp;lt;'&lt;/CODE&gt; or nothing, the file is opened for input. If the filename begins with &lt;CODE&gt;'&amp;gt;'&lt;/CODE&gt;, the file is truncated and opened for output, being created if necessary.
If the filename begins with &lt;CODE&gt;'&amp;gt;&amp;gt;'&lt;/CODE&gt;, the file is opened for appending, again being created if necessary. You
can put a &lt;CODE&gt;'+'&lt;/CODE&gt; in front of the &lt;CODE&gt;'&amp;gt;'&lt;/CODE&gt; or &lt;CODE&gt;'&amp;lt;'&lt;/CODE&gt; to indicate that you want both read and write access to the file; thus &lt;CODE&gt;'+&amp;lt;'&lt;/CODE&gt; is almost always preferred for read/write updates--the &lt;CODE&gt;'+&amp;gt;'&lt;/CODE&gt; 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 &lt;STRONG&gt;-i&lt;/STRONG&gt;
switch in [perlman:perlrun|the perlrun manpage] for a better approach.

&lt;P&gt;
The prefix and the filename may be separated with spaces. These various prefixes correspond to the 
&lt;CODE&gt;fopen(3)&lt;/CODE&gt; modes of
 &lt;CODE&gt;'r'&lt;/CODE&gt;, &lt;CODE&gt;'r+'&lt;/CODE&gt;, &lt;CODE&gt;'w'&lt;/CODE&gt;,
&lt;CODE&gt;'w+'&lt;/CODE&gt;, &lt;CODE&gt;'a'&lt;/CODE&gt;, and &lt;CODE&gt;'a+'&lt;/CODE&gt;.

&lt;P&gt;
If the filename begins with &lt;CODE&gt;'|'&lt;/CODE&gt;, the filename is interpreted as a command to which output is to be piped,
and if the filename ends with a
&lt;CODE&gt;'|'&lt;/CODE&gt;, the filename is interpreted See [%linkNodeTitle "perlman:perlipc|Using open() for IPC", $NODE, "Using open() for IPC",{anchor=&gt;"Using_open_for_IPC"};%]
for more examples of this. (You are not allowed to [perlfunc:open|open()] to a command that pipes both in &lt;EM&gt;and&lt;/EM&gt; out, but see [perlfunc:../../lib/IPC/Open2|Open2], [perlfunc:../../lib/IPC/Open3|Open3], and [%linkNodeTitle "perlman:perlipc|Bidirectional Communication", $NODE, "Bidirectional Communication",{anchor=&gt;"Bidirectional_Communication"};%] for alternatives.)

&lt;P&gt;
Opening &lt;CODE&gt;'-'&lt;/CODE&gt; opens 
&lt;FONT SIZE=-1&gt;STDIN&lt;/FONT&gt; and opening &lt;CODE&gt;'&amp;gt;-'&lt;/CODE&gt; opens 
&lt;FONT SIZE=-1&gt;STDOUT.&lt;/FONT&gt; Open returns nonzero upon success, the
undefined value otherwise. If the [perlfunc:open|open()]
involved a pipe, the return value happens to be the pid of the subprocess.

&lt;P&gt;
If you're unfortunate enough to be running Perl on a system that
distinguishes between text files and binary files (modern operating systems
don't care), then you should check out [perlfunc:binmode|binmode] for tips for dealing with this. The key distinction between systems that
need [perlfunc:binmode|binmode()]
and those that don't is their text file formats. Systems like Unix, MacOS, and Plan9, which delimit lines with a single character, and which encode that character in 
&lt;FONT SIZE=-1&gt;C&lt;/FONT&gt; as
 &lt;CODE&gt;&amp;quot;\n&amp;quot;&lt;/CODE&gt;, do not need [perlfunc:binmode|binmode()]. The rest need it.

&lt;P&gt;
When opening a file, it's usually a bad idea to continue normal execution
if the request failed, so [perlfunc:open|open()] is frequently used in connection with
[perlfunc:die|die()]. Even if [perlfunc:die|die()] won't do what you want (say, in a 
&lt;FONT SIZE=-1&gt;CGI&lt;/FONT&gt; script, where you want to make a nicely formatted
error message (but there are modules that can help with that problem)) you
should always check the return value from opening a file. The infrequent
exception is when working with an unopened filehandle is actually what you
want to do.

&lt;P&gt;
Examples:

&lt;P&gt;
&lt;PRE&gt;    $ARTICLE = 100;
    open ARTICLE or die &amp;quot;Can't find article $ARTICLE: $!\n&amp;quot;;
    while (&amp;lt;ARTICLE&amp;gt;) {...
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    open(LOG, '&amp;gt;&amp;gt;/usr/spool/news/twitlog'); # (log is reserved)
    # if the open fails, output is discarded
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    open(DBASE, '+&amp;lt;dbase.mine')             # open for update
        or die &amp;quot;Can't open 'dbase.mine' for update: $!&amp;quot;;
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    open(ARTICLE, &amp;quot;caesar &amp;lt;$article |&amp;quot;)     # decrypt article
        or die &amp;quot;Can't start caesar: $!&amp;quot;;
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    open(EXTRACT, &amp;quot;|sort &amp;gt;/tmp/Tmp$$&amp;quot;)      # $$ is our process id
        or die &amp;quot;Can't start sort: $!&amp;quot;;
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    # process argument list of files along with any includes
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    foreach $file (@ARGV) {
        process($file, 'fh00');
    }
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    sub process {
        my($filename, $input) = @_;
        $input++;               # this is a string increment
        unless (open($input, $filename)) {
            print STDERR &amp;quot;Can't open $filename: $!\n&amp;quot;;
            return;
        }
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;        local $_;
        while (&amp;lt;$input&amp;gt;) {              # note use of indirection
            if (/^#include &amp;quot;(.*)&amp;quot;/) {
                process($1, $input);
                next;
            }
            #...                # whatever
        }
    }
&lt;/PRE&gt;
&lt;P&gt;
You may also, in the Bourne shell tradition, specify an 
&lt;FONT SIZE=-1&gt;EXPR&lt;/FONT&gt; beginning with &lt;CODE&gt;'&amp;gt;&amp;amp;'&lt;/CODE&gt;, in which case the rest of the string is interpreted as the name of a
filehandle (or file descriptor, if numeric) to be duped and opened. You may
use &lt;CODE&gt;&amp;amp;&lt;/CODE&gt; after &lt;CODE&gt;&amp;gt;&lt;/CODE&gt;, &lt;CODE&gt;&amp;gt;&amp;gt;&lt;/CODE&gt;, &lt;CODE&gt;&amp;lt;&lt;/CODE&gt;, &lt;CODE&gt;+&amp;gt;&lt;/CODE&gt;,
&lt;CODE&gt;+&amp;gt;&amp;gt;&lt;/CODE&gt;, and &lt;CODE&gt;+&amp;lt;&lt;/CODE&gt;. The mode you specify should match the mode of the original filehandle. (Duping a filehandle does not take into account any existing contents of stdio buffers.) Here is a script that saves, redirects, and restores 
&lt;FONT SIZE=-1&gt;STDOUT&lt;/FONT&gt; and 
&lt;FONT SIZE=-1&gt;STDERR:&lt;/FONT&gt;

&lt;P&gt;
&lt;PRE&gt;    #!/usr/bin/perl
    open(OLDOUT, &amp;quot;&amp;gt;&amp;amp;STDOUT&amp;quot;);
    open(OLDERR, &amp;quot;&amp;gt;&amp;amp;STDERR&amp;quot;);
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    open(STDOUT, &amp;quot;&amp;gt;foo.out&amp;quot;) || die &amp;quot;Can't redirect stdout&amp;quot;;
    open(STDERR, &amp;quot;&amp;gt;&amp;amp;STDOUT&amp;quot;) || die &amp;quot;Can't dup stdout&amp;quot;;
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    select(STDERR); $| = 1;     # make unbuffered
    select(STDOUT); $| = 1;     # make unbuffered
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    print STDOUT &amp;quot;stdout 1\n&amp;quot;;  # this works for
    print STDERR &amp;quot;stderr 1\n&amp;quot;;  # subprocesses too
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    close(STDOUT);
    close(STDERR);
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    open(STDOUT, &amp;quot;&amp;gt;&amp;amp;OLDOUT&amp;quot;);
    open(STDERR, &amp;quot;&amp;gt;&amp;amp;OLDERR&amp;quot;);
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    print STDOUT &amp;quot;stdout 2\n&amp;quot;;
    print STDERR &amp;quot;stderr 2\n&amp;quot;;
&lt;/PRE&gt;
&lt;P&gt;
If you specify &lt;CODE&gt;'&amp;lt;&amp;amp;=N'&lt;/CODE&gt;, where &lt;CODE&gt;N&lt;/CODE&gt; is a number, then Perl will do an equivalent of C's &lt;CODE&gt;fdopen()&lt;/CODE&gt; of that file descriptor; this is more parsimonious of file descriptors. For
example:

&lt;P&gt;
&lt;PRE&gt;    open(FILEHANDLE, &amp;quot;&amp;lt;&amp;amp;=$fd&amp;quot;)
&lt;/PRE&gt;
&lt;P&gt;
If you open a pipe on the command &lt;CODE&gt;'-'&lt;/CODE&gt;, i.e., either &lt;CODE&gt;'|-'&lt;/CODE&gt; or &lt;CODE&gt;'-|'&lt;/CODE&gt;, then there is an implicit fork done, and the return value of open is the
pid of the child within the parent process, and &lt;CODE&gt;0&lt;/CODE&gt; within the child process. (Use [perlfunc:defined|defined($pid)] to determine whether the open was successful.) The filehandle behaves normally for the parent, but i/o to that filehandle is piped from/to the 
&lt;FONT SIZE=-1&gt;STDOUT/STDIN&lt;/FONT&gt; of the child process. In the child process the filehandle isn't opened--i/o happens from/to the new 
&lt;FONT SIZE=-1&gt;STDOUT&lt;/FONT&gt; or 
&lt;FONT SIZE=-1&gt;STDIN.&lt;/FONT&gt; Typically this is used like the normal piped open when you want to exercise more control over just how the pipe command gets executed, such as when you are running setuid, and don't want to have to scan shell commands for metacharacters. The following pairs are more or less equivalent:

&lt;P&gt;
&lt;PRE&gt;    open(FOO, &amp;quot;|tr '&amp;#091;a-z&amp;#093;' '&amp;#091;A-Z&amp;#093;'&amp;quot;);
    open(FOO, &amp;quot;|-&amp;quot;) || exec 'tr', '&amp;#091;a-z&amp;#093;', '&amp;#091;A-Z&amp;#093;';
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    open(FOO, &amp;quot;cat -n '$file'|&amp;quot;);
    open(FOO, &amp;quot;-|&amp;quot;) || exec 'cat', '-n', $file;
&lt;/PRE&gt;
&lt;P&gt;
See [%linkNodeTitle "perlman:perlipc|Safe Pipe Opens", $NODE, "Safe Pipe Opens",{anchor=&gt;"Safe_Pipe_Opens"};%] for more examples of this.

&lt;P&gt;

&lt;FONT SIZE=-1&gt;NOTE:&lt;/FONT&gt; On any operation that may do a fork, any
unflushed buffers remain unflushed in both processes, which means you may
need to set &lt;CODE&gt;$|&lt;/CODE&gt; to avoid duplicate output.

&lt;P&gt;
Closing any piped filehandle causes the parent process to wait for the
child to finish, and returns the status value in &lt;CODE&gt;$?&lt;/CODE&gt;.

&lt;P&gt;
The filename passed to open will have leading and trailing whitespace deleted, and the normal redirection characters honored. This property, known as ``magic open'', can often be used to good effect. 
&lt;FONT SIZE=-1&gt;A&lt;/FONT&gt; user could specify a filename of

&lt;EM&gt;"rsh cat file |"&lt;/EM&gt;, or you could change certain filenames as needed:

&lt;P&gt;
&lt;PRE&gt;    $filename =~ s/(.*\.gz)\s*$/gzip -dc &amp;lt; $1|/;
    open(FH, $filename) or die &amp;quot;Can't open $filename: $!&amp;quot;;
&lt;/PRE&gt;
&lt;P&gt;
However, to open a file with arbitrary weird characters in it, it's
necessary to protect any leading and trailing whitespace:

&lt;P&gt;
&lt;PRE&gt;    $file =~ s#^(\s)#./$1#;
    open(FOO, &amp;quot;&amp;lt; $file\0&amp;quot;);
&lt;/PRE&gt;
&lt;P&gt;
If you want a ``real'' 
&lt;FONT SIZE=-1&gt;C&lt;/FONT&gt; [perlfunc:open|open()] (see &lt;EM&gt;open(2)&lt;/EM&gt; on your system), then you should use the &lt;CODE&gt;sysopen()&lt;/CODE&gt; function, which involves no such magic. This is another way to protect your
filenames from interpretation. For example:

&lt;P&gt;
&lt;PRE&gt;    use IO::Handle;
    sysopen(HANDLE, $path, O_RDWR|O_CREAT|O_EXCL)
        or die &amp;quot;sysopen $path: $!&amp;quot;;
    $oldfh = select(HANDLE); $| = 1; select($oldfh);
    print HANDLE &amp;quot;stuff $$\n&amp;quot;);
    seek(HANDLE, 0, 0);
    print &amp;quot;File contains: &amp;quot;, &amp;lt;HANDLE&amp;gt;;
&lt;/PRE&gt;
&lt;P&gt;
Using the constructor from the &lt;CODE&gt;IO::Handle&lt;/CODE&gt; package (or one of its subclasses, such as &lt;CODE&gt;IO::File&lt;/CODE&gt; or &lt;CODE&gt;IO::Socket&lt;/CODE&gt;), you can generate anonymous filehandles that have the scope of whatever
variables hold references to them, and automatically close whenever and
however you leave that scope:

&lt;P&gt;
&lt;PRE&gt;    use IO::File;
    #...
    sub read_myfile_munged {
        my $ALL = shift;
        my $handle = new IO::File;
        open($handle, &amp;quot;myfile&amp;quot;) or die &amp;quot;myfile: $!&amp;quot;;
        $first = &amp;lt;$handle&amp;gt;
            or return ();     # Automatically closed here.
        mung $first or die &amp;quot;mung failed&amp;quot;;       # Or here.
        return $first, &amp;lt;$handle&amp;gt; if $ALL;       # Or here.
        $first;                                 # Or here.
    }
&lt;/PRE&gt;
&lt;P&gt;
See [perlfunc:seek|seek()] for some details about mixing reading and writing.

&lt;HR&gt;
</field>
</data>
</node>
