open() - Opening File Handles for Input and Output

This section describes various ways to use the open() function to open file handles to input data from files or output data to files, input data from another process or output data to another process.

Input to and output from Perl programs must be done through file handles. File handles are created by calling the open() function in different ways for different situations. Examples are:

1. Open a file handle for input from the file named by an expression:

rc = open(file_handle, expression);

2. Open a file handle for input from the file named by an expression:

rc = open(file_handle, "< ".expression);

3. Open a file handle for output to the file named by an expression:

rc = open(file_handle, "> ".expression);

4. Open a file handle for output to append to the file named by an expression:

rc = open(file_handle, ">> ".expression);

5. Open a file handle for output to and input from the file named by an expression:

rc = open(file_handle, "+> ".expression);

I am not sure how input and output can be managed on the same file. Need to be tested.

6. Open a file handle for input from another process named by an expression:

rc = open(file_handle, expression." |");

In this case, "rc" is the process id forked by this call.

7. Open a file handle for output to another process named by an expression:

rc = open(file_handle, "| ".expression);

In this case, "rc" is the process id forked by this call.

8. Open a file handle for input from the standard input channel, usually the key board:

rc = open(file_handle, "_");

9. Open a file handle for output to the standard output channel, usually the monitor screen:

rc = open(file_handle, ">_");

10. Open a file handle for input from a file named by the scale variable of the same name as the file handle:

rc = open(file_handle);

The above statement is equivalent to rc=open(file_handle,$file_handle).

There are 3 pre-defined file handles:

File handles are closed by the close function:

rc = close(file_handle);

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

File Handles and Data Input/Output

open() - Opening File Handles for Input and Output

 print() - Printing Output to File Handles

 <file_handle> - Reading Data from File Handles

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Converting Perl Script to Executable Binary

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server

 CGI.pm Module for Building Web Pages

 LWP::UserAgent and Web Site Testing

 References

 PDF Printing Version