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 ModulesLWP is an abbreviation for library of WWW modules in <strong>Perl</strong>. LWP modules enable you <strong>to</strong>incorporate common web tasks in<strong>to</strong> your <strong>Perl</strong> program through a set of functions that canbe imported in<strong>to</strong> your namespace.Using the LWP modules (the LWP, for short), you can perform tasks such as retrievingweb pages, submitting web forms, and mirroring a web site. As with other tasks in <strong>Perl</strong>, youcould accomplish these same things without the help of the LWP. However, this chapterwill concentrate on using the LWP modules (thus the title). The LWP contains a number ofpro<strong>to</strong>col methods, including ones <strong>to</strong> work with HTTP, HTTPS, FTP, NNTP, and others. Thischapter looks at how <strong>to</strong> use the LWP with HTTP and HTTPS.Getting Started with the LWPTo use the LWP modules, you need <strong>to</strong> first obtain and install them. Distributions such asDebian have the LWP prepackaged, which makes installation rather trivial (apt-get installlibwww-perl). If your distribution doesn’t contain a prepackaged version of the LWP, you candownload and install the software manually. The LWP modules are available from your favoriteCPAN mirror.It’s always a good idea <strong>to</strong> check whether the modules are already installed prior <strong>to</strong> goingthrough the job of installing them. An easy method for testing this is with the following command,executed from the shell:perl -MLWP -e 'print "$LWP::VERSION\n"'5.803You have the LWP installed if you see a version number such as this output:If you don’t have the LWP installed, you’ll need <strong>to</strong> perform the installation in order <strong>to</strong>accomplish most of the tasks in this chapter.The LWP primarily works with the HTTP request and response model. This means thatthe module is an excellent choice for retrieving and parsing web pages on the Internet. Here’sa quick example <strong>to</strong> get your feet wet. The code (Getua.pl) retrieves a web page and prints it all<strong>to</strong> STDOUT.89

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

Saved successfully!

Ooh no, something went wrong!