【发布时间】:2011-02-25 18:26:55
【问题描述】:
我需要一个打印在线文件头部的 perl 内联脚本。例如:
perl -MLWP::Simple -e "print head \"http:stackoverflow.com\""
但是这个打印结果是一行。我需要打印单独的行。
【问题讨论】:
标签: perl command-line line head
我需要一个打印在线文件头部的 perl 内联脚本。例如:
perl -MLWP::Simple -e "print head \"http:stackoverflow.com\""
但是这个打印结果是一行。我需要打印单独的行。
【问题讨论】:
标签: perl command-line line head
还有一个-
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
虽然在这种情况下 curl v LWP 的 HEAD 响应不同。 :)
【讨论】:
哦,我更喜欢这个。需要 >5.10。
perl -MLWP::Simple -E "say for head q(http://stackoverflow.com)"
text/html; charset=utf-8
196768
1298660195
1298660255
【讨论】:
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";'
【讨论】:
'\n'应该用双引号
你需要加入head()返回的列表。
perl -MLWP::Simple -e "print join qq(\n), head q(http://stackoverflow.com)"
text/html; charset=utf-8
196503
1298659282
1298659342
【讨论】: