1. 习题
2. 代码与输出
cat_reverse.pl
1 # -----------------------------------------------------------# 2 # Source: Learning Perl, chapter5, exercise-1 3 # Date: 2012-01-15 4 # Author: xiaodongrush 5 #-----------------------------------------------------------# 6 use 5.010; 7 while(<>) { chomp; unshift @array, $_; } 8 foreach( @array) { say $_; } 9 <STDIN> 10 # @_表示子程序的参数列表,$_是Perl的老地方,容易混淆
printf_example.pl
1 # -----------------------------------------------------------# 2 # Source: Learning Perl, chapter5, exercise-2 3 # Date: 2012-01-15 4 # Author: xiaodongrush 5 #-----------------------------------------------------------# 6 use 5.010; 7 say " Input several str! (Use Control+Z to Stop Input). "; 8 while(<>) { chomp; push @strs, $_; } 9 say " 1234567890 " x 4; 10 foreach( @strs) { printf " %20s\n ", $_; } 11 <STDIN>
printf_example_2.pl
1 # -----------------------------------------------------------# 2 # Source: Learning Perl, chapter5, exercise-3 3 # Date: 2012-01-15 4 # Author: xiaodongrush 5 #-----------------------------------------------------------# 6 use 5.010; 7 say " Input several str! (Use Control+Z to Stop Input). "; 8 while(<>) { chomp; push @strs, $_; } 9 print " The Width of the Rule Line Width = "; $width = <STDIN>; 10 print " 1234567890 " x ( $width/ 10); 11 foreach( 1.. 9) { if( $_ <= ( $width% 10)) { print $_; } } say; 12 foreach( @strs) { printf " %20s\n ", $_; } 13 <STDIN>
3. 文件