1

私のコードではdisc、Linux システムでのコマンドの結果と等しくなるように変数 disk を割り当てています。これにより、文字列 RESEARCH が出力されます

my $disc = `disc`;
print "$disc\n";
$disc = chomp($disc);
print "$disc\n";

ただし、chomp を使用して文字列から改行文字を削除すると、文字列が 1 に変更されます。出力は次のとおりです。

RESEARCH

1

何が起こっている?

4

3 に答える 3

7

perldoc -f chompから:

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. 

適切な使用法は、その場で変更される変数またはリストを単に提供することです。使用する戻り値は、引数リストを「チョッピング」した回数です。例えば

chomp $disc;

あるいは:

chomp(my $disc = `disc`);

たとえば、配列またはリスト全体を chomp することができます。

my @file = <$fh>;          # read a whole file
my $count = chomp(@file);  # counts how many lines were chomped

もちろん、スカラー引数が 1 つの場合、chomp の戻り値は 1 または 0 になります。

于 2013-02-18T11:59:53.603 に答える
3

chomp の結果を変数に代入しないでください。

$disc = chomp($disc);

使用する:

chomp($disc);

これは、chomp が指定された文字列を変更し、すべての引数から削除された文字の総数を返すためです。

于 2013-02-18T12:01:03.123 に答える
2

chompchomp $discは削除された文字数を返すため、何の影響もなく使用してください。

于 2013-02-18T11:58:04.763 に答える