2011-04-28 29 views
0
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转换为Perl中的变量

HTTP/1.1 302实测值缓存控制: 无缓存,必重新验证到期日: 星期六,01月11日200 5时00分○○秒GMT 地点:cookiecheck = 1 内容类型:文本/ html日期:星期四,28 Apr 2011 09:15:57 GMT服务器: xxxx/0.1内容长度:0 连接:保持活动设置Cookie: auth = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx; expires =星期六,27-Apr-2013 09:15:57 GMT; path = /;域= .foo.com中

谢谢

回答

0

你到底想干什么?可能是LWP::Simple是你需要的...

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有这样做的更好的办法,因为我不知道我为你提供了一个黑客工具,将你所需要的是模块的命令。

相关问题