1. 题目
2. 代码
1 #-----------------------------------------------------------#
2 # Source: Learning Perl, chapter2
3 # Date: 2012-01-12
4 # Author: xiaodongrush
5 #-----------------------------------------------------------#
6 # code-1 #
7 #-----------------------------------------------------------#
8 $split = "-------------------------------------------------";
9 $code1 = 'print 12.5 * 2 * 3.14_15926_54 . "\n";';
10 print "code-1\n" . $code1 . "\n" . $split;
11 #-----------------------------------------------------------#
12 # execute-1 #
13 #-----------------------------------------------------------#
14 print "\nexecute-1\n";
15 print 12.5 * 2 * 3.14_15926_54 . "\n";
16 #-----------------------------------------------------------#
17 # code-2 #
18 #-----------------------------------------------------------#
19 $code2 = 'print "input r\n";' . "\n" .
20 'chomp($r = <STDIN>);' . "\n" .
21 'print $r * 2 * 3.14_15926_54 . "\n";';
22 print $split . "\ncode-2\n" . $code2 . "\n" . $split;
23 #-----------------------------------------------------------#
24 # execute-2 #
25 #-----------------------------------------------------------#
26 print "\nexecute-2\n";
27 print "input r\n";
28 chomp($r = <STDIN>);
29 print $r * 2 * 3.14_15926_54 . "\n";
30 #-----------------------------------------------------------#
31 # code-3 #
32 #-----------------------------------------------------------#
33 $code3 = 'print "input r\n";' . "\n" .
34 'chomp($r = <STDIN>);' . "\n" .
35 'if ($r < 0) {' . "\n" .
36 ' print 0;' . "\n" .
37 '} else { ' . "\n" .
38 ' print $r * 2 * 3.14_15926_54 . "\n";' . "\n" .
39 '}';
40 print $split . "\ncode-3\n" . $code3 . "\n" . $split;
41 #-----------------------------------------------------------#
42 # execute-3 #
43 #-----------------------------------------------------------#
44 print "\nexecute-3\n";
45 print "input r\n";
46 chomp($r = <STDIN>);
47 if ($r < 0) {
48 print 0;
49 } else {
50 print $r * 2 * 3.14_15926_54 . "\n";
51 }
52 #-----------------------------------------------------------#
53 # code-4 #
54 #-----------------------------------------------------------#
55 $code4 = 'print "input x\n";' . "\n" .
56 'chomp($x = <STDIN>);' . "\n" .
57 'print "input y\n";' . "\n" .
58 'chomp($y = <STDIN>);' . "\n" .
59 'print $x . " * " . $y . " = " . ($x * $y) . "\n";';
60 print $split . "\ncode-4\n" . $code4 . "\n" . $split;
61 #-----------------------------------------------------------#
62 # execute-4 #
63 #-----------------------------------------------------------#
64 print "\nexecute-4\n";
65 print "input x\n";
66 chomp($x = <STDIN>);
67 print "input y\n";
68 chomp($y = <STDIN>);
69 print $x . " * " . $y . " = " . ($x * $y) . "\n";
70 #-----------------------------------------------------------#
71 # code-5 #
72 #-----------------------------------------------------------#
73 $code5 = 'print "input str\n";' . "\n" .
74 'chomp($str=<STDIN>);' . "\n" .
75 'print "input num\n";' . "\n" .
76 'chomp($num=<STDIN>);' . "\n" .
77 'print $str x $num . "\n";';
78 print $split . "\ncode-5\n" . $code5 . "\n" . $split;
79 #-----------------------------------------------------------#
80 # execute-5 #
81 #-----------------------------------------------------------#
82 print "\nexecute-5\n";
83 print "input str\n";
84 chomp($str=<STDIN>);
85 print "input num\n";
86 chomp($num=<STDIN>);
87 print $str x $num . "\n";
88 #-----------------------------------------------------------#
89 print "END!!!";
90 <STDIN>
91 #-----------------------------------------------------------#
92 # 总结
93 # 1. 当作数字使用的字符串,可以不用chomp
94 # 2. if-else必须要用"{}"
95 # 3. 字符串重复操作,用x
96 #-----------------------------------------------------------#
3. 运行结果
4. 文件