(1) For example,
#
# t1aq1.pl
#
# Solution to Question 1 of CSCI 4230.1 Mid-term
examination
# Summer 2001
#
# By Kwok-Bun Yue
# June 14, 2001
#
use strict;
use LWP::Simple;
# Get the URL content.
@ARGV >= 1 || die "Usage: t1aq1.pl URL\n";
my $url = $ARGV[0];
my $urlContents = get($url);
my %images = ();
while ($urlContents =~ /<img.*?src=['"]?([^'"
]+)['"]?/gsi) {
$images{$1}++;
}
if (keys %images > 0) {
print "Images found in
$url:\n\n";
foreach (sort keys %images)
{
print " $_: $images{$_}.\n";
}
}
else {
print "Invalid URL or
no images found.\n";
}
exit 0;
Alternatively, you may use HTML::LinkExtor or HTML::TokeParser.
(2) (a) 107135
(b) A reference of an array, such as: ARRAY(0x3a4cac)
(c) For example:
%h = (a => 1, b => 2, c => 3, d => 1, e => 2, f => 1);
my %result = ();
foreach (values %h) {
$result{$_}++;
}
foreach (keys %result) {
print "$_\n";
}
(3) For example:
use CGI;
use strict;
my $q = new CGI;
my $PARAM_PREFIX = 'user';
print $q->header,
$q->start_html('Parameter listing'),
$q->h2('Parameter listig'),
$q->p;
my %parameters = ();
my $key;
foreach $key ($q->param()) {
if ($key=~ /^${PARAM_PREFIX}\d+$/)
{
$parameters{$key} = $q->param($key);
}
}
if (%parameters) {
print "The HTTP parameters
with '${PARAM_PREFIX}' plus a number:\n",
$q->p;
foreach (sort keys %parameters)
{
print "$_ => $parameters{$_}\n",
$q->br;
}
}
else {
print "There are no HTTP
parameters that is '${PARAM_PREFIX}' plus a number.";
}
exit 0;
(4) For example,
use strict;
use CGI::Pretty;
my $CONTENT = 'content';
my $q = new CGI;
my $body = $q->param($CONTENT);
if ($body) {
print $q->header,
$q->start_html('Space
counter'),
$q->h2('Space counter'),
'Number of space in the
submitted text: ';
print scalar grep $_ eq
' ', split //, $body;
print $q->end_html;
}
else {
print $q->header,
$q->start_html('Space
counter'),
$q->h2('Space counter'),
$q->start_form,
'This page will show the
number of spaces in the text you submitted below: ',
$q->p,
$q->textarea(-name=>$CONTENT,
-rows=>4,
-columns=>50),
$q->p,
$q->submit,
$q->end_form,
$q->end_html;
}
exit 0;