Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use Perl's LWP or lib curl?

Tags:

perl

As I understand that (from HTTP client perspective) we have LWP and libcurl (WWW::Curl) available in Perl. Do we have any criteria which one to choose?

like image 815
rpg Avatar asked Dec 30 '11 19:12

rpg


1 Answers

Just talking from an API perspective, I prefer LWP. The problem with Curl is that it's very obviously made off of a C library. For instance:

$curl->setopt(CURLOPT_URL, 'http://example.com');
my $response_body;
$curl->setopt(CURLOPT_WRITEDATA, \$response_body);

my $retcode = $curl->perform;
if ($retcode == 0) {
    # Response is now in $response_body
}
else {
    die "Error\n";
}

Setting parameters with setopt()? Returning the response using a reference to one of those parameters? Having a method return 0 on success? These things are idiomatic in C code, but not in modern OO Perl.

Here's roughly the same code in LWP:

my $response = $lwp->get('http://example.com');
if( $response->is_success ) {
    $response_body = $response->decoded_content;
}
else {
    die "Error\n";
}

The call to is_success() is more self-documenting and blends better inside an OO language. C coders got used to seeing code like if($retcode == 0) on success for historical reasons, but there's no reason Perl coders should pick up this habit. The above also shows how LWP easily takes care of content decoding for us, which Curl leaves for you to do.

It's not shown above, but Curl also forces you to handle GET/POST parameter parsing on your own. In LWP, you pass a hash and it breaks down the name=value pairs for you. Cookies, too. Curl is very low-level that way.

Curl may well be faster, but ask yourself how much that will matter in your application. Are you really going to send 100 requests in a short period of time? If so, then Curl may well be worth it. If not, then go for ease of implementation, which I think LWP is going to win without much of a fight.

like image 163
frezik Avatar answered Sep 20 '22 14:09

frezik