Home arrow Perl Programming arrow Page 4 - Web Access with LWP

Getting the Weather - Perl

There are a number of ways you can retrieve information from the web. You can access it directly via a browser, or you can write a script that gets the information for you and delivers it in a form you can use. The LWP library for Perl can help you with the latter. Keep reading for a closer look.

  1. Web Access with LWP
  2. Making Requests
  3. Making it Work
  4. Getting the Weather
By: Peyton McCullough
Rating: starstarstarstarstar / 4
May 14, 2009

print this article



Earlier, I talked about creating a script that would retrieve the weather. However, the script was left unfinished. That was fine then, since I only wanted to explain how to form a POST request, but let's wrap up by re-examining the weather example in order to create something functional. 

The first thing we should do is rewrite the script so that it uses one of the shortcut methods of LWP::UserAgent that we just examined. Since we need to make a POST request, we'll use the post method. 

We'll need to pass in form data, though, and thankfully, the post method makes this really easy. All we have to do is pass a reference to a dictionary of key/value pairs for the form. This will be passed as the second argument to the method. 

Here's what that looks like:


#!/usr/bin/perluse strict;use LWP::UserAgent;# Create the user agentmy $ua = new LWP::UserAgent;# Post the form datamy $res = $ua->post('http://forecast.weather.gov/zipcity.php',

 {'inputstring' => 'Washington,DC'});


Again, the post method returns a response object. You might think that we should now examine the content of the response object to search for whatever information we need. However, it turns out that the server returns a 302 status code, indicating that the information we want is found elsewhere. The National Weather Service Web site actually finds a geographical point that matches the input string, and then redirects us to the proper page for that point. 

In order to actually view the weather, then, we need to redirect to the proper location. This location is stored in the response headers in a field called “Location” and can be easily extracted using the header method. Here's how we extract the new URL:


# Extract the new locationmy $location = $res->header('Location');


Now we just need to request the new page. After we get the content, we can use regular expressions to extract the current temperature Here's the final few lines of the script:


# Redirect to the new location$res = $ua->get($location);# Get the weatherif ($res->content =~ />(.*?)<br><br>(d+) &d/){ print "$1n$2n";}


Of course, there is a lot more to LWP, but it contains far too much functionality to be summarized in a single article. This article, however, has covered the very basics, and you should now be able to create simple applications that can access the Web.

>>> More Perl Programming Articles          >>> More By Peyton McCullough

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: