Prev

Perl Notes

Next

File Operation Modes

The following example overwrites a file:

open(FILE, ">../myfile.txt") || die "Could not open file $!"; print FILE "I'm writing text to a file.\n"; close(FILE);

This example reads a file line by line:

open(FILE, "<../myfile.txt") || die "Could not open file $!"; while ($line=<FILE>) { print $line; } close(FILE);

As you can see, the only difference in the open statement is the operand used (> and the <) in front of the path to the file (../myfile.txt). This tells perl what mode to open the file in. Here is a small summary of other modes for the open command.:

OperandDescription
< Read access only (The same as not specifying a mode.)
> Write access, create if nonexistant, and overwrite existing data.
>> Write access, create if nonexistant, and append to existing data.
+< Read and Write only, no file creation/appending. Overwrite existing data.
+> Read, Write, Create, and Truncate
+>> Read, Write, Create, and Append
| SYSCOMMANDWrite data to external command only.
SYSCOMAND | Read data from external command only.

Prev Home Next