3

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 can tell, I'm no perl expert!)

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.