Great Templates

FEATURED WEB TEMPLATES

Amazon Books

Learn PHP

PHP Training
Zend Cert Training Zend Certified Engineer Zend PHP Pro

Perl: How To Write Or Use A Text File In PERL

To Write a line to a new text file ?

eg :-

open(FILE,">text.dat") || die "Can\\'t Create file" ;
print FILE "Hello ! this is a trial line";
close(FILE);

*Note :
Here FILE is a filehandle(like a nick name !)
and ">" means open for output(writing). "||"
is the OR part ,ie; if PERL is unable to create this
file then it shows this message
(ie which follows die command).
To append lines to a file use ">>".

To read all lines from a text file and print them ?
eg :-
open(FH,"xyz.txt") || die "Can\\'t open xyz.dat file";
@file_lines=< FH>;
print "File lines are : \\n";
print @file_lines;

*Note :
Here @file_lines is just an array variable that receives all lines from the file into each of its elements.

The above lines can still be reduced to :-
open(FH,"xyz.txt") && (@file_lines=< FH>) && close(FH)
|| die "Can\\'t open xyz.dat file";
print @file_lines;

Here "&&" represents AND.


 


Learn PHP | Zend Certified Engineer | Zend PHP Pro | PHP Web Apps | Web Hosting Service | Low Cost Domain Names | Great Templates | Great Books | Testimonials | Tech.Articles | TOS | AUS | Home | Linux Apache MySQL PHP