开发者

Print head from site in perl

开发者 https://www.devze.com 2023-02-14 00:35 出处:网络
I need a perl inline script that prints t开发者_开发问答he head section of an online file. For example:

I need a perl inline script that prints t开发者_开发问答he head section of an online file. For example:

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

But this print results in one line. I need to print separate lines.


One more-

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

Update, the response/output–

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

Updated once more, for the sake of completeness. Generalized to take an 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

And I love me teh Perl but this is probably a better solution for this task–

curl -I http://stackoverflow.com

Though the HEAD responses for curl v LWP differ in this case. :)


Ooh, I like this far better. Requires >5.10.

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


The head() call returns a list.

That list, when printed, is printed by concatenating individual elements.

Instead, join with "\n":

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

An alternative is to append "\n" to each element (this is better as it prints "\n" at the end as well):

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


You need to join the list head() returns.

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

精彩评论

暂无评论...
验证码 换一张
取 消