1. Question
2. Code and Output
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] + $/) {# All input values are numbers.
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, blank line
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] + $/) {# All input values are numbers
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, blank line
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 # If $ value = '', $ value |" (undefined) "return" (undefined )"
28 # If $ value = '', $ value //" (undefined) "returns '';
29 # If $ value = UNDEF, $ value | "(undefined )"
30 # And $ value // "(undefined)" both return "(undefined )"
31 #-----------------------------------------------------------#
3. File
/Files/pangxiaodong/learns ningperl/ch10-answer.rar