2011-02-25 94 views
1

我需要一个perl内联脚本来打印在线文件的头部分。例如:从perl网站打印头

perl -MLWP::Simple -e "print head \"http:stackoverflow.com\"" 

但是这个打印结果在一行。我需要打印单独的行。

回答

3

一个更多 -

perl -MLWP::Simple -e 'print head("http://stackoverflow.com")->as_string' 

更新,响应/输出–

HTTP/1.1 200 OK 
Cache-Control: public, max-age=60 
Connection: close 
Date: Fri, 25 Feb 2011 21:49:45 GMT 
Vary: * 
Content-Length: 194708 
Content-Type: text/html; charset=utf-8 
Expires: Fri, 25 Feb 2011 21:50:46 GMT 
Last-Modified: Fri, 25 Feb 2011 21:49:46 GMT 
Client-Date: Fri, 25 Feb 2011 21:49:46 GMT 
Client-Peer: 64.34.119.12:80 
Client-Response-Num: 1 

更新一次以上,完整起见。广义采取的argument-

perl -MLWP::Simple -e 'print head(shift||die"Give a URL\n")->as_string' 

perl -MLWP::Simple -e 'print head(shift||die"Give a URL\n")->as_string' http://stackoverflow.com 

我爱我的Perl德,但这可能是这个任务为

curl -I http://stackoverflow.com 

虽然为卷曲v LWP的HEAD反应在这种情况下,不同的一个更好的解决方案。 :)

+1

SUUUPPEEERRR!谢谢! – Ned 2011-02-27 18:02:08

1

head()调用返回一个列表。

该列表在打印时通过连接各个元素进行打印。

相反,加入用“\ n”个:

perl -MLWP::Simple -e "print join('\n', head(\"http:stackoverflow.com\"));" 

一种替代方法是追加“\ n”来的每个元素(这是更好的,因为它在结束打印“\ n”个为好):

perl -MLWP::Simple -e 'print map { "$_\n" } head "http:stackoverflow.com";' 
+0

在第一个例子中,我想'\ n''应该用双引号 – bvr 2011-02-25 20:37:26

1

您需要加入list head()返回。

perl -MLWP::Simple -e "print join qq(\n), head q(http://stackoverflow.com)" 
text/html; charset=utf-8 
196503 
1298659282 
1298659342 
2

哦,我更喜欢这样。需要> 5.10。

perl -MLWP::Simple -E "say for head q(http://stackoverflow.com)" 
text/html; charset=utf-8 
196768 
1298660195 
1298660255 
+1

不能相信花了我很长时间才寻找一个可选的功能命令行开关。我一直在把'使用5.01x'投入到我的线索上来说,嘿。 – Oesor 2011-02-25 19:00:00