chomp 是 chop 的安全版本,相对于chop 删除字符串或list最后任意字符。
chomp 只删除 '\n',否则不删除。
VARIABLE == string
例1:$str="test function of chomp\n";chomp($str);#去掉结尾的\n例2:$str=;#从标准输入中读入chomp($str);上面的二行可以合并为chomp($str=)例3.$test="string";chop$test;print$test;#结尾的g将被去掉备注:
1.在使用chomp的时候,可以不使用圆括号(),即chomp$str;
2.如果字符串结尾有2个或2个以上的换行符\n,chomp只去掉一个。
3.如果字符串结尾没有换行符,那chomp什么都不做,返回0。
VARIABLE == hash
If VARIABLE is a hash, it chomps the hash's values,
but not its keys, resetting the each iterator in the process
VARIABLE == list
If you chomp a list, each element is chomped,
and the total number of characters removed is returned.
while (<>) {chomp; # avoid \n on last fieldmy@array = split(/:/);# ...}