views:

56

answers:

2

In a Perl script (with Ubuntu) I'd like to do something like

    use Blah;
    ...
    ...
    my $response = 
       &Blah::Fetch($URL, {'method'=>'POST', 'parameters' => \%params});

which I've written for convenience to look a lot like a Prototype.js ajax call, but obviously we're using Perl not Javascript, we're on a server not a browser, and the caller wants to block until we get an answer or timeout back from the remote server. In case of a server or timeout error, defined($response) should be false. If there's a way to fetch HTTP status that's nice, but it is enough to know the request failed. It should be able to do either GET or POST.

I know I can do this by using system and wget, but that's a kludge.

What is the best way to do this task in Perl?

Is there a Perl interface that is nice and tidy?

+4  A: 

You can use HTTP::Request

use HTTP::Request::Common qw(POST); 
use LWP::UserAgent; 

$ua = LWP::UserAgent->new; 
$ua->timeout(3); 

my $req = (POST 'http://stackoverflow.com',  
["param1" => $var1, 
"param2" => $var2]); 

$response = $ua->request($req); 
$content = $response->content; 

exit;

For response elements, see more here: http://kobesearch.cpan.org/htdocs/libwww-perl/HTTP/Request.html

Zurahn
+4  A: 

The de facto methods are with LWP and WWW:Mechanize to pass on the request. These libraries are often included in Perl bundles, but otherwise are available via CPAN. CGI.pm is the most basic module for handling most simple web requests.

Tutorials for both are common, try http://perl.com/ , http://PerlMonks.org/ http://perldoc.perl.org/ amongst others. LWP also offers LWP::Simple if your needs are basic.

mctylr