File input/output

Opening a filehandle

In order to use a filehandle other than STDIN, STDOUT and STDERR, the filehandle needs to be opened.

The open function opens a file or device and associates it with a filehandle.

It returns 1 upon success and undef otherwise.

Examples

# open a filehandle for reading:
 
open (IN_FILE, "filename");   # or
open (IN_FILE, "<filename");


# open a filehandle for writing:

open (OUT_FILE, ">filename");


# open a filehandle for appending:

open (LOGFILE, ">>filename";


# SOURCE_FILE, IN_FILE, RESULT_FILE, OUT_FILE, LOGFILE and IN_OUT_FILE are examples
  of filehandle names.

Note:

You may write a variable name instead of "filename". Examples:
my $in_file = "filename1";
open (SOURCE_FILE, "$in_file");

my $out_file = "filename2";
open (RESULT_FILE, ">$out_file");

Table of Contents.
Previous | Next.