/g; print "
$heading
"; } } print <##
#template1.tmpl
Test Template
My Home Directory is
My Path is set to
##
##
#!/bin/perl.exe -w
use lib '/site/lib';
use HTML::Template;
# open the html template
my $template = HTML::Template->new(filename => 'template1.tmpl');
# fill in some parameters
$template->param(HOME => $ENV{HOME});
$template->param(PATH => $ENV{PATH});
# send the obligatory Content-Type and print the template output
print "Content-Type: text/html\n\n", $template->output;
####
#!bin/perl.exe -w
#This CGI script extracts weather information from the Australian Bureau of
#retrieve url, and split into segments on double new-lines, which seems to be the divider
#between each Station section
use warnings;
#use strict;
use LWP::Simple;
$file = get "http://www.bom.gov.au/cgi-bin/wrap_fwo.pl?IDN10177.txt";
@segments = split(/\n{2}/, $file);
#Html file necessities
print "Content-type: text/html\n\n";
print <
Weather to the minute!
SLP802
EOM
foreach (@segments) {
if ($_ =~ /IDN\w/) {
$heading = "$&$'";
$heading =~ s/(\n)/
/g;
print "$heading
";
}
}
print <
Region Name Av Min Temp(°C) Av Max Temp(°C) Av Rainfall(mm)
EOM
foreach $segments(@segments) {
if ($segments =~ /Station/) {
$region = ®ionname($segments);
$min = &mintemp($segments);
$max = &maxtemp($segments);
$rain = &rainfall($segments);
print "$region $min $max $rain ";
}
}
print "\n";
sub regionname {
local $seg = $_[0];
local $region;
local @words = split(/\n/,$seg);
$region = $words[0];
return $region;
}
sub mintemp {
local $seg = $_[0];
local ($min, $size, $line, $numregion);
local $num = 0;
local $sum = 0;
local @words = split(/\n/,$seg);
foreach (@words) {
if ($_ =~ /^([A-Z]{1}[a-z]{1})/) {
$line = $_;
chomp($line);
$size = length($line);
$numregion = substr($line, ($size - 19), 5);
if ($numregion =~ /(-?[0-9]+)/) {
$sum = $sum + $&;
$num++;
}
}
}
if ($num == 0) {
$min = 0;
}
else {
$min = $sum/$num;
if ($min =~ /(-?[0-9]+\.[0-9]{2})/) { #the average is intended
$min = $&; #to be rounded off here,
} #but it is merely chopped
} #due to limited know-how
#on perl rounding.
return $min;
}
sub maxtemp {
local $seg = $_[0];
local ($max, $size, $line, $numregion);
local $num = 0;
local $sum = 0;
local @words = split(/\n/,$seg);
foreach (@words) {
if ($_ =~ /^([A-Z]{1}[a-z]{1})/) {
$line = $_;
chomp($line);
$size = length($line);
$numregion = substr($line, ($size - 12), 5);
if ($numregion =~ /(-?[0-9]+)/) {
$sum = $sum + $&;
$num++;
}
}
}
if ($num == 0) {
$max = 0;
}
else {
$max = $sum/$num;
if ($max =~ /([0-9]+\.[0-9]{2})/) {
$max = $&;
}
}
return $max;
}
sub rainfall {
local $seg = $_[0];
local ($rain, $size, $line, $lastchar, $numregion);
local $num = 0;
local $sum = 0;
local @words = split(/\n/,$seg);
foreach (@words) {
if ($_ =~ /^([A-Z]{1}[a-z]{1})/) {
$line = $_;
chomp($line);
$size = length($line);
$lastchar = substr($line, ($size - 1), 1);
if (($lastchar ne " ") && ($lastchar ne "-")){
$line =~ s/(tr)/0/gi;
$numregion = substr($line, ($size - 5), 5);
$numregion =~ /\S+/;
$sum = $sum + $&;
$num++;
}
}
}
if ($num == 0) {
$rain = 0;
}
else {
$rain = $sum/$num;
if ($rain =~ /([0-9]+\.[0-9]{2})/) {
$rain = $&;
}
}
return $rain;
}
print<
EOM