在perl中从站点打印头

时间:2011-02-25 18:26:56

标签: perl command-line line head

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

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

但是这个打印产生了一行。我需要打印单独的行。

4 个答案:

答案 0 :(得分: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

为了完整起见,再次更新。推广参数 -

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

虽然在这种情况下,卷曲与LWP的HEAD响应不同。 :)

答案 1 :(得分:2)

是的,我更喜欢这个。需要> 5.10。

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

答案 2 :(得分: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";'

答案 3 :(得分:1)

你需要加入列表head()返回。

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