Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/forums/perl/)
-   -   hello!! (http://www.go4expert.com/forums/hello-t7617/)

adroit89 29Nov2007 10:10

how to open a text file using perl????

naveen 29Nov2007 17:32

Re: hello!!
PHP Code:


use strict;

my $file_name="file.txt";

open (R$file_name);

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"); 

All times are GMT +5.5. The time now is 12:22.