perl中的chomp无法按预期工作
问题描述:
我在Perl中发现了chomp的一种奇怪行为,我无法理解为什么chomp会像这样工作.
I found a strange behavior of chomp in Perl and I am unable to comprehend why is chomp is working like this.
以下行无法按预期运行
if ( chomp($str1) eq chomp($str2) )
但是,以下方法可以正常工作
But, the following works fine
chomp $str1;
chomp $str2;
if ( $str1 eq $str2 )
请问您对这种行为有何见解?
Can you please give some insight in this behavior of chomp?
答
chomp
修改其参数.它不返回修改后的参数.实际上,第二个示例是您应该如何使用它.
chomp
modifies its argument. It does not return a modified argument. The second example is, in fact, how you're supposed to use it.
编辑 :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.