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

时间:2011-04-28 09:20:12

标签: perl curl

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();

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

  

HTTP / 1.1 302找到缓存控制:   no-cache,must-revalidate过期:   周六,11 Jan 200 05:00:00 GMT   位置:?cookiecheck = 1   内容类型:text / html日期:星期四,28   2011年4月09:15:57 GMT服务器:   xxxx / 0.1内容长度:0   连接:Keep-Alive Set-Cookie:   AUTH = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;   到期= 2013年4月27日星期六09:15:57 GMT;   路径= /;域= .foo.com中

谢谢

2 个答案:

答案 0 :(得分:2)

我同意PacoRG,你很可能应该考虑使用LWP::空间中的模块。由于您有更具体的需求,我建议使用LWP::UserAgent

如果你真的需要将一些正在打印的东西存储在一个变量中,那么我们可以玩一些更深入的Perl魔法游戏。

# 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

也许WWW::Curl::Easy有更好的方法可以做到这一点,因为我不知道那个模块的命令我为你提供了一个可以满足你需要的黑客。

答案 1 :(得分:0)

你想做什么?可能LWP::Simple可能是你需要的......

相关问题