How to build LWP::UserAgent form parameters dynamically?
I want to build a set of form parameters for use in an HTTP POST on the fly, but I'm not sure how to access/build the data structure LWP::UserAgent uses dynamically.
The typical example code has this structure being passed as a request.
my $response = $browser->post(
'http://example.com/postme',
[
'param1' => 'value1',
'param2' => 'value2'
],
);
I have a set of parameter names and values stored in a hash, and I want to build the structure in the square brackets from my hash data. What is that structure, and how can I do what I want to do? (as you ca开发者_运维问答n tell, I'm no perl expert!)
The square brackets construct an arrayref, but in this case the post
method accepts either an arrayref or a hashref. So you can just do:
my %params;
$params{param1} = 'value1'; # store parameters into %params here
my $response = $browser->post('http://example.com/postme', \%params);
Read perlreftut for an introduction to references, and perlref for more details.
精彩评论