PERL Opening Files

Domain Hosting image
Web Hosting
Dedicated server
ssl certificate
Web Design image
Email

The open() function is used to open a file and create a connection to it called a file handle. The basic open() function call looks like this:

open (FI LE HAN D LE);

The FILE HANDLE parameter in this version of open() is the name for the new file handle. It is also the name of the scalar variable that holds the file name that you would like to open for input. For example:

To Open a File for Input

$INPUT_FILE="fixed.dat";
open(INPUT_FILE);
@array=;
close(INPUT_FILE);
foreach(@array){
print();
}

This program displays all the lines in the fixed.dat

It is considered good programming practice to close any connections that are made with the open() function as soon as possible. While not 5trictly needed, it does ensure that all temporary buffers and caches are written to the hard disk in case of a power failure or other catastrophic failure. The open() function has many variations to let you access files in different ways.

The Different Ways to Open a File

open(FILE_HANDLE); - Opens the file named in $FILE_HANDLE and connect to it using FILE HANDLE as the file handle. The file will be opened for input only.

open(FILE_HANDLE, FILENAME.EXT); - Opens the file called FILENAME.EXTfor input using FILE HA.NDLE as the file handle.

open(FILE_HANDLE, + open(FILE_HANDLE, >FILENAME.EXT); - Opens FILENAME.EXT for output using FILE_HANDLE as the file handle.

open(FILE_HANDLE, ); - Opens standard input.

open (F I LE_HANDLE, >>FILENAME.EXT); - Opens FILENAME.EXT for appending using FILE HANDLE as the file handle.

open(FILE_HANDLE, + open(FILE_HANDLE, +>FILENAME.EXT),; - Opens FILENAME.EXT f or both input and output using FILE_HANDLE as the file handle.

open (FI LE HAND LE, +>>FILENAME.EXT); - Opens FILENAME.EXT for both input and output using FILE HANDLE as the file handle.

open(FILE_HANDLE, IPROGRAM) - Sends the output printed to FILE HANDLE to another program.

open(FILE_HANDLE, PROGRAM I) - Reads the output from another program using FILE HANDLE.



Domain Name Search

www.


Copyright (C) 2007. Web Domain design hosting. All rights reserved.