Chapter 3. The LWP Class Model

For full access to every part of an HTTP transaction—request headers and body, response status line, headers and body—you have to go beyond LWP::Simple, to the object-oriented modules that form the heart of the LWP suite. This chapter introduces the classes that LWP uses to represent browser objects (which you use for making requests) and response objects (which are the result of making a request). You’ll learn the basic mechanics of customizing requests and inspecting responses, which we’ll use in later chapters for cookies, language selection, spidering, and more.

The Basic Classes

In LWP’s object model, you perform GET, HEAD, and POST requests via a browser object (a.k.a. a user agent object) of class LWP::UserAgent, and the result is an HTTP response of the aptly named class HTTP::Response. These are the two main classes, with other incidental classes providing features such as cookie management and user agents that act as spiders. Still more classes deal with non-HTTP aspects of the Web, such as HTML. In this chapter, we’ll deal with the classes needed to perform web requests.

The classes can be loaded individually:

use LWP::UserAgent;
use HTTP::Response;

But it’s easiest to simply use the LWP convenience class, which loads LWP::UserAgent and HTTP::Response for you:

use LWP;               # same as previous two lines

If you’re familiar with object-oriented programming in Perl, the LWP classes will hold few real surprises for you. All you need is to learn the names ...

Get Perl & LWP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.