Perl
  Home arrow Perl arrow Page 4 - Web Access with LWP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PERL

Web Access with LWP
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2009-05-14


    Table of Contents:
  • Web Access with LWP
  • Making Requests
  • Making it Work
  • Getting the Weather

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Web Access with LWP - Getting the Weather
    ( Page 4 of 4 )

     

    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 Articles          >>> More By Peyton McCullough
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek