【问题标题】:Print head from site in perl在 perl 中从站点打印头
【发布时间】:2011-02-25 18:26:55
【问题描述】:

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

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

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

【问题讨论】:

    标签: perl command-line line head


    【解决方案1】:

    还有一个-

    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 响应不同。 :)

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 不敢相信我花了这么长时间才找到一个可选的功能命令行开关。我一直在将“使用 5.01x”扔进我的单线器中使用说,嘿。
      【解决方案3】:

      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'应该用双引号
      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 2018-09-26
        • 1970-01-01
        • 2018-03-26
        • 1970-01-01
        • 2023-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-25
        相关资源
        最近更新 更多