【问题标题】:What is role of #!usr/bin/perl -n? [duplicate]#!usr/bin/perl -n 的作用是什么? [复制]
【发布时间】:2014-09-18 23:36:01
【问题描述】:

#!usr/bin/perl -n的作用是什么?我试图参考书籍和其他来源,但我没有得到更好的主意。它的行为类似于 STDIN,例如 my @array = <> 许多输入存储到 @array 中。 -n 也有这样的行为,但是如何将输入值存储在数据类型中。?哪个是原始功能?

【问题讨论】:

标签: perl


【解决方案1】:

它本质上为您提供了将您放在命令行中的内容包装在里面的管道:

while (<>) {  ... What you have on cmd line here ... }

所以:

perl -e 'while (<>) { if (/^(\w+):/) { print "$1\n"; } }'

还有这个:

perl -n -e 'if (/^(\w+):/) { print "$1\n" }'

是等价的。

【讨论】:

    【解决方案2】:

    使用 -n 选项,脚本中的代码将被解释为您编写的代码:

    while (<>) {
       <code>
    }
    

    例如,以下以文件为参数调用的脚本将替换文件的所有行尾并将结果发送到标准输出:

    #!usr/bin/perl -n
    ~s/\n//;
    print ; 
    

    它必须像这样调用:

    perl script.pl <file>
    

    【讨论】:

      猜你喜欢
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      相关资源
      最近更新 更多