Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Grasping for Tags

August 9, 1999

Presumably, these comments are used by the Weather Underground's own processing scripts but they benefit us as well. Our script need only search for the temperature comment to grab the current reading ... which is exactly what we do to pluck the temperature from this page, followed by a conversion to Fahrenheit (the Weather Underground's internal data assumes Celsius, the metric standard).

Plucking the temperature from a complex page.
#grab temperature and convert to Fahrenheit
if 
($weatherPage=~/<!-- Temperature == ([0-9]+\.[0-9]+) -->/i)
 {$weatherTemp=$1;
  #convert C to F below
  $weatherTemp=int(($weatherTemp*1.8)+32);
  $weatherTemp.="&deg; F"
 } else 
    {$weatherTemp="N/A"}

An if clause determines whether the retrieved page contains a temperature comment tag at all. If, for instance, the page was unavailable due to network problems or a site problem, this if test would fail and the $weatherTemp variable would receive the value "N/A", or "Not Available".

Notice the regular expression match used to locate the temperature comment tag. It tries to match the tag exactly as it appears in the HTML document yet allows for any possible temperature in the clause "([0-9]+\.[0-9]+)". Translated, we can read this regular expression as "one or more of any digit followed by a decimal followed by one or more of any digit". Because this clause is grouped within parentheses the matching portion will be automatically assigned to the Perl variable $1 (if there was a second grouped clause its match would be assigned to $2 and so on).

We assign the temperature -- $1 -- to $weatherTemp and then perform a few calculations to convert the number into Fahrenheit and then tack on the HTML degree symbol and Fahrenheit label. Now that we've got the temperature, it can easily be substituted into the correct placeholder, completing the new weather-aware Smallville Gazette. View a live demo of the enhanced Gazette.

smallville2.cgi: New weather-aware version of the Smallville Gazette.
#!/usr/bin/perl
use CGI;
use LWP::Simple;

$cgiobject=new CGI;
$cgiobject->use_named_parameters;

print $cgiobject->header;

#read in template page
$templateFile="smallville.html";
open(INFILE,"<$templateFile");
@templatePage=<INFILE>;
close(INFILE);

#condense page array into one scalar variable
$resultPage=join("",@templatePage);

#determine current date
($sec,$min,$hour,$mday,
 $mon,$year,$wday,$yday,$isdst)=localtime(time);
$curDay=(Sunday,Monday,Tuesday,Wednesday,
         Thursday,Friday,Saturday)[$wday];
$curMonth=(January,February,March,April,
           May,June,July,August,September,
           October,November,December)[$mon];
$liveDate="$curDay, $curMonth $mday, ".($year+1900);

#search-and-replace on date
$resultPage=~s/<!--INSERT DATE HERE-->/$liveDate/g;

#set URL to weather web page
$weatherURL="http://www.wunderground.com/cgi-bin/
findweather/getForecast?query=14850";
#retrieve this web page
$weatherPage=get($weatherURL);
#grab temperature and convert to Fahrenheit
if 
($weatherPage=~/<!-- Temperature == ([0-9]+\.[0-9]+) -->/i)
 {$weatherTemp=$1;
  #convert C to F below
  $weatherTemp=int(($weatherTemp*1.8)+32);
  $weatherTemp.="&deg; F"
 } else 
    {$weatherTemp="N/A"}
 
#search-and-replace on weather
$resultPage=~s/<!--INSERT WEATHER HERE-->/$weatherTemp/g;  

#done, output page to browser
print $resultPage;

Simply, LWP::Simple
The Perl You Need to Know
Pulling Tags Like Taffy: TokeParser


Up to => Home / Authoring / Languages / Perl / PerlfortheWeb




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers