perl - Perl 中的 chomp 未按预期工作

标签 perl chomp

我在 Perl 中发现了 chomp 的奇怪行为,我无法理解为什么 chomp 会这样工作。

以下行无法按预期工作

if ( chomp($str1) eq chomp($str2) )

但是,以下工作正常

chomp $str1;
chomp $str2;
if ( $str1 eq $str2 )

您能否对 chomp 的这种行为提供一些见解?

最佳答案

chomp 修改其参数。它不返回修改后的参数。事实上,第二个示例是您应该如何使用它。

编辑:perldoc -f chomp 说:

   chomp   This safer version of "chop" removes any trailing string that
           corresponds to the current value of $/ (also known as
           $INPUT_RECORD_SEPARATOR in the "English" module).  It returns
           the total number of characters removed from all its arguments.

关于perl - Perl 中的 chomp 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9270915/

相关文章:

regex - 无法理解 !~ 在正则表达式中

perl - 如何在运行的 Perl session 中编译 Perl 脚本?

ruby - 如果没有该特定行,我的代码将无法工作。为什么?

perl - 我们在Perl中有自动冠军吗?

linux - 浏览器尝试下载 .pl 文件而不是执行它 - apache2、perl cgi 脚本、ubuntu

perl - 签名不吸水

perl - 在可变长度文件名中使用 bash 排序命令

perl - Perl 中的 "Use of uninitialized value in scalar chomp"

perl - 如何将多行处理/存储到从 perl 文件读取的单个字段中?

Android 滑动 View ?