(1) For example,
sub sum {
my $result = 0;
foreach (@_) {
$result += (ref $_ eq 'ARRAY') ? sum(@{$_})
: $_;
}
$result;
}
(2) For example,
use strict;
# t1aqb.pl
@ARGV < 1 && die "usage t1aqb.pl
inputfile";
my $infile = $ARGV[0];
open DATA, "<$infile" || die "Can't open
file $infile.";
my %majors = ();
while ($_ = <DATA>) {
chomp;
# Name starts at column 24. Extra
. before and
# after (.{20}) removes leading and
trailing " of
# names.
my ($major, $name) = /^(.{4}).{20}(.*).$/;
push @{$majors{$major}}, $name;
}
close DATA;
# print result.
foreach (sort keys %majors) {
print "$_:\n";
foreach (sort @{$majors{$_}}) {
print " $_\n";
}
}
exit 0;
(3) (a) baa
(b) http://dcm.uhcl.edu/yue/q3.pl?user=Bun+Yue&submit=submit
(4) For example,
sub processURL {
my $url = shift;
# Use LWP::UserAgent to get the page.
my $ua = new LWP::UserAgent;
my $response = $ua->request(new HTTP::Request('GET',
$url));
if ($response->is_error()) {
print "<h2>Error in URL</h2>\n",
"Sorry, your $url
is erronous.";
}
else {
# Append to file.
if (open OUT, ">>$filename") {
print OUT "$url\n";
print "<h2>URL Added</h2>\n",
"Your URL
$url has been added. Thanks.\n";
close OUT;
}
else {
print "<h2>URL not added</h2>\n",
"Sorry, there
is a file problem. Your URL has not bee added.";
}
}
} # process URL.
# print the form to solicit URL.
sub showForm {
print <<_URL_FORM;
<form>
Enter your URL to be listed:
<br>
<input type="text" name="$URL_PARAM" size="40"
maxlength="200">
<p>
<input type="submit" value="submit URL!">
</form>
_URL_FORM
} # showForm