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.

100CHAPTER 5 ■ LWP MODULESyou have a browser object, $browser, already created. The following code would create a duplicateof that browser object:$browser2 = $browser->clone();Submitting a <strong>Web</strong> FormTwo HTTP methods are used <strong>to</strong> pass form variables in<strong>to</strong> a script on the web: GET and POST. UsingGET, the parameters are passed as part of the URL itself in name=value pairs. This type of submissionusing the LWP is rather trivial and can be accomplished in a number of ways through variousGET methods, as you’ve already seen in this chapter.However, even though GET is the most commonly used method, the POST method is alsofrequently used, especially when working with web forms or web services.Using GET, any parameters passed in<strong>to</strong> a CGI application are passed via the URL. This canbe problematic for three main reasons:• Some browsers and servers limit the length of the URL, thus making complicatedparameter passing more difficult.• All characters in the URL must be encoded in order <strong>to</strong> be safe for URLs.• Parameters passed on the URL are visible <strong>to</strong> anyone listening, regardless of whether ornot SSL (HTTPS) is used.In contrast, using POST, all of the parameters are passed as part of the message body. Thisalone effectively removes all three problems with GET. Parameters passed via POST aren’t limitedby length, nor do they need <strong>to</strong> be encoded. And since the parameters are passed withinthe body, they are indeed encrypted when passed over SSL.Using the LWP post() method, the name=value pairs are passed as an array—well, actuallyas a reference <strong>to</strong> an array, as you’ll see shortly.When working with forms, there are a number of form elements that appear insidethe tags on the page. For example, assume a web form located at http://www.example.com/form.cgi contains text boxes <strong>to</strong> fill in with information such as the user’sname, e-mail address, and zip code. The name=value parameters might look like this for a filledinform:name=Steve Suehringemail=suehring@braingia.comzip=54481You can send these in a POST request through the LWP by placing them as argumentswithin the call <strong>to</strong> the post() method of the browser object, as shown here:$result = $browser->post('http://www.example.com/form.cgi',['name' => 'Steve','email' => 'suehring@braingia.com','zip' => '54481']);To analyze a web form, the first task is <strong>to</strong> determine the URL of the target. This is definedin the opening tag as the “action” for the form. <strong>From</strong> there, it’s a matter of determining

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

Saved successfully!

Ooh no, something went wrong!