在我的代码中,我将一个变量光盘分配给disc我的linux系统上的命令结果.这输出字符串RESEARCH
my $disc = `disc`;
print "$disc\n";
$disc = chomp($disc);
print "$disc\n";
Run Code Online (Sandbox Code Playgroud)
但是,当我使用chomp从字符串中去除换行符时,它将字符串更改为1.这是输出
RESEARCH
1
Run Code Online (Sandbox Code Playgroud)
到底是怎么回事?
chomp VARIABLE
chomp( LIST )
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)
正确的用法是简单地提供一个将在适当位置改变的变量或列表.你使用的返回值是它"扼杀"其参数列表的次数.例如
chomp $disc;
Run Code Online (Sandbox Code Playgroud)
甚至:
chomp(my $disc = `disc`);
Run Code Online (Sandbox Code Playgroud)
例如,您可以选择整个数组或列表,例如:
my @file = <$fh>; # read a whole file
my $count = chomp(@file); # counts how many lines were chomped
Run Code Online (Sandbox Code Playgroud)
当然,使用单个标量参数,chomp返回值只能是1或0.