Analyzing HTML with Perl
by Kendrew Lau
|
Finishing Up
The final requirement of the assignment is a pleasant look of the rendered pages. Unfortunately, HTML::TreeBuilder and its related modules do not analyze and quantify the visual appearance of a web page. Neither does any module that I know. OK, I would award marks for the appearance myself but still want Perl to help in the process--the program sets the default score and comment, and allows overriding them in flexible way. By using alternative regular expressions, I can accept the default, override the score only, or override both the score and comment.
my $input = "";
do {
print "$str1 [$str2]: ";
$input = <STDIN>;
$input =~ s/(^\s+|\s+$)//g;
} until ( $input =~ /(.*\.\s+\d+$|^\s*$|^\d+$)/ );
$input = $str2 if $input eq "";
if ( $input =~ /^\d+$/ ) {
$n = $input;
if ( $n == 10 ) {
$input = "good looking, nice content. $n";
}
else {
( $input = $str2 ) =~ s/(\.\s*)\d+\s*$/$1$n/;
}
}
marking "$str1 $input";
Finally, the code examines the marking text string containing comments and scores for each requirement to calculate the total score of the assignment. Each line in that string is in a fixed format (for example, "Italicized text (2 points): good. 0"). Again, regular expressions retrieve and accumulate the maximum and awarded points.
my ( $total, $score ) = ( 0, 0 );
while ( $marktext =~ /.*?\((\d+)\s+points\).*?\.\s+(\d+)/g )
{
$total += $1;
$score += $2;
}
marking "Total ($total points): $score";
Depending on the command-line switches, the program may start a browser to show the first page so that I can look at the pages' appearance. It can also optionally write the grading comments and score to a text file which can be feedback for the student.
I can simply run the program in the directory containing the HTML files, or specify the set of HTML files in the command-line arguments. In the best case, I just let it grade the requirements and press Enter to accept the default marking for the appearance, and then jot down the total score and email the grading text file to the student.
Conclusion
I did not evaluate the time saved by the program against its developing effort. Anyway, the program makes the grading process more accurate and less prone to error, and it is more fun to spend time writing a Perl program and getting familiar with useful modules.
In fact, there are many other modules that could have been used in the program to provide even more automation. Had I read Wasserman's article "Automating Windows Applications with Win32::OLE," the program would record the final score to an Excel file automatically. In addition, networking modules such as Mail::Internet, Mail::Mailer, and Mail::Folder could retrieve the assignment files from emails and send the feedback files to the students directly from the program.
Prev [1] [2] [3]