CSCI 4230
Software Tools
Fall 1999
Suugested Solution to Homework #5

(1)    For example,

#!/usr/bin/perl
use strict;
use Win32::ODBC;

#    createSmiley.pl
#    by Kwok-Bun Yue
#    October 4, 1999
#
#    This program creates a table of smiley character using ODBC DSN
#    odbctest

open IN, "smiley.txt" || die "Can't open file.";
my @lines = <IN>;
chomp @lines;

#   Connect to the database.
my $db = new Win32::ODBC("odbctest");
die qq(cannot open the ODBC DSN odbctest\n) if (! $db);
my $sql;
my $result;

foreach (@lines) {
   my ($symbol, $desc);
   #   Extract smiley symbol and description.
   s/'/''/g;
   next unless /\s+[a-zA-Z]/;
   $symbol = $`;
   $desc = $& . $';
   $desc =~ s/^\s+//;
   #   Insert symbols as records
   $sql = "Insert into Smiley(symbol, description) values('$symbol', '$desc')";
   $result = $db->Sql($sql);
   die qq(SQL failed "$sql":), $db->Error(), qq(\n) if $result;
   print "$symbol ==> $desc\n";
}

exit 0;
 
#!/opt/gnu/bin/perl

use CGI qw/:standard :html3/;
use Win32::ODBC;
use strict;

#
#    Program:   findsmiley.pl
#    Author:    Kwok-Bun Yue
#    Date:      October 4, 1999
#
#    Purpose:   This program allows the user to find smiley for
#               user-specified keyword.
#
#               The program uses the ODBC DSN "odbctest" which
#               contains the following table:
#                   Smiley(symbol, description, Sid)
#
#               It displays a form allowing user to input a keyword to search
#               The output are all smileys with description containing the keywords.
#

#   Open connection to the ODBC database.
my $db = new Win32::ODBC("odbctest");
die qq(cannot open the ODBC DSN odbctest\n) if (! $db);

#   Get parameters.
my $keyword = param("keyword");   #  user input keyword.
my $all_smileys = param("allsmileys");

#   Print the html header.
print header;

#   Process parameters.
if ($keyword || $all_smileys) {
   &print_result($keyword);
}
else {
   &print_initial_page;
}

print hr;
&print_search_form;
print end_html;
exit 0;
#   End of main.
 

#
#   print_initial_page
#
sub print_initial_page($word, $play) {
   print start_html(-title=>"Search for smiley.",
                    -author=>'yue@uhcl.edu',
                    -BGCOLOR=>"#ccccff"),
         h2("Looking for the right Smiley?"),
         p,
         "This page allows you to find the right smiley for your uses.";
}
 

#
#   print_result: print the result of searching.
#
sub print_result {
   my $keyword = shift;
   my $sql;
   my $result;
   my $condition = "";
   my %symbols = ();

   #   Set condition.
   if (!$all_smileys) {
      $keyword =~ s/'/''/g;
      $condition = " where description like '\%$keyword\%'";
   }
   #   Execute sql statement and get result.
   $sql = "select symbol, description from Smiley $condition";
   $result = $db->Sql($sql);

   while ($db->FetchRow()) {
       if ($symbols{$db->Data("symbol")}) {
           push (@{$symbols{$db->Data("symbol")}}, $db->Data("description"));
       }
       else {
           $symbols{$db->Data("symbol")} = [$db->Data("description")];
      }
   }

   #   Print out content.
   print start_html(-title=>"Smiley listing",
                    -author=>'yue@uhcl.edu',
                    -BGCOLOR=>"#ccccff"),
         h2("Smiley for your keyword"),
         p;

   my $num_result = scalar keys %symbols;
   if ($num_result == 0) {
      print "Sorry, there is no smiley with description matching your keyword: ",
            span({-style=>'Color: green;'}, $keyword),
            ".";
   }
   else {
      if ($all_smileys) {
          print "There are a total of $num_result smileys in the database.", p;
      }
      else {
          print "There are $num_result smileys that match your keyword ",
                span({-style=>'Color: green;'}, $keyword),
                ".", p;
      }
      my @rows = th(["Smiley", "Description"]);
      foreach (sort keys %symbols) {
          my $sym = $_;
          my $desc = "1. " . shift @{$symbols{$_}};
          my $count = 2;
          foreach (@{$symbols{$_}}) {
              $desc .= br . $count++ . "." . $_;
          }
          $desc =~ s/($keyword)/<SPAN STYLE="Color:red;">$1<\/SPAN>/gi;
          push (@rows, td([$sym, $desc]));
       }
       print table({-border=>1, -width=>'75%'},
                   Tr(\@rows));
   }
}

#
#   print_search_form: Print the search form.
#
sub print_search_form {
     print br,
           start_form,
           "Please enter a keyword for the description of the smiley ",
           "you want to find.",
           br,
           "keyword: ",
           textfield(-name=>"keyword",
                     -size=>20,
                     -maxlength=>80),
           space(7),
           submit(-value=>"Find Smiley!"),
           p,
           "Alternatively, you can find all smileys: ",
           space(5),
           submit(-name=>"allsmileys", -value=>"All Smiley"),
           endform;
}
 
#
#   print HTML spaces
#
sub space {
   my $result = "";
   for (my $i=0; $i < shift; $i++) {
      $result .= "&nbsp;";
   }
   $result;
}
 

(2)    For example,

A {text-transform:lowercase;
   text-decoration:none;
   font-family:Arial}
A:link { color: blue }       /* unvisited link */
A:visited { color: green }   /* visited links */
A:active { color: red }    /* active links */

UL {list-style-image: url(bullet.gif);}

H1 {text-transform:uppercase;
    color:blue;
    background-color:yellow}

H1 EM {color: red}