我在 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/