chomp in perl没有按预期工作

Bil*_*ill 3 perl chomp

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

以下行无法按预期工作

if ( chomp($str1) eq chomp($str2) )
Run Code Online (Sandbox Code Playgroud)

但是,以下工作正常

chomp $str1;
chomp $str2;
if ( $str1 eq $str2 )
Run Code Online (Sandbox Code Playgroud)

你能否对chomp的这种行为有所了解?

小智 12

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.
Run Code Online (Sandbox Code Playgroud)