如何将WWW:Curl::Easy的输出转换成Perl中的变量

如何将WWW:Curl::Easy的输出转换成Perl中的变量,perl,curl,Perl,Curl,它会像这样打印出来。 如何从perform函数将输出转换为变量 HTTP/1.1 302找到缓存控制: 无缓存,必须重新验证过期: 星期六,2001年1月11日05:00:00 GMT 位置:?cookiecheck=1 内容类型:text/html日期:28日星期四 2011年4月09:15:57 GMT服务器: xxxx/0.1内容长度:0 连接:保持活动集Cookie: auth=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx; expires=2013年

它会像这样打印出来。 如何从perform函数将输出转换为变量

HTTP/1.1 302找到缓存控制: 无缓存,必须重新验证过期: 星期六,2001年1月11日05:00:00 GMT 位置:?cookiecheck=1 内容类型:text/html日期:28日星期四 2011年4月09:15:57 GMT服务器: xxxx/0.1内容长度:0 连接:保持活动集Cookie: auth=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx; expires=2013年4月27日星期六09:15:57 GMT; 路径=/;域=.foo.com


谢谢你

你想做什么?可能正是您所需要的…

我同意PacoRG的观点,您最有可能从
LWP::
空间研究如何使用模块。由于您有更具体的需求,我建议您选择

也就是说,如果您真的需要将要打印的内容存储在变量中,我们可以使用更深入的Perl魔术来玩一些游戏

use WWW::Curl::Easy;

$curl->setopt(CURLOPT_HEADER,1);    
$curl->setopt(CURLOPT_RETURNTRANSFER,1);    
$curl->setopt(CURLOPT_URL,"http://foo.com/login.php");
$curl->setopt(CURLOPT_POSTFIELDS,"user=usertest&pass=passwdtest");
$curl->perform();
也许
WWW::Curl::Easy
有更好的方法来实现这一点,因为我不知道该模块的命令,我已经为您提供了一种可以满足您需要的方法

# setopt method calls here

## the variable you want to store your data in
my $variable;

{
  ## open a "filehandle" to that variable
  open my $output, '>', \$variable;

  ## then redirect STDOUT (where stuff goes when it is printed) to the filehandle $output
  local *STDOUT = $output;

  ## when you do the perform action, the results should be stored in your variable
  $curl->perform();
}

## since you redirected with a 'local' command, STDOUT is restored outside the block
## since $output was opened lexically (with my), its filehandle is closed when the block ends

# do stuff with $variable here