10.07.2015 Views

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 5 ■ LWP MODULES 99Recall that the default is 180 seconds. You can set the timeout either when you create thebrowser object or at any time during its life. Assume you have a browser object called$browser. In this example, you set the timeout <strong>to</strong> 30 seconds, instead of the default 180:$browser->timeout(30);Controlling Browser RedirectsBrowser objects created through the LWP::UserAgent class accept HTTP redirects for the GETand HEAD methods. You can change this behavior <strong>to</strong> accept redirects for other combinations ofHTTP methods or disallow redirects entirely. The requests_redirectable attribute acceptsa list of HTTP methods that can be redirected:$browser->requests_redirectable([\@methods]);This list is inclusive, so if you merely call the function with one method as an argument,you overwrite what’s already there. To accept redirects for the POST method (discussed in the“Submitting a <strong>Web</strong> Form” section a little later in this chapter), you add it <strong>to</strong> the list by callingrequests_redirectable:push @{$browser->requests_redirectable}, 'POST';Realize that the requests_redirectable attribute already contains two values: GET andHEAD. Therefore, if you want <strong>to</strong> add a method <strong>to</strong> that list, you must use a method such as push(as in this example). If you don’t push a new value on<strong>to</strong> the stack, you’ll be replacing what’salready there. This can cause no end <strong>to</strong> confusion.Based on that note of caution, it’s sometimes helpful <strong>to</strong> see if a particular method willindeed accept a redirect for a given browser object. A call <strong>to</strong> the redirect_ok() method willreturn true if a redirect would be permitted for the given method. Consider this example:if ($browser->redirect_ok(GET)) {print "The browser object would accept a redirect for GET\n";}Sending Additional HeadersIn some cases, you may need <strong>to</strong> specify additional header lines as part of the request for a URL.In these instances, you can send them along with the request as key/value pairs. For example,a GET method using the get() function would normally look like this:$browser->get($url);To include additional headers, place them after the URL, as in this example:$browser->get($url, Header => Value, Header => Value . . .)A use for this might be <strong>to</strong> send the acceptable character set <strong>to</strong> the server:$browser->get($url, 'Accept-Charset' => 'iso-9859-1');Cloning the BrowserIf you already have a browser object set up in your program and configured as you like it, youcan use the clone() method <strong>to</strong> quickly create a duplicate of the browser object. Assume that

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!