#!/usr/local/bin/perl use CGI; $query = new CGI; print $query->header; &do_prompt($query); &do_work($query); &print_tail; sub do_prompt { my($query) = @_; # define the types of calculations we'll offer my(@types) = ('count lines','count words','count characters'); print <Word Counts Select the browse button to choose a text file to upload. When you press the submit button, this script will count the number of lines, words, and characters in the file. END ; # Start a multipart form. print $query->start_multipart_form, "Enter the file to process:", $query->filefield(-name=>'filename', -size=>30),"
", $query->checkbox_group(-name=>'count', -values=>\@types, -defaults=>\@types),"

", $query->reset,$query->submit(-label=>'Process File'), $query->end_form; } sub do_work { my($query) = @_; # Process the form if there is a file name entered if ($file = $query->param('filename')) { print "


\n"; print "

$file

\n"; while (<$file>) { $lines++; $words += @words=split(/\s+/); $characters += length($_); } grep($stats{$_}++,$query->param('count')); if (%stats) { print "Lines: $lines
\n" if $stats{'count lines'}; print "Words: $words
\n" if $stats{'count words'}; print "Characters: $characters
\n" if $stats{'count characters'}; } else { print "No statistics selected.\n"; } } } sub print_tail { print < Last modified 6 May 1996. END ; print $query->end_html; }