2011-02-25 99 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