open FILEHANDLE
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 See ``Using open() for IPC'' for more examples of this. as command which pipes input to us. (You may not have a raw open() to a command that pipes both in and out, but see See open2, open3, and ``Bidirectional Communication'' for alternatives.)
Opening '-' opens STDIN and opening '>-' opens STDOUT. Open returns non-zero upon 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 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 binmode for tips for dealing with this. The key distinction between systems that need binmode and those that don't is their text file formats. Systems like Unix and Plan9 that delimit lines with a single character, and that encode that character in C as '\n', do not need binmode . The rest need it.
Examples:
open(LOG, '>>/usr/spool/news/twitlog'); # (log is reserved) open(DBASE, '+/tmp/Tmp$$"); # $$ is our process id # process argument list of files along with any includes foreach $file (@ARGV) { process($file, 'fh00'); } sub process { local($filename, $input) = @_; $input++; # this is a string increment unless (open($input, $filename)) { print STDERR "Can't open $filename: $!\n"; return; } while (<$input>) { # note use of indirection if (/^#include "(.*)"/) { process($1, $input); next; } ... # whatever } }
You may also, in the Bourne shell tradition, specify an EXPR beginning with ``>&'', in which case the rest of the string is interpreted as the name of a filehandle (or file descriptor, if numeric) which is to be duped and opened. You may use & after >, >>, <, +>, +>> and +<. The mode you specify should match the mode of the original filehandle. (Duping a filehandle does not take into acount any existing contents of stdio buffers.) Here is a script that saves, redirects, and restores STDOUT and STDERR:
If you specify ``<&=N'', where N is a number, then Perl will do an equivalent of C's fdopen() of that file descriptor; this is more parsimonious of file descriptors. For example:
If you open a pipe on the command ``-'', i.e. either ``|-'' or ``-|'', then there is an implicit fork done, and the return value of open is the pid of the child within the parent process, and 0 within the child process. (Use 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 STDOUT/STDIN of the child process. In the child process the filehandle isn't opened--i/o happens from/to the new STDOUT or STDIN. 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:
See ``Safe Pipe Opens'' for more examples of this.
Explicitly closing any piped filehandle causes the parent process to wait for the child to finish, and returns the status value in $?. Note: on any operation which may do a fork, unflushed buffers remain unflushed in both processes, which means you may need to set $| to avoid duplicate output.
The filename that is passed to open will have leading and trailing whitespace deleted. In order to open a file with arbitrary weird characters in it, it's necessary to protect any leading and trailing whitespace thusly:
If you want a ``real'' C
open()
(see L
See
seek
for some details about mixing reading and writing.