【问题标题】:Behavior of . and , operators in Perl for concatenation and parsing的行为。和 , Perl 中用于连接和解析的运算符
【发布时间】:2013-01-01 20:10:54
【问题描述】:

我试图在 Perl 中使用 ., 运算符,但得到了一些我无法弄清楚的奇怪现象:

如果我运行这个:

print hello . this,isatest, program

输出是:

hellothisisatestprogram

我能理解的是,它将点运算符之前和之后的文本都视为字符串并将它们连接起来。

但是逗号呢?为什么它被省略而不是连接?

【问题讨论】:

  • 启用严格和警告 (use strict; use warnings;) 以摆脱裸字转字符串。然后,考虑:print "hello", "thisisatest", "program"没有 . 运算符 - print 的行为是什么?好的,那么这与 . 运算符一起使用有何意义? (PS 有关特定运算符的问题,请参阅参考/手册/教程首先 - 在这种情况下,请参阅perlop。另请参阅perlvar 中的$, 和@ 中的print 987654334@.)
  • 始终使用use strict; use warnings;

标签: perl parsing concatenation dot-operator


【解决方案1】:

第一个句点 (.) 被视为连接运算符。后面的逗号分隔print 的多个参数。结果是一样的——所有部分都连接起来。如果要打印文字逗号,请将 this,isatest, program 括在引号中 - "this,isatest, program" 以形成单个参数。

http://perldoc.perl.org/functions/print.html

【讨论】:

    【解决方案2】:

    我想这就是你想要的:

    perl -e 'print "hello"." this,isatest,program"."\n"'
    

    运行上面的代码并检查输出。如果它给你想要的输出,那么我想我们有一个解释。

    【讨论】:

      猜你喜欢
      • 2015-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-12
      • 1970-01-01
      • 2012-05-21
      • 2020-10-05
      • 2012-08-13
      相关资源
      最近更新 更多