Perl
  Home arrow Perl arrow Page 2 - 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 - Making Requests
    ( Page 2 of 4 )

     

    For simple applications, LWP::Simple provides all the functionality necessary. However, sometimes you'll need more control over the process, and the LWP library provides the means necessary to exercise this control. For example, say that you want to submit some form data. Obviously, this requires a bit more work in some cases. 

    Now we're going to take a look at some more advanced functionality that requires a bit of extra effort on the developer's part. Before, when we wanted the content of a page, we only had to call one message. This would request the desired document and either return the content or output it somewhere. However, we can break this up and perform the individual steps ourselves. This first involves preparing an HTTP request. This is done with the HTTP::Request module, and it isn't very difficult. 

    Since this isn't an article on HTTP, we'll skip the underlying details. Let's jump right in and see how this looks in Perl. Say we want to once again request the Google index page. We'd simply have to create an HTTP::Request object, which represents the request. The constructor takes, at a minimum, two arguments. The first is the method to use (GET or POST are the most common), and the second argument is the URI. 

    So, here's how we would create a request for the Google index page:

     

    use HTTP::Request;my $google_request = new HTTP::Request(GET => 'http://google.com');

     

    If that's all we want to do, though, we may be better off just using LWP::Simple. Say, however, that we wanted to post form data. For example, the National Weather Service's (US) Web site provides a way to look up the forecast for a given city. It gets the city name from form data, through the POST method. What if we wanted to get the weather for Washington, DC? 

    Let's create another request, this time to get the weather. When creating the object, we need to pass in POST rather than GET, and we need to modify the URI. Then, we need to set the content type to an appropriate value. This is done using the content_type method. Finally, we need to specify the form values. These form values are stored in the body of the HTTP request and are set using the content method. 

    Here's what the result looks like:

     

    my $request = new HTTP::Request(POST => 'http://forecast.weather.gov/zipcity.php');$request->content_type('application/x-www-form-urlencoded');$request->content('inputstring=Washington,DC');

     



     
     
    >>> 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek