Reading Files in Perl

   



Reading File Contents into Perl Arrays

Listing C.10 displays the contents of a Perl script that reads the contents of a file into a Perl array in a single line.

Listing C.10 readFile2.pl

start example
my($count)       = 0; my($currentLine) = ""; my($fileName)    = "somedata.txt"; open(INPUT, "<$fileName") || die "Cannot open $fileName: $!\n";  (@fileContents) = <INPUT>; $count = scalar(@fileContents); print "File $fileName contains $count lines\n";
end example

Remarks

You can launch the Perl script readFile2.pl in Listing C.10 from the command line as follows,

perl -w readFile2.pl

and the output is as follows:

File somedata.txt contains 5 lines



   



Fundamentals of SVG Programming. Concepts to Source Code
Fundamentals of SVG Programming: Concepts to Source Code (Graphics Series)
ISBN: 1584502983
EAN: 2147483647
Year: 2003
Pages: 362

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net