開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの機能制限無料版、light版)
- Script言語:Perl
『初めてのPerl 第5版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-427-9) の10章(さまざまな制御構造), 10.11(練習問題)2を解いてみる。
2.
やり方の1つ。(「やり方は何通りもある」(TIMTOWTDI(There Is More Than One Way To Do It.)))
コード(TextWrangler)
#!/usr/bin/env perl use strict; use warnings; my $secret_num = int(1 + rand 100); my $Debug = $ENV{DEBUG} // 1; print "$secret_num\n" if $Debug; print "\$ENV{DEBUG}: $ENV{DEBUG}\n"; print "\$Debug: $Debug\n"; print "1から100までの間の秘密の数当てゲーム(空白で終了)\n"; while (1) { chomp(my $num =); if ( $num =~ /^\s*$/ ){ print "降参\n"; print "正解は$secret_num\n"; last; } elsif ( $num < $secret_num){ print "小さすぎる。\n"; } elsif ( $num > $secret_num){ print "大きすぎる。\n"; } else { print "正解!\n"; last; } }
入出力結果(Terminal)
$ ./perl_program 70 Use of uninitialized value $ENV{"DEBUG"} in concatenation (.) or string at ./perl_program line 8. $ENV{DEBUG}: $Debug: 1 1から100までの間の秘密の数当てゲーム(空白で終了) 70 正解! $
0 コメント:
コメントを投稿