输出结果:
F:/>perl/a.pl  
please input some lines,then press Ctrl+Z.  
how are you  
fine,thank you  
^Z  
123456789012345678901234567890  
 how are you#u在第20个字符处  
  fine,thank you
#------------------------
没有chomp的程序:
print "please input some lines,then press Ctrl+Z. /n";  
@s=<STDIN>;  
print "1234567890"x 3 ."/n";  
foreach $s(@s)  
{  
printf "%20s/n",$s;  
} 
输出结果:
F:/>perl/a.pl  
please input some lines,then press Ctrl+Z.  
how are you  
fine,thank you  
^Z  
123456789012345678901234567890  
how are you#u在第19个字符处  
 fine,thank you 
来观察下有什么不同,如果没有用chomp,输出的结果不仅中间有空格,并且可以发现最后的字符却在第9上,相当于在第19个字符处。这是因为perl把a newline 当做一个字符。
第二部分:
如果我们自己指定字符串的宽度,那么程序如下:
输出结果:
F:/>perl/a.pl  
Please input column width.  
30  
please input some lines,then press Ctrl+Z.  
how are you  
fine,thank you  
^Z  
1234567890123456789012345678901234567890123456789012345678901234567890  
   how are you  
fine,thank you 
下面是没有width=<>,没有经过chomp的话,会出现如下结果:
F:/>perl/a.pl  
Please input column width.  
30  
please input some lines,then press Ctrl+Z.  
how are you  
fine,thank you  
^Z  
1234567890123456789012345678901234567890123456789012345678901234567890  
%30#这里的30因为没有去掉转行符,所有是30+转行符,得到了这种结果  
%30  
s 
新闻热点
疑难解答