|
|
Listing C.8 displays the contents of the file somedata.txt that we will use for the purposes of illustrating how to read the contents of a file.
Listing C.8 somedata.txt
# this is a comment line Jones:Tom:1000 Smith:Ann:2000 Smith:Bill:3000
Listing C.9 displays the contents of readFile1.pl, which is a Perl script that reads the contents of the file somedata.txt.
Listing C.9 readFile1.pl
my($count) = 0; my($currentLine) = ""; my($fileName) = "somedata.txt"; open(INPUT, "<$fileName") || die "Cannot open $fileName: $!\n"; while($currentLine = <INPUT>) { $count++; } print "File $fileName contains $count lines\n";
You can launch the Perl script readFile1.pl in Listing C.9 from the command line as follows:
perl -w readFile1.pl
If the file was opened successfully, you will see the following output:
File somedata.txt contains 5 lines
|
|