如何在 Perl 中将名称-值对作为参数传递
我认为这是非常基本的 Perl 问题,但我没有解决。
我正在使用 LWP::UserAgent 包在 perl 脚本中构建 post 请求。 代码如下:
my $urlStr = "http://localhost/testproj/AServlet";
my $postDataStr = "{name => \'ankur434\'}";
my $response = $ua->post($urlStr, $postDataStr);
上面的代码不起作用&给出以下错误 -
<Dec 6, 2010 3:15:54 PM IST> <Error> <HTTP> <BEA-101215> <Malformed Request "/testproj/AServlet". Request parsing failed, Code: -1>
但是,当我直接将 postDataStr 的值传递给 post 方法时,它工作得很好,如下所示:
my $response = $ua->post($urlStr, {name => 'ankur434'});
我尝试了一些选项,例如使用反斜杠(\{)转义 { 但没有任何效果...
任何人都可以提出建议吗?谢谢!
I think this is very basic perl question but I am not getting it through.
I am using LWP::UserAgent package to build a post request in perl script.
The code is as follows:
my $urlStr = "http://localhost/testproj/AServlet";
my $postDataStr = "{name => \'ankur434\'}";
my $response = $ua->post($urlStr, $postDataStr);
The above code doesn't work & gives following error -
<Dec 6, 2010 3:15:54 PM IST> <Error> <HTTP> <BEA-101215> <Malformed Request "/testproj/AServlet". Request parsing failed, Code: -1>
However when I directly pass postDataStr's value to post method, it works perfectly well, like shown below:
my $response = $ua->post($urlStr, {name => 'ankur434'});
I tried few options like escaping { with backward slash (\{) but nothing worked...
Can anyone suggest something? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是一个哈希引用。
正如您的变量名称所示,是一个字符串。
Perl 不会自动将后者转换为前者。
您似乎有这样的印象:在 Perl 中,只有字符串可以分配给变量或传递给子例程。
事实并非如此。只需使用
is a hash reference.
is, as your variable name indicates, a string.
Perl does not automatically convert the latter to the former.
You seem to be under the impression that in Perl, only strings can be assigned to variables or passed to subroutines.
This is not the case. Just use