PHP Code:
#!usr/bin/perl
use strict;
my $file_name="file.txt";
open (R, $file_name);
while(<R>){
print $_;
}
close R;
The above code opens a file in a READ-ONLY mode and then the while loop iterates through the file and prints every line as it is. You can do whatever operation you want to do within the while loop.
Alternatively, you can feed the contents of the file into an array by doing
PHP Code:
my @file_contents = <R>; ## Replace the while(){} loop with this code
and then perform array operations on it.
You can open a file in Append mode and Write mode by doing
PHP Code:
open (R, ">>$file_name"); ## For appending to the file
open (R, ">file_name"); ## For creating a new file, if it doesn't exist, else delete an existing file and recreate it from blank
You can also open a file for reading by doing
PHP Code:
open (R, "<file_name");