Due: September 21, 1999
(1) Write a simple perl program to count the number of characters, words and lines in a user-specified file name, without opening the file directly within your Perl program. That is, you cannot use the open statement. A session of running the program looks like below. User input is in green. Hint: use the Unix system command wc and develop it in Apollo.
[yue@apollo whatever]$ ./wordcount.pl
file to count words => h3q1dat.txt
For file h3q1dat.txt:
number of characters => 111.
number of words => 19.
number of lines => 5.
[yue@apollo whatever]$
The contents of h3q1dat.txt is:
This is a sentence. This is another
sentence.
Another line.
Yet another line.
The last sentence in two
lines.
(2) Write a Perl function itemCount to count the number of scalar items (including ()) of an argument. For example, for the following Perl code:
my $var1 = ['This', 'is', 'an', 'example',
'of',
['an', 'array', 'of', {hash => 'yes', array => 'no'}, 0, 1],
{why => 'me', so => ()}, [{}]];
print "total count of var1 => ", itemCount($var1)
, "\n";
my $var2 = 'hi';
print "total count of var2 => ", itemCount($var2)
, "\n";
my $var3 = ();
print "total count of var3 => ", itemCount($var3)
, "\n";
the following is the suitable output:
total count of var1 => 14
total count of var2 => 1
total count of var3 => 1