3

我想动态构建一组表单参数以在 HTTP POST 中使用,但我不确定如何访问/构建 LWP::UserAgent 动态使用的数据结构。

典型示例代码将此结构作为请求传递。

my $response = $browser->post(
  'http://example.com/postme',
  [
    'param1'  => 'value1',
    'param2' => 'value2'
  ],
);

我有一组存储在散列中的参数名称和值,我想从我的散列数据中构建方括号中的结构。那是什么结构,我怎样才能做我想做的事?(如您所知,我不是 perl 专家!)

4

1 回答 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.

于 2011-06-03T11:32:05.947 回答