Perl in a Nutshell

Perl in a NutshellSearch this book
Previous: Reference: octChapter 5
Function Reference
Next: Reference: opendir
 

open

open filehandle, filename

Opens the file given by filename, and associates it with filehandle. If filehandle is omitted, the scalar variable of the same name as the filehandle must contain the filename. (And you must also be careful to use "or die" after the statement rather than "|| die", because the precedence of || is higher than list operators like open.)

If filename is preceded by either < or nothing, the file is opened for input (read-only). If filename is preceded by >, the file is opened for output. If the file doesn't exist, it will be created; if the file exists, it will be overwritten with output using >. Preceding the filename with >> opens an output file for appending. For both read and write access, use a + before either < or >.

A filehandle may also be attached to a process by using a piped command. If the filename begins with |, the filename is interpreted as a command to which output is to be piped. If the filename ends with a |, the filename is interpreted as a command which pipes input to you. You may not have an open command that pipes both in and out.

Any pipe command containing shell metacharacters is passed to the shell for execution; otherwise, it is executed directly by Perl. The filename "-" refers to STDIN, and ">" refers to STDOUT. open returns non-zero upon success, the undefined value otherwise. If the open involved a pipe, the return value happens to be the process ID of the subprocess.


Previous: Reference: octPerl in a NutshellNext: Reference: opendir
Reference: octBook IndexReference: opendir

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.