Search iEntry News

An Alternative To LWP And LWP::Parallel

By Michael Marr
Expert Author
Article Date: 2010-11-02

The benefit of using a Perl library is that you can instantly tap the knowledge and experience of one or more programmers who have already sought out and accomplished what you wish to harness from a given library. However, when utilizing an unmaintained library, you often run into new problems that have arisen since the library was maintained.

Although giving us a head start on the task at hand, often times digging through code that is not our own takes longer than generating the same code from the scratch (especially when undocumented or poorly written). Therefore, we should aim to avoid using unmaintained or out-dated libraries.

The World-Wide Web library for Perl, or LWP, is a vital tool in a Perl programmer's arsenal. We can use it to access website APIs like Twitter, download webpages, connect to FTP servers, and a myriad of other web related functions. The module, LWP::Parallel, is a way to do multiple of these requests at once, and thus saving valuable user time by consolidating wait times for requests. Bryan Young discussed these advantages briefly and how to accomplish this here. Although there are not many circumstances where LWP can not handle the job effectively and efficiently, LWP::Parallel, unfortunately, is no longer maintained. Luckily, we have an alternative to LWP::Parallel with WWW::Curl::Multi.

Like LWP, it is easy to grab a webpage with WWW::Curl.


use WWW::Curl::Easy;
my $curl = WWW::Curl::Easy->new;
$curl->setopt(CURLOPT_HEADER, 1);
$curl->setopt(CURLOPT_URL, 'http://perlpronews.com');
my $response;
$curl->setopt(CURLOPT_WRITEDATA, \$response);
my $retcode = $curl->perform;

The above code stores the returned data into >$response. With WWW::Curl::Multi, we initialize multiple Curl handles in the same fashion as above. Then, we use the >add_handle() method to gather these handles. When executing >perform on a Curl::Multi object, it will take all the Curl handles stored in that same object and send the requests in parallel. By utilizing a hash or array for our responses, we can easily access those responses once the parallel requests are complete. For a more complete example of how to utilize Curl::Multi, check out the example on it's CPAN page.



About the Author:
Michael Marr is a IT staff Writer for WebProNews.




Newsletter Archive | Article Archive | Submit Article | Advertising Information | About Us | Contact

PerlProNews is an iEntry, Inc. ® publication - All Rights Reserved Privacy Policy and Legal