1. 题目
2. 代码与输出
1
#
-----------------------------------------------------------#
2 # Source: Learning Perl, chapter10,exercise-1
3 # Date: 2012-01-20
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 $secret_num = int( 1 + rand 100);
8 while(<STDIN>) {
9 chomp;
10 if(/^[ 0- 9]+$/) { # 键入都是数字
11 if( $_ < $secret_num) {
12 say " Too low. Try again. ";
13 } elsif( $_ > $secret_num) {
14 say " Too high. Try again. ";
15 } else {
16 say " You get it. ";
17 last;
18 }
19 } elsif(/^quit$|^ exit$|^\s*$/) { # quit,exit,空白行
20 last;
21 } else {
22 say " Not Vaild Input. Try again ";
23 }
24 }
25 # -----------------------------------------------------------#
2 # Source: Learning Perl, chapter10,exercise-1
3 # Date: 2012-01-20
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 $secret_num = int( 1 + rand 100);
8 while(<STDIN>) {
9 chomp;
10 if(/^[ 0- 9]+$/) { # 键入都是数字
11 if( $_ < $secret_num) {
12 say " Too low. Try again. ";
13 } elsif( $_ > $secret_num) {
14 say " Too high. Try again. ";
15 } else {
16 say " You get it. ";
17 last;
18 }
19 } elsif(/^quit$|^ exit$|^\s*$/) { # quit,exit,空白行
20 last;
21 } else {
22 say " Not Vaild Input. Try again ";
23 }
24 }
25 # -----------------------------------------------------------#
1
#
-----------------------------------------------------------#
2 # Source: Learning Perl, chapter10,exercise-2
3 # Date: 2012-01-20
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 $secret_num = int( 1 + rand 100);
8 if( @ARGV[ 0] =~ /-debug/i) {
9 say " [DEBUG] The secret num is $secret_num ";
10 }
11 while(<STDIN>) {
12 chomp;
13 if(/^[ 0- 9]+$/) { # 键入都是数字
14 if( $_ < $secret_num) {
15 say " Too low. Try again. ";
16 } elsif( $_ > $secret_num) {
17 say " Too high. Try again. ";
18 } else {
19 say " You get it. ";
20 last;
21 }
22 } elsif(/^quit$|^ exit$|^\s*$/) { # quit,exit,空白行
23 last;
24 } else {
25 say " Not Vaild Input. Try again ";
26 }
27 }
28 # -----------------------------------------------------------#
2 # Source: Learning Perl, chapter10,exercise-2
3 # Date: 2012-01-20
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 $secret_num = int( 1 + rand 100);
8 if( @ARGV[ 0] =~ /-debug/i) {
9 say " [DEBUG] The secret num is $secret_num ";
10 }
11 while(<STDIN>) {
12 chomp;
13 if(/^[ 0- 9]+$/) { # 键入都是数字
14 if( $_ < $secret_num) {
15 say " Too low. Try again. ";
16 } elsif( $_ > $secret_num) {
17 say " Too high. Try again. ";
18 } else {
19 say " You get it. ";
20 last;
21 }
22 } elsif(/^quit$|^ exit$|^\s*$/) { # quit,exit,空白行
23 last;
24 } else {
25 say " Not Vaild Input. Try again ";
26 }
27 }
28 # -----------------------------------------------------------#
1
#
-----------------------------------------------------------#
2 # Source: Learning Perl, chapter10,exercise-3
3 # Date: 2012-01-20
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 $ENV{ " ZERO "} = 0;
8 $ENV{ " EMPTY "} = '';
9 $ENV{ " UNDEFINED "} = undef;
10 $max_key_len = 0;
11 $key_len_limit = 10;
12 $value_len_limit = 10;
13 while(( $key, $value) = each %ENV) {
14 if( length( $key) > $key_len_limit) {
15 next;
16 }
17 $max_key_len = $max_key_len > length( $key) ? $max_key_len : length( $key);
18 }
19 say " 1234567890 " x 7;
20 while(( $key, $value) = each %ENV) {
21 if( length( $key) > $key_len_limit || length( $value) > $value_len_limit) {
22 next;
23 }
24 printf " % " . $max_key_len . " s " . " %s\n ", $key, $value // " (undefined) ";
25 }
26 # -----------------------------------------------------------#
27 # 如果$value='',那么$value || "(undefined)" 返回"(undefined)"
28 # 如果$value='',那么$value // "(undefined)" 返回'';
29 # 如果$value=undef,$value || "(undefined)"
30 # 和$value // "(undefined)"都返回"(undefined)"
31 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter10,exercise-3
3 # Date: 2012-01-20
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 use 5.010;
7 $ENV{ " ZERO "} = 0;
8 $ENV{ " EMPTY "} = '';
9 $ENV{ " UNDEFINED "} = undef;
10 $max_key_len = 0;
11 $key_len_limit = 10;
12 $value_len_limit = 10;
13 while(( $key, $value) = each %ENV) {
14 if( length( $key) > $key_len_limit) {
15 next;
16 }
17 $max_key_len = $max_key_len > length( $key) ? $max_key_len : length( $key);
18 }
19 say " 1234567890 " x 7;
20 while(( $key, $value) = each %ENV) {
21 if( length( $key) > $key_len_limit || length( $value) > $value_len_limit) {
22 next;
23 }
24 printf " % " . $max_key_len . " s " . " %s\n ", $key, $value // " (undefined) ";
25 }
26 # -----------------------------------------------------------#
27 # 如果$value='',那么$value || "(undefined)" 返回"(undefined)"
28 # 如果$value='',那么$value // "(undefined)" 返回'';
29 # 如果$value=undef,$value || "(undefined)"
30 # 和$value // "(undefined)"都返回"(undefined)"
31 #-----------------------------------------------------------#
3. 文件